logaretm / vee-validate

✅ Painless Vue forms
https://vee-validate.logaretm.com/v4
MIT License
10.84k stars 1.27k forks source link

Stricter typing for validationSchema #4928

Open shunjilin opened 3 weeks ago

shunjilin commented 3 weeks ago

Description:

I’m not sure if this issue has already been addressed or if it’s a limitation, so I apologize if this is redundant.

Currently, the validationSchema accepts any as its type, which allows any value to be assigned. In the example below:

type Form = {
  a: string;
};

useForm<{ a: string }>({
  validationSchema: "", // accepts any type
});

Solution

Would it be possible to narrow down the type of validationSchema to at least one of the following:

For reference, see docs.

Context:

I’m raising this because we had a case where we changed the Form type, but forgot to update the validationSchema. This led to an invalid property being set as required, causing the form to fail submission. Having stronger type safety for validationSchema would help prevent such issues.

Alternatives

A straightforward solution to this would be to use the typedSchema from yup or zod, but since we are already using another validation library effect-ts, we would like to avoid introducing an additional dependency.

I’d be happy to contribute if this approach seems suitable. Thanks!

logaretm commented 3 weeks ago

You raise a very good issue here. The problem I think was that we supported Yup by default, which caused the typed schema type inference to short circuit most of the time or resolve to incorrect types all together. So I handicapped the resolution.

if you want to try to give it another go by limiting the types go a head, But it would be a matter of seeing if this qualifies as a breaking change or not. If it is then maybe we should consider this for v5 with more aggressive changes like dropping yup built-in support.

shunjilin commented 2 weeks ago

Thank you for the explanation! Understand the concern about this being a breaking change, will take a quick look to see if this can be mitigated, e.g. with an explicit typed flag