-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validate multiple fields against each other using Zod #116
Comments
Hey! Do you have an example of this failing even with casting? As far as I can see, the API we use internally is the same (for it to have a |
I've dug a bit into this and while I did manage to recreate the issue, I'm starting to think that this is a zod thing not a felte thing. Even after reading through the zod docs, I can't seem to tell if this is how zod works or if it's a bug. The issue:The validation that compares A workaround would be to always provide <script lang="ts">
import reporter from '@felte/reporter-tippy';
import { validateSchema } from '@felte/validator-zod';
import { createForm } from 'felte';
import 'tippy.js/dist/tippy.css';
import { z } from 'zod';
const schema = z
.object({
price: z.number(),
quantity: z.number(),
start: z.preprocess((arg) => {
if (typeof arg === 'string' || arg instanceof Date) return new Date(arg);
}, z.date()),
end: z.preprocess((arg) => {
if (typeof arg === 'string' || arg instanceof Date) return new Date(arg);
}, z.date())
})
.refine((val) => val.start < val.end, {
path: ['start'],
message: 'Start date must be before end date'
})
.refine((val) => val.start < val.end, {
path: ['end'],
message: 'End date must be after start date'
});
const { form } = createForm({
initialValues: {
start: '2022-01-15',
end: '2022-01-13'
},
extend: reporter(),
validate: validateSchema(schema as unknown as z.AnyZodObject)
});
</script>
<form use:form>
<input type="number" name="price" />
<input type="number" name="quantity" />
<input type="date" name="start" />
<input type="date" name="end" />
<button type="submit">Submit</button>
</form> |
I mistook Zod with Yup here so this is not the reason the issue is happening. But I see your comment below, so there's nothing to do besides using |
I should've done a better job searching. It seems like this is how zod works. There are a few workarounds in that thread as well. |
Thanks for the issue regardless! I did fix the type of the expected schema, so you shouldn't need to cast the value on the latest version. |
@ambiguous48 what worked for you? |
It's common to validate fields based on other fields. For example, a form with a
start date
andend date
.The way to do this with Zod (that I know of) is like the snippet below. However, typescript will complain because validator will only accept a schema of type
ZodObject
, whereas here the schema is of typeZodEffects
.I've had some success with manually casting like below. It doesn't seem to work all the time though. Some of my forms fail to validate properly even when doing this. Not sure why yet.
The text was updated successfully, but these errors were encountered: