11import { computed , isRef , reactive , ref , Ref , unref , watch , MaybeRef , MaybeRefOrGetter } from 'vue' ;
22import { FieldMeta , FieldState , FieldValidator , InputType , PrivateFormContext , PathState } from './types' ;
33import { getFromPath , isEqual , normalizeErrorItem } from './utils' ;
4+ import { TypedSchema } from '../dist/vee-validate' ;
45
56export interface StateSetterInit < TValue = unknown > extends FieldState < TValue > {
67 initialValue : TValue ;
@@ -24,6 +25,7 @@ export interface StateInit<TValue = unknown> {
2425 label ?: MaybeRefOrGetter < string | undefined > ;
2526 type ?: InputType ;
2627 validate ?: FieldValidator ;
28+ schema ?: TypedSchema < TValue > ;
2729}
2830
2931let ID_COUNTER = 0 ;
@@ -37,7 +39,7 @@ export function useFieldState<TValue = unknown>(
3739 if ( ! init . form ) {
3840 const { errors, setErrors } = createFieldErrors ( ) ;
3941 const id = ID_COUNTER >= Number . MAX_SAFE_INTEGER ? 0 : ++ ID_COUNTER ;
40- const meta = createFieldMeta ( value , initialValue , errors ) ;
42+ const meta = createFieldMeta ( value , initialValue , errors , init . schema ) ;
4143
4244 function setState ( state : Partial < StateSetterInit < TValue > > ) {
4345 if ( 'value' in state ) {
@@ -74,6 +76,7 @@ export function useFieldState<TValue = unknown>(
7476 label : init . label ,
7577 type : init . type ,
7678 validate : init . validate ,
79+ schema : init . schema ,
7780 } ) ;
7881
7982 const errors = computed ( ( ) => state . errors ) ;
@@ -207,11 +210,15 @@ function createFieldMeta<TValue>(
207210 currentValue : Ref < TValue > ,
208211 initialValue : MaybeRef < TValue > | undefined ,
209212 errors : Ref < string [ ] > ,
213+ schema ?: TypedSchema < TValue > ,
210214) {
215+ const isRequired = schema ?. describe ?.( ) . required ?? false ;
216+
211217 const meta = reactive ( {
212218 touched : false ,
213219 pending : false ,
214220 valid : true ,
221+ required : isRequired ,
215222 validated : ! ! unref ( errors ) . length ,
216223 initialValue : computed ( ( ) => unref ( initialValue ) as TValue | undefined ) ,
217224 dirty : computed ( ( ) => {
0 commit comments