-
Notifications
You must be signed in to change notification settings - Fork 15
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
check using enum does not validate #2
Comments
@hyusetiawan Can you provide the EDIT: Also, please provide the zod and formik versions that you're using |
the versions: |
@hyusetiawan I've made a small example here: import { z } from 'zod';
import { Formik } from 'formik';
import { toFormikValidationSchema } from 'zod-formik-adapter';
export enum OCCUPANT {
OWNER = 'owner',
RENTER = 'renter',
}
const NonOccupantType = z.object({
product: z.enum(['Ho3', 'Dp3']),
occupantType: z.null(),
});
const RequireOccupantType = z.object({
product: z.literal('Ho6'),
occupantType: z.nativeEnum(OCCUPANT),
});
const validationSchema = z.union([NonOccupantType, RequireOccupantType]);
function ExampleForm() {
return (
<Formik
validationSchema={toFormikValidationSchema(validationSchema)}
initialValues={{
product: "",
occupantType: "",
}}
onSubmit={console.log}
validateOnMount={true}
>
{({ handleSubmit, handleChange, isValid, handleBlur, errors, touched, values }) => (
<form onSubmit={(e) => {
e.preventDefault();
handleSubmit(e);
}}>
<div>
<span>product</span>
<input
value={values.product}
onChange={handleChange("product")}
onBlur={handleBlur("product")}
/>
{
touched.product &&
errors.product &&
<strong>Error: {errors.product}</strong>
}
</div>
<div>
<span>occupantType</span>
<input
value={values.occupantType}
onChange={handleChange("occupantType")}
onBlur={handleBlur("occupantType")}
/>
{
touched.occupantType &&
errors.occupantType &&
<strong>Error: {errors.occupantType}</strong>
}
</div>
<button disabled={!isValid} type="submit">Submit</button>
</form>
)}
</Formik>
);
} Debugging, I've found out that an error from a plain ZodSchema has this shape [
{
"code": "invalid_enum_value",
"options": [
"owner",
"renter"
],
"path": [
"occupantType"
],
"message": "Invalid enum value. Expected 'owner' | 'renter', received ''"
}
] But a union, has a different shape of error [
{
"code": "invalid_union",
"unionErrors": [
{
"issues": [
{
"code": "invalid_enum_value",
"options": [
"Ho3",
"Dp3"
],
"path": [
"product"
],
"message": "Invalid enum value. Expected 'Ho3' | 'Dp3', received 'Ho6'"
},
{
"code": "invalid_type",
"expected": "null",
"received": "string",
"path": [
"occupantType"
],
"message": "Expected null, received string"
}
],
"name": "ZodError"
},
{
"issues": [
{
"code": "invalid_enum_value",
"options": [
"owner",
"renter"
],
"path": [
"occupantType"
],
"message": "Invalid enum value. Expected 'owner' | 'renter', received ''"
}
],
"name": "ZodError"
}
],
"path": [],
"message": "Invalid input"
}
] This shape is not mapped by this lib. I'll attempt to make a fix for this later on, but feel free to open a PR to comply with this fix. I'll add a message to the README regarding this, so people are aware to not use zod unions for a while. EDIT: The main problem are the multiple |
Managed to make this work by using https://codesandbox.io/s/headless-night-tlyz19?file=/src/App.tsx |
@amatiasq-heydoc Thanks so much for the PR! I hope it's accepted soon.
I also wondered this -- I found one Formik form in a repo I maintain that uses both I guess the takeaway is that maybe folks have additional validation outside of zod? It's not hard to compose that with zod, but I imagine an adapter function with usage like |
Interesting, we can extend |
I can move forward the proposed PR, but wanted to know if the OP @hyusetiawan also believes that the proposed solution fixes the problem |
I have moved on from this project, unfortunately, but it looks like you are able to replicate on your end here: |
any updates? |
I made It builds on some of the solution proposed in #10 so only exposes a function for use with the Feel free to give it a try and I hope it helps! |
Appreciate it, it does help :) |
robertLichtnow Robert, can you move forward with the PR? |
Closing this since the proposed solution was merged and deployed at v1.2.0 |
@robertLichtnow
|
given the following schema:
validating using formik the following object passes (the onSubmit gets called):
{product: "Ho6", occupantType: ""}
and passing that same object using
.parse()
correctly throws on the enum value validationThe text was updated successfully, but these errors were encountered: