@@ -208,6 +208,37 @@ describe('schematype', function() {
208
208
} ) ;
209
209
} ) ;
210
210
211
+ it ( 'merges default validators (gh-14070)' , function ( ) {
212
+ class TestSchemaType extends mongoose . SchemaType { }
213
+ TestSchemaType . set ( 'validate' , checkIfString ) ;
214
+
215
+ const schemaType = new TestSchemaType ( 'test-path' , {
216
+ validate : checkIfLength2
217
+ } ) ;
218
+
219
+ assert . equal ( schemaType . validators . length , 2 ) ;
220
+ assert . equal ( schemaType . validators [ 0 ] . validator , checkIfString ) ;
221
+ assert . equal ( schemaType . validators [ 1 ] . validator , checkIfLength2 ) ;
222
+
223
+ let err = schemaType . doValidateSync ( [ 1 , 2 ] ) ;
224
+ assert . ok ( err ) ;
225
+ assert . equal ( err . name , 'ValidatorError' ) ;
226
+
227
+ err = schemaType . doValidateSync ( 'foo' ) ;
228
+ assert . ok ( err ) ;
229
+ assert . equal ( err . name , 'ValidatorError' ) ;
230
+
231
+ err = schemaType . doValidateSync ( 'ab' ) ;
232
+ assert . ifError ( err ) ;
233
+
234
+ function checkIfString ( v ) {
235
+ return typeof v === 'string' ;
236
+ }
237
+ function checkIfLength2 ( v ) {
238
+ return v . length === 2 ;
239
+ }
240
+ } ) ;
241
+
211
242
describe ( 'set()' , function ( ) {
212
243
describe ( 'SchemaType.set()' , function ( ) {
213
244
it ( 'SchemaType.set, is a function' , ( ) => {
0 commit comments