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

[ZOD] Implement logic to handle Zod.discriminatedUnion #1292

Open
dj-fiorex opened this issue Oct 28, 2023 · 1 comment
Open

[ZOD] Implement logic to handle Zod.discriminatedUnion #1292

dj-fiorex opened this issue Oct 28, 2023 · 1 comment
Assignees
Labels
Area: Bridge Affects some of the bridge packages Bridge: Zod Affects the uniforms-bridge-zod package Type: Feature New features and feature requests
Milestone

Comments

@dj-fiorex
Copy link

Hel everyone, i'm willig to use Zod.discriminatedUnion to create an object that based on the value of a property has a different shape. i tried with this simple example:

const columnSchema = z.discriminatedUnion("columnType", [
  z.object({
    columnType: z.literal("slider"),
    columnContent: z.object({
      title: z.string(),
      imageUrl: z.string(),
    }),
  }),
  z.object({
    columnType: z.literal("image"),
    columnContent: z.object({
      title: z.string(),
      description: z.string(),
      link: z.string(),
      imageUrl: z.string(),
    }),
  }),
]);

 const schema = z.object({
  invertColumns: z.boolean(),
  settings: z.object({
    desktop: z.object({ ratio: z.string() }),
    mobile: z.object({ ratio: z.string() }),
  }),
  column1: columnSchema,
  column2: columnSchema,
});


but i received an error:

Uncaught Invariant Violation: Field "column1" has an unknown type
    at invariant (http://localhost:8282/dist/vendors-node_modules_digitalrock_skin-styles-utils_dist_Container_js-node_modules_digitalrock-9a1109.bundle.js:86287:15)
    at ZodBridge.getType (http://localhost:8282/dist/vendors-node_modules_digitalrock_skin-styles-utils_dist_Container_js-node_modules_digitalrock-9a1109.bundle.js:104862:57)
    at ZodBridge.memoized [as getType] (http://localhost:8282/dist/vendors-node_modules_digitalrock_skin-styles-utils_dist_Container_js-node_modules_digitalrock-9a1109.bundle.js:97861:23)
    at useField (http://localhost:8282/dist/vendors-node_modules_digitalrock_skin-styles-utils_dist_Container_js-node_modules_digitalrock-9a1109.bundle.js:106747:38)
    at AutoField (http://localhost:8282/dist/vendors-node_modules_digitalrock_skin-styles-utils_dist_Container_js-node_modules_digitalrock-9a1109.bundle.js:106495:86)
    at renderWithHooks (http://localhost:8282/dist/main.bundle.js:59943:18)
    at mountIndeterminateComponent (http://localhost:8282/dist/main.bundle.js:62769:13)
    at beginWork (http://localhost:8282/dist/main.bundle.js:64007:16)
    at HTMLUnknownElement.callCallback (http://localhost:8282/dist/main.bundle.js:48903:14)
    at Object.invokeGuardedCallbackDev (http://localhost:8282/dist/main.bundle.js:48952:16)
invariant @ browser.js:38
getType @ ZodBridge.js:196
memoized @ memoize.js:62
useField @ useField.js:21
AutoField @ createAutoField.js:11
renderWithHooks @ react-dom.development.js:14985

I think that the error come from getType of the ZodBridge because there isn't an implementation of discriminatedUnion

Is there any plan on supporting this? i think i could have some time to work on this if you think it could help others also.
Or if you have a different implementation for my use case please let me know!

Thanks

@kestarumper kestarumper added Type: Feature New features and feature requests Area: Bridge Affects some of the bridge packages Bridge: Zod Affects the uniforms-bridge-zod package labels Nov 3, 2023
@Monteth
Copy link
Member

Monteth commented Nov 3, 2023

Hello @dj-fiorex,
Thank you for pointing it out - currently, we don't have support for it in the Zod schema. There is something very similar in JSON schema: https://uniforms.tools/docs/api-bridges/#note-on-allofanyofoneof, but it doesn't work in all cases.
I think it is an important feature, and we want it to be supported, but we didn't have it on the board until now.

We would love to receive help from the community, so if you find the time, we encourage you to create a PR with this change. Remember that we can always help if you encounter any difficulties.

@kestarumper kestarumper added this to the v4.x milestone Nov 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Bridge Affects some of the bridge packages Bridge: Zod Affects the uniforms-bridge-zod package Type: Feature New features and feature requests
Projects
Status: Concept
Development

No branches or pull requests

3 participants