import { RuleItem , Rules } from 'async-validator' ; import DeepValidator from '../../src/deepValidator' ; describe ( 'DeepValidator' , ( ) => { const rules = { name : { type : 'string' , required : true , validator : ( rule , value ) => value === 'muji' , } , age : { type : 'number' , asyncValidator : ( rule , value ) => new Promise ( ( resolve , reject ) => { if ( value < 18 ) { reject ( 'too young' ) ; } else { resolve ( ) ; } } ) , } , } ; const validator = DeepValidator ( rules ) ; test ( 'registerRule' , async ( done ) => { validator . registerRule ( 'email' , { type : 'email' , } ) ; validator . validateField ( 'email' , { email : 'xxx' } ) . catch ( ( errors ) => { expect ( errors ) . toBeDefined ( ) ; done ( ) ; } ) ; } ) ; test ( 'register deep rule' , async ( done ) => { const friendsValidator = ( rule , value , cb ) => { if ( value . length < 1 ) { cb ( 'must have a friend' ) ; } } ; validator . registerRule ( 'person.friends' , { type : 'array' , validator : friendsValidator , } ) ; expect ( validator . getRules ( ) . person ) . toEqual ( { type : 'object' , fields : { friends : { type : 'array' , validator : friendsValidator , } , } , } as RuleItem ) ; validator . validate ( { person : { friends : [ ] , } , } ) . catch ( ( errors ) => { expect ( errors [ 'person.friends' ] ) . toBeDefined ( ) ; done ( ) ; } ) ; } ) ; } ) ;