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
Unknown type error for nullable() and nullish() #1330
Labels
Bridge: Zod
Affects the uniforms-bridge-zod package
Comments
workaround// ChatGPT
import z from "zod";
// Convert zod nullable types to optional types
export function convertNullableToOptional<T extends z.ZodRawShape>(
schema: z.ZodObject<T>
): z.ZodObject<T> {
const newShape: any = {};
for (const key in schema.shape) {
const field = schema.shape[key];
if (field instanceof z.ZodNullable) {
newShape[key] = field.unwrap();
if (!(newShape[key] instanceof z.ZodArray))
newShape[key] = newShape[key].optional();
// .transform((x: any) => x ?? null);
} else if (
field instanceof z.ZodOptional &&
field.unwrap() instanceof z.ZodNullable
) {
newShape[key] = field.unwrap().unwrap();
if (!(newShape[key] instanceof z.ZodArray))
newShape[key] = newShape[key].optional();
// .transform((x: any) => x ?? null);
} else if (
field instanceof z.ZodOptional &&
field.unwrap() instanceof z.ZodArray
) {
newShape[key] = field.unwrap();
} else if (field instanceof z.ZodObject) {
newShape[key] = convertNullableToOptional(field);
} else {
newShape[key] = field;
}
}
return z.object(newShape) as z.ZodObject<T>;
} problemThe transform() also causes unknown type error, so we can't return null, only undefined. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
problem
When we have a nullable() or nullish() zod field it throws 'unknown type'.
We can't use our main zod types for uniforms directly because we use null fields in the mysql database and endpoints. We use both types: undefined to mean unchanged or unknown, and null to mean the field is empty.
Maybe treat nullable the same as optional in the forms? Set the field to null if not entered by user?
workaround
Create separate zod types for uniforms with optional() instead of nullish() or nullable(). Replace undefined fields with null in the onSubmit handler. Keep them updated with the main schema.
packages
The text was updated successfully, but these errors were encountered: