Skip to content

Commit

Permalink
feat: add constructorOptionDefaults static fn
Browse files Browse the repository at this point in the history
fixes #401
  • Loading branch information
aldeed committed Aug 31, 2020
1 parent d8ae4a9 commit fc93376
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 25 deletions.
66 changes: 41 additions & 25 deletions package/lib/SimpleSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = {};
Expand Down Expand Up @@ -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',
Expand Down
56 changes: 56 additions & 0 deletions package/lib/SimpleSchema.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit fc93376

Please sign in to comment.