Skip to content

Commit 4d3243f

Browse files
committed
fix: correctly add prepend default validators re: #14070
1 parent ae961ff commit 4d3243f

File tree

2 files changed

+4
-13
lines changed

2 files changed

+4
-13
lines changed

lib/schemaType.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,7 @@ function SchemaType(path, options, instance) {
6060

6161
for (const option of defaultOptionsKeys) {
6262
if (option === 'validate') {
63-
const defaultValidators = Array.isArray(defaultOptions.validate)
64-
? defaultOptions.validate
65-
: defaultOptions.validate == null
66-
? []
67-
: [defaultOptions.validate];
68-
const specifiedValidators = Array.isArray(options.validate)
69-
? options.validate
70-
: options.validate == null
71-
? []
72-
: [options.validate];
73-
options.validate = [...defaultValidators, ...specifiedValidators];
63+
this.validate(defaultOptions.validate);
7464
} else if (defaultOptions.hasOwnProperty(option) && !Object.prototype.hasOwnProperty.call(options, option)) {
7565
options[option] = defaultOptions[option];
7666
}

test/schematype.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,9 @@ describe('schematype', function() {
212212
class TestSchemaType extends mongoose.SchemaType {}
213213
TestSchemaType.set('validate', v => typeof v === 'string');
214214

215-
const schemaType = new TestSchemaType();
216-
schemaType.validate(v => v.length === 2);
215+
const schemaType = new TestSchemaType('test-path', {
216+
validate: v => v.length === 2
217+
});
217218

218219
let err = schemaType.doValidateSync([1, 2]);
219220
assert.ok(err);

0 commit comments

Comments
 (0)