diff --git a/package/lib/SimpleSchema.js b/package/lib/SimpleSchema.js index f44eab8..2efa518 100644 --- a/package/lib/SimpleSchema.js +++ b/package/lib/SimpleSchema.js @@ -50,21 +50,18 @@ const propsThatCanBeFunction = [ ]; class SimpleSchema { - constructor(schema = {}, { - check, - clean: cleanOptions, - defaultLabel, - humanizeAutoLabels = true, - requiredByDefault = true, - tracker, - } = {}) { + constructor(schema = {}, options = {}) { // Stash the options object this._constructorOptions = { - check, - defaultLabel, - humanizeAutoLabels, - requiredByDefault, - tracker, + ...SimpleSchema._constructorOptionDefaults, + ...options, + }; + delete this._constructorOptions.clean; // stored separately below + + // Schema-level defaults for cleaning + this._cleanOptions = { + ...SimpleSchema._constructorOptionDefaults.clean, + ...(options.clean || {}), }; // Custom validators for this instance @@ -74,18 +71,6 @@ class SimpleSchema { // Named validation contexts this._validationContexts = {}; - // Schema-level defaults for cleaning - this._cleanOptions = { - filter: true, - autoConvert: true, - removeEmptyStrings: true, - trimStrings: true, - getAutoValues: true, - removeNullsFromArrays: false, - extendAutoValueContext: {}, - ...cleanOptions, - }; - // Clone, expanding shorthand, and store the schema object in this._schema this._schema = {}; this._depsLabels = {}; @@ -872,6 +857,37 @@ class SimpleSchema { SimpleSchema._docValidators.push(func); } + // Global constructor options + static _constructorOptionDefaults = { + clean: { + autoConvert: true, + extendAutoValueContext: {}, + filter: true, + getAutoValues: true, + removeEmptyStrings: true, + removeNullsFromArrays: false, + trimStrings: true, + }, + humanizeAutoLabels: true, + requiredByDefault: true, + }; + + /** + * @summary Get/set default values for SimpleSchema constructor options + */ + static constructorOptionDefaults(options) { + if (!options) return SimpleSchema._constructorOptionDefaults; + + SimpleSchema._constructorOptionDefaults = { + ...SimpleSchema._constructorOptionDefaults, + ...options, + clean: { + ...SimpleSchema._constructorOptionDefaults.clean, + ...(options.clean || {}), + }, + }; + } + static ErrorTypes = { REQUIRED: 'required', MIN_STRING: 'minString', diff --git a/package/lib/SimpleSchema.tests.js b/package/lib/SimpleSchema.tests.js index e5aad7d..e70e520 100644 --- a/package/lib/SimpleSchema.tests.js +++ b/package/lib/SimpleSchema.tests.js @@ -918,6 +918,62 @@ describe('SimpleSchema', function () { SimpleSchema._docValidators = []; }); + it('SimpleSchema.constructorOptionDefaults', function () { + const initialDefaults = SimpleSchema.constructorOptionDefaults(); + + // Default defaults + expect(initialDefaults).toEqual({ + clean: { + autoConvert: true, + extendAutoValueContext: {}, + filter: true, + getAutoValues: true, + removeEmptyStrings: true, + removeNullsFromArrays: false, + trimStrings: true, + }, + humanizeAutoLabels: true, + requiredByDefault: true, + }); + + // Verify they are actually used + const schema = new SimpleSchema(); + expect(schema._constructorOptions.humanizeAutoLabels).toBe(true); + expect(schema._cleanOptions.filter).toBe(true); + + // Change some + SimpleSchema.constructorOptionDefaults({ + humanizeAutoLabels: false, + clean: { + filter: false, + }, + }); + + // Verify they are changed + const newDefaults = SimpleSchema.constructorOptionDefaults(); + expect(newDefaults).toEqual({ + clean: { + autoConvert: true, + extendAutoValueContext: {}, + filter: false, + getAutoValues: true, + removeEmptyStrings: true, + removeNullsFromArrays: false, + trimStrings: true, + }, + humanizeAutoLabels: false, + requiredByDefault: true, + }); + + // Verify they are actually used + const otherSchema = new SimpleSchema(); + expect(otherSchema._constructorOptions.humanizeAutoLabels).toBe(false); + expect(otherSchema._cleanOptions.filter).toBe(false); + + // Don't mess up other tests + SimpleSchema.constructorOptionDefaults(initialDefaults); + }); + it('addDocValidator', function () { const schema = new SimpleSchema({ string: String,