@@ -26,7 +26,8 @@ type RuleExpression = MaybeReactive<string | Record<string, any> | GenericValida
2626export function useField ( fieldName : MaybeReactive < string > , rules : RuleExpression , opts ?: Partial < FieldOptions > ) {
2727 const { value, form, immediate, bails, disabled } = normalizeOptions ( opts ) ;
2828 const { meta, errors, failedRules, onBlur, handleChange, reset, patch } = useValidationState ( value ) ;
29- let schemaValidation : GenericValidateFunction | string | Record < string , any > ;
29+ // eslint-disable-next-line prefer-const
30+ let schemaValidation : GenericValidateFunction | string | Record < string , any > | undefined ;
3031 const normalizedRules = computed ( ( ) => {
3132 return normalizeRules ( schemaValidation || unwrap ( rules ) ) ;
3233 } ) ;
@@ -97,9 +98,7 @@ export function useField(fieldName: MaybeReactive<string>, rules: RuleExpression
9798 form . register ( field ) ;
9899
99100 // set the rules if present in schema
100- if ( form . schema ?. [ unwrap ( fieldName ) ] ) {
101- schemaValidation = form . schema [ unwrap ( fieldName ) ] ;
102- }
101+ schemaValidation = extractRuleFromSchema ( form . schema , unwrap ( fieldName ) ) ;
103102
104103 // extract cross-field dependencies in a computed prop
105104 const dependencies = computed ( ( ) => {
@@ -270,3 +269,25 @@ function useAriAttrs(fieldName: MaybeReactive<string>, meta: Record<Flag, Ref<bo
270269 } ;
271270 } ) ;
272271}
272+
273+ /**
274+ * Extracts the validation rules from a schema
275+ */
276+ function extractRuleFromSchema ( schema : Record < string , any > | undefined , fieldName : string ) {
277+ // no schema at all
278+ if ( ! schema ) {
279+ return undefined ;
280+ }
281+
282+ // a yup schema
283+ if ( schema . fields ?. [ fieldName ] ) {
284+ return schema . fields ?. [ fieldName ] ;
285+ }
286+
287+ // there is a key on the schema object for this field
288+ if ( schema [ fieldName ] ) {
289+ return schema [ fieldName ] ;
290+ }
291+
292+ return undefined ;
293+ }
0 commit comments