@@ -73,7 +73,9 @@ const {
7373 errorCount,
7474 clearErrors,
7575 getErrorMessage,
76+ errorPaths,
7677 focusFirstErroredInput,
78+ cleanup,
7779} = useFormValidation (schema , form )
7880
7981// Submit your form
@@ -95,6 +97,7 @@ const options = {
9597 // Custom validation logic
9698 return {} // Return errors if any
9799 },
100+ errorStrategy: ' flatten' // or 'deep'
98101}
99102
100103const { validate } = useFormValidation (schema , form , options )
@@ -104,9 +107,11 @@ const { validate } = useFormValidation(schema, form, options)
104107
105108- validate(): Triggers the validation process.
106109- clearErrors(): Resets the validation errors.
107- - getErrorMessage(path: keyof F): Retrieves the error message for a specific field.
110+ - getErrorMessage(path: keyof F | string): Retrieves the error message for a specific field. Supports nested paths like "user.name".
111+ - errorPaths: Computed property that returns an array of all error paths, including nested ones (e.g., [ "email", "user.name"] ).
108112- focusFirstErroredInput(): Focuses the first input with an error.
109- - focusInput(inputName: keyof F): Focuses a specific input by its name.
113+ - focusInput(inputName: keyof F | string): Focuses a specific input by its name. Supports nested paths like "user.name".
114+ - cleanup(): Manually cleans up watchers, event listeners, and caches. Automatically called on component unmount when used inside a component context.
110115
111116## API Reference
112117
@@ -117,6 +122,7 @@ declare function useFormValidation<S extends InputSchema<F>, F extends Form>(
117122 options ? : {
118123 mode? : ' eager' | ' lazy' | ' agressive' | ' onBlur' // lazy by default
119124 transformFn? : GetErrorsFn <S , F >
125+ errorStrategy? : ' flatten' | ' deep'
120126 }
121127): ReturnType <F >
122128```
@@ -128,6 +134,7 @@ declare function useFormValidation<S extends InputSchema<F>, F extends Form>(
128134- ** options ** : Optional configuration object .
129135 - ** mode ** : (optional ) Validation mode (` 'eager' ` for immediate validation ,` 'agressive' ` for validation on load , ` 'lazy' ` for validation on form changes or ` 'onBlur' ` for validation on input blur ).
130136 - ** transformFn ** : (optional ) A transformation function that can be used when integrating a different validation library. It allows you to transform data before it is validated. Use this option only if you are integrating another validation library that requires specific data handling.
137+ - ** errorStrategy** : (optional ) Error format mode (` 'flatten' ` user.name has an error , ` 'deep' ` for { user : { name : ' name has an error' } }).
131138
132139#### Return Value
133140
@@ -139,9 +146,67 @@ Returns an object containing the following properties:
139146- ` isLoading ` : Reactive reference indicating if the form validation is in progress .
140147- ` errorCount ` : Reactive reference to the number of errors .
141148- ` clearErrors ` : Function to clear validation errors .
142- - ` getErrorMessage ` : Function to get the error message for a specific field .
149+ - ` getErrorMessage ` : Function to get the error message for a specific field . Supports nested paths like " user.name" .
150+ - ` errorPaths ` : Computed property that returns an array of all error paths , including nested ones (e .g ., [" email" , " user.name" ]).
143151- ` focusFirstErroredInput ` : Function to focus the first input with an error .
144- - ` focusInput ` : Function to focus a specific input .
152+ - ` focusInput ` : Function to focus a specific input . Supports nested paths like " user.name" .
153+ - ` cleanup ` : Function to manually clean up watchers , event listeners , and caches . This is automatically called when the component unmounts if used within a component context , but can be called manually when needed (e .g ., when using the composable outside of a component or for manual cleanup ).
154+
155+ ## Deep Strategy Example
156+
157+ When using the ` errorStrategy: 'deep' ` option , you can work with nested form structures and automatically get all error paths :
158+
159+ ` ` ` vue
160+ <script setup>
161+ import { ref } from 'vue'
162+ import { useFormValidation } from 'vue-use-form-validation'
163+ import * as z from 'zod'
164+
165+ const schema = z.object({
166+ user: z.object({
167+ name: z.string().min(1, 'Name is required'),
168+ }),
169+ email: z.string().email('Invalid email'),
170+ })
171+
172+ const form = ref({
173+ user: { name: '' },
174+ email: '',
175+ })
176+
177+ const {
178+ validate,
179+ hasError,
180+ errorPaths,
181+ getErrorMessage,
182+ focusInput,
183+ } = useFormValidation(schema, form, { errorStrategy: 'deep' })
184+
185+ async function onSubmit() {
186+ await validate()
187+ if (!isValid.value) {
188+ // errorPaths.value will contain ["user.name", "email"] when there are errors
189+ console.log('Error paths:', errorPaths.value)
190+ }
191+ }
192+ </script>
193+
194+ <template>
195+ <form @submit.prevent="onSubmit">
196+ <input v-model="form.user.name" name="user.name" placeholder="Name">
197+ <input v-model="form.email" name="email" placeholder="Email">
198+
199+ <!-- Display all errors with automatic deep path handling -->
200+ <div v-if="hasError">
201+ <div v-for="errorPath in errorPaths" :key="errorPath">
202+ <button @click="focusInput({ inputName: errorPath })">
203+ {{ getErrorMessage(errorPath) }}
204+ </button>
205+ </div>
206+ </div>
207+ </form>
208+ </template>
209+ ` ` `
145210
146211## Example
147212
0 commit comments