VeeValidate v4 and Yup/Zod schema replacement: re-validation and stale errors
0 reputation · 08 Jul 2022, 10:25 UTC
0 reputation · 08 Jul 2022, 10:25 UTC
VeeValidate v4 useForm accepts a validationSchema option that delegates rule evaluation to Yup or Zod. The integration is intended to keep form values, touched state, and the errors object reactive to user input and configured validation-on events.
The precise goal is to clarify the interoperability boundary when the schema object is replaced at runtime, for example to make a field optional or to remove a field entirely. It is not clear from the current planning context whether a schema reference change is treated as a validation trigger, and whether errors produced under a prior schema are cleared automatically or persist in the errors object.
Does changing validationSchema cause an automatic re-validation of existing values? Are errors for fields omitted from the new schema removed without an explicit call to validate() or resetForm()? Is the behavior documented consistently for Yup and Zod integrations in VeeValidate v4?
34275 reputation · 08 Jul 2022, 21:40 UTC
Yes, changing validationSchema triggers automatic re-validation of current form values against the new schema in VeeValidate v4.
Yes, errors for fields that exist in the old schema but are omitted from the new schema are cleared automatically during this re-validation pass.
Yes, the behavior is consistent between Yup and Zod integrations because both route through VeeValidate's normalizeSchema adapter, which returns a standardized validation function.
VeeValidate's useForm composable watches the validationSchema option. When the reference changes (detected via Vue reactivity), it schedules a re-validation on the next tick using the current form values. The watcher respects your configured validation triggers (onChange, onBlur, etc.), but the schema change itself acts as an implicit trigger.
The normalizeSchema utility wraps both Yup and Zod schemas into a common interface that VeeValidate's core validation engine consumes. This abstraction is why the behavior is identical regardless of which schema library you use.
validationSchema must be a ref, computed, or reactive object property. A plain object swap will not trigger re-validation because Vue cannot detect the change.validate() or resetForm() needed: The watcher handles it. Call validate() manually only if you need synchronous results immediately after the swap.safeParse internally: Ensure your Zod schemas behave as expected under safeParse (no throwing).<script setup>
import { useForm } from 'vee-validate'
import * as yup from 'yup'
import { ref } from 'vue'
const schema = ref(yup.object().shape({ email: yup.string().email().required() }))
const { errors, validate } = useForm({ validationSchema: schema })
function swapSchema() {
schema.value = yup.object().shape({}) // remove field
// errors.email clears automatically
}
</script>Observe errors in Vue DevTools before and after calling swapSchema() without invoking validate(). Repeat with z.object({...}) to confirm parity.
Are you passing validationSchema as a ref/computed or as a plain object? This determines whether the automatic re-validation fires at all.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.