Skip to content

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

@dj-fiorex

Description

@dj-fiorex

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

Metadata

Metadata

Assignees

Labels

Area: BridgeAffects some of the bridge packagesBridge: ZodAffects the uniforms-bridge-zod packageType: FeatureNew features and feature requests

Type

No type
No fields configured for issues without a type.

Projects

Status
Concept

Relationships

None yet

Development

No branches or pull requests

Issue actions