Skip to content

Commit fc93376

Browse files
committed
feat: add constructorOptionDefaults static fn
fixes #401
1 parent d8ae4a9 commit fc93376

File tree

2 files changed

+97
-25
lines changed

2 files changed

+97
-25
lines changed

package/lib/SimpleSchema.js

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,18 @@ const propsThatCanBeFunction = [
5050
];
5151

5252
class SimpleSchema {
53-
constructor(schema = {}, {
54-
check,
55-
clean: cleanOptions,
56-
defaultLabel,
57-
humanizeAutoLabels = true,
58-
requiredByDefault = true,
59-
tracker,
60-
} = {}) {
53+
constructor(schema = {}, options = {}) {
6154
// Stash the options object
6255
this._constructorOptions = {
63-
check,
64-
defaultLabel,
65-
humanizeAutoLabels,
66-
requiredByDefault,
67-
tracker,
56+
...SimpleSchema._constructorOptionDefaults,
57+
...options,
58+
};
59+
delete this._constructorOptions.clean; // stored separately below
60+
61+
// Schema-level defaults for cleaning
62+
this._cleanOptions = {
63+
...SimpleSchema._constructorOptionDefaults.clean,
64+
...(options.clean || {}),
6865
};
6966

7067
// Custom validators for this instance
@@ -74,18 +71,6 @@ class SimpleSchema {
7471
// Named validation contexts
7572
this._validationContexts = {};
7673

77-
// Schema-level defaults for cleaning
78-
this._cleanOptions = {
79-
filter: true,
80-
autoConvert: true,
81-
removeEmptyStrings: true,
82-
trimStrings: true,
83-
getAutoValues: true,
84-
removeNullsFromArrays: false,
85-
extendAutoValueContext: {},
86-
...cleanOptions,
87-
};
88-
8974
// Clone, expanding shorthand, and store the schema object in this._schema
9075
this._schema = {};
9176
this._depsLabels = {};
@@ -872,6 +857,37 @@ class SimpleSchema {
872857
SimpleSchema._docValidators.push(func);
873858
}
874859

860+
// Global constructor options
861+
static _constructorOptionDefaults = {
862+
clean: {
863+
autoConvert: true,
864+
extendAutoValueContext: {},
865+
filter: true,
866+
getAutoValues: true,
867+
removeEmptyStrings: true,
868+
removeNullsFromArrays: false,
869+
trimStrings: true,
870+
},
871+
humanizeAutoLabels: true,
872+
requiredByDefault: true,
873+
};
874+
875+
/**
876+
* @summary Get/set default values for SimpleSchema constructor options
877+
*/
878+
static constructorOptionDefaults(options) {
879+
if (!options) return SimpleSchema._constructorOptionDefaults;
880+
881+
SimpleSchema._constructorOptionDefaults = {
882+
...SimpleSchema._constructorOptionDefaults,
883+
...options,
884+
clean: {
885+
...SimpleSchema._constructorOptionDefaults.clean,
886+
...(options.clean || {}),
887+
},
888+
};
889+
}
890+
875891
static ErrorTypes = {
876892
REQUIRED: 'required',
877893
MIN_STRING: 'minString',

package/lib/SimpleSchema.tests.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,62 @@ describe('SimpleSchema', function () {
918918
SimpleSchema._docValidators = [];
919919
});
920920

921+
it('SimpleSchema.constructorOptionDefaults', function () {
922+
const initialDefaults = SimpleSchema.constructorOptionDefaults();
923+
924+
// Default defaults
925+
expect(initialDefaults).toEqual({
926+
clean: {
927+
autoConvert: true,
928+
extendAutoValueContext: {},
929+
filter: true,
930+
getAutoValues: true,
931+
removeEmptyStrings: true,
932+
removeNullsFromArrays: false,
933+
trimStrings: true,
934+
},
935+
humanizeAutoLabels: true,
936+
requiredByDefault: true,
937+
});
938+
939+
// Verify they are actually used
940+
const schema = new SimpleSchema();
941+
expect(schema._constructorOptions.humanizeAutoLabels).toBe(true);
942+
expect(schema._cleanOptions.filter).toBe(true);
943+
944+
// Change some
945+
SimpleSchema.constructorOptionDefaults({
946+
humanizeAutoLabels: false,
947+
clean: {
948+
filter: false,
949+
},
950+
});
951+
952+
// Verify they are changed
953+
const newDefaults = SimpleSchema.constructorOptionDefaults();
954+
expect(newDefaults).toEqual({
955+
clean: {
956+
autoConvert: true,
957+
extendAutoValueContext: {},
958+
filter: false,
959+
getAutoValues: true,
960+
removeEmptyStrings: true,
961+
removeNullsFromArrays: false,
962+
trimStrings: true,
963+
},
964+
humanizeAutoLabels: false,
965+
requiredByDefault: true,
966+
});
967+
968+
// Verify they are actually used
969+
const otherSchema = new SimpleSchema();
970+
expect(otherSchema._constructorOptions.humanizeAutoLabels).toBe(false);
971+
expect(otherSchema._cleanOptions.filter).toBe(false);
972+
973+
// Don't mess up other tests
974+
SimpleSchema.constructorOptionDefaults(initialDefaults);
975+
});
976+
921977
it('addDocValidator', function () {
922978
const schema = new SimpleSchema({
923979
string: String,

0 commit comments

Comments
 (0)