Skip to content

Commit cf62382

Browse files
committed
write test
1 parent 3ba9778 commit cf62382

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

lib/schemaType.js

+6
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,12 @@ SchemaType.prototype.get = function(fn) {
803803
return this;
804804
};
805805

806+
SchemaType.prototype.validateAll = function(approvalSeekers) {
807+
for (let i = 0; i < approvalSeekers.length; i++) {
808+
this.validate(approvalSeekers[i]);
809+
}
810+
}
811+
806812
/**
807813
* Adds validator(s) for this document path.
808814
*

test/schematype.test.js

+33
Original file line numberDiff line numberDiff line change
@@ -281,4 +281,37 @@ describe('schematype', function() {
281281
});
282282
});
283283
});
284+
it('demonstrates the `validateAll()` function (gh-6910)', async function() {
285+
const m = new mongoose.Mongoose();
286+
await m.connect(start.uri);
287+
const validateSchema = new m.Schema({ name: String, password: String });
288+
validateSchema.path('name').validate({
289+
validator: function(v) {
290+
return v.length > 5;
291+
},
292+
message: 'name must be longer than 5 characters'
293+
})
294+
validateSchema.path('password').validateAll([
295+
{
296+
validator: function(v) {
297+
return this.name == v;
298+
},
299+
message: `password must not equal name`
300+
},
301+
{
302+
validator: function(v) {
303+
return v.length > 5
304+
},
305+
message: `password must be at least six characters`
306+
}
307+
]);
308+
309+
const Test = m.model('Test', validateSchema);
310+
await Test.deleteMany({});
311+
const check = new Test();
312+
check.name = 'Test';
313+
check.password = 'test';
314+
const test = check.validateSync();
315+
assert.ok(true);
316+
});
284317
});

0 commit comments

Comments
 (0)