@@ -301,6 +301,63 @@ describe("Test compile (integration test)", () => {
301301
302302 } ) ;
303303
304+ describe ( "Test check generator with custom path & parent" , ( ) => {
305+
306+ it ( "when schema is defined as an array, and custom path & parent are specified, they should be forwarded to validators" , ( ) => {
307+ const v = new Validator ( ) ;
308+ const customValidator = jest . fn ( ) . mockReturnValue ( true ) ; // Will be called with (value, schema, path, parent)
309+ v . add ( "customValidator" , customValidator ) ;
310+
311+ const validate = v . compile ( [ { type : "customValidator" } ] ) ;
312+ const parent = { } ;
313+ const res = validate ( { customValue : 4711 } , "customPath" , parent ) ;
314+
315+ expect ( res ) . toBe ( true ) ;
316+ expect ( customValidator . mock . calls [ 0 ] [ 2 ] ) . toBe ( "customPath" ) ;
317+ expect ( customValidator . mock . calls [ 0 ] [ 3 ] ) . toBe ( parent ) ;
318+ } ) ;
319+
320+ it ( "when schema is defined as an array, path & parent should be set to default values in validators" , ( ) => {
321+ const v = new Validator ( ) ;
322+ const customValidator = jest . fn ( ) . mockReturnValue ( true ) ; // Will be called with (value, schema, path, parent)
323+ v . add ( "customValidator" , customValidator ) ;
324+
325+ const validate = v . compile ( [ { type : "customValidator" } ] ) ;
326+ const res = validate ( { customValue : 4711 } ) ;
327+
328+ expect ( res ) . toBe ( true ) ;
329+ expect ( customValidator . mock . calls [ 0 ] [ 2 ] ) . toBeUndefined ( ) ;
330+ expect ( customValidator . mock . calls [ 0 ] [ 3 ] ) . toBeNull ( ) ;
331+ } ) ;
332+
333+ it ( "when schema is defined as an object, and custom path is specified, it should be forwarded to validators" , ( ) => {
334+ // Note: as the item we validate always must be an object, there is no use
335+ // of specifying a custom parent, like for the schema-as-array above.
336+ // The parent is currently used in the validator code (only forwarded to the generated
337+ // function that validates all properties) and there is no way to test it.
338+ const v = new Validator ( ) ;
339+ const customValidator = jest . fn ( ) . mockReturnValue ( true ) ; // Will be called with (value, schema, path, parent)
340+ v . add ( "customValidator" , customValidator ) ;
341+
342+ const validate = v . compile ( { customValue : { type : "customValidator" } } ) ;
343+ const res = validate ( { customValue : 4711 } , "customPath" ) ;
344+
345+ expect ( res ) . toBe ( true ) ;
346+ expect ( customValidator . mock . calls [ 0 ] [ 2 ] ) . toBe ( "customPath.customValue" ) ;
347+ } ) ;
348+
349+ it ( "when schema is defined as an object, path should be set to default value in validators" , ( ) => {
350+ const v = new Validator ( ) ;
351+ const customValidator = jest . fn ( ) . mockReturnValue ( true ) ; // Will be called with (value, schema, path, parent)
352+ v . add ( "customValidator" , customValidator ) ;
353+
354+ const validate = v . compile ( { customValue : { type : "customValidator" } } ) ;
355+ const res = validate ( { customValue : 4711 } ) ;
356+
357+ expect ( res ) . toBe ( true ) ;
358+ expect ( customValidator . mock . calls [ 0 ] [ 2 ] ) . toBe ( "customValue" ) ;
359+ } ) ;
360+ } ) ;
304361} ) ;
305362
306363describe ( "Test nested schema" , ( ) => {
0 commit comments