File tree 2 files changed +39
-0
lines changed
2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -803,6 +803,12 @@ SchemaType.prototype.get = function(fn) {
803
803
return this ;
804
804
} ;
805
805
806
+ SchemaType . prototype . validateAll = function ( approvalSeekers ) {
807
+ for ( let i = 0 ; i < approvalSeekers . length ; i ++ ) {
808
+ this . validate ( approvalSeekers [ i ] ) ;
809
+ }
810
+ }
811
+
806
812
/**
807
813
* Adds validator(s) for this document path.
808
814
*
Original file line number Diff line number Diff line change @@ -281,4 +281,37 @@ describe('schematype', function() {
281
281
} ) ;
282
282
} ) ;
283
283
} ) ;
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
+ } ) ;
284
317
} ) ;
You can’t perform that action at this time.
0 commit comments