@@ -74,6 +74,10 @@ async function _validate(field: FieldValidationContext, value: any) {
7474 } ;
7575 }
7676
77+ if ( isCallable ( field . rules ?. validate ) ) {
78+ return validateWithYup ( field , value ) ;
79+ }
80+
7781 const errors : ReturnType < typeof _generateFieldError > [ ] = [ ] ;
7882 const rules = Object . keys ( field . rules ) ;
7983 const length = rules . length ;
@@ -101,6 +105,32 @@ async function _validate(field: FieldValidationContext, value: any) {
101105 } ;
102106}
103107
108+ /**
109+ * Handles yup validation
110+ */
111+ async function validateWithYup ( field : FieldValidationContext , value : any ) {
112+ const result = await field . rules
113+ . validate ( value )
114+ . then ( ( ) => true )
115+ . catch ( ( err : Error ) => {
116+ // Yup errors have a name prop one them.
117+ // https://github.com/jquense/yup#validationerrorerrors-string--arraystring-value-any-path-string
118+ if ( err . name === 'ValidationError' ) {
119+ return err . message ;
120+ }
121+
122+ // re-throw the error so we don't hide it
123+ throw err ;
124+ } ) ;
125+
126+ const isValid = typeof result !== 'string' && result ;
127+
128+ return {
129+ valid : isValid ,
130+ errors : ! isValid ? [ { msg : result , rule : '' } ] : [ ] ,
131+ } ;
132+ }
133+
104134/**
105135 * Tests a single input value against a rule.
106136 */
@@ -117,6 +147,7 @@ async function _test(field: FieldValidationContext, value: any, rule: { name: st
117147 form : field . crossTable ,
118148 rule,
119149 } ;
150+
120151 const result = await validator ( value , params , ctx ) ;
121152
122153 if ( typeof result === 'string' ) {
0 commit comments