Skip to content
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

Open
dwidge opened this issue May 2, 2024 · 1 comment
Open

Unknown type error for nullable() and nullish() #1330

dwidge opened this issue May 2, 2024 · 1 comment
Labels
Bridge: Zod Affects the uniforms-bridge-zod package

Comments

@dwidge
Copy link

dwidge commented May 2, 2024

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

import ZodBridge from "uniforms-bridge-zod";
import { AutoForm } from "uniforms-mui";
@dwidge
Copy link
Author

dwidge commented May 2, 2024

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>;
}

problem

The transform() also causes unknown type error, so we can't return null, only undefined.

@piotrpospiech piotrpospiech added the Bridge: Zod Affects the uniforms-bridge-zod package label May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bridge: Zod Affects the uniforms-bridge-zod package
Projects
Status: Needs triage
Development

No branches or pull requests

2 participants