vazco / uniforms

A React library for building forms from any schema.
https://uniforms.tools
MIT License
1.94k stars 239 forks source link

ZodBridge should ignore literal property type #1262

Closed kziemski closed 1 year ago

kziemski commented 1 year ago

crashing in getType for instance const PersonSchema = z.object({ __typename: z.literal("Person").optional(), name: z.string().min(1) }) const schema = new ZodBridge(PersonSchema) ...

Crashes and throws Field "__typename" has an unknown type.

radekmie commented 1 year ago

Hi @kziemski! Thanks for reporting this. The thing is, it's not necessarily true that these properties should be ignored - I can imagine a case, where they are rendered as read-only fields instead.

To work round that, it'd be easiest to build it into the bridge. You could subclass the ZodBridge and change the getSubfields method to exclude such fields.

Let me know how it went!

kziemski commented 1 year ago

Agree it would be better if it could also be rendered as a readonly.
Assuming it could be treated as a bug

Is there anyway to ignore fields and subfields in which ZodBridge cannot understand type as opposed to crashing?

radekmie commented 1 year ago

Rendering it as read-only out-of-the-box is a problem on its own, as z.literal accepts null and undefined. Also, not all themes support read-only fields.

Is there anyway to ignore fields and subfields in which ZodBridge cannot understand type as opposed to crashing?

Not in a "if crash then ignore" way, as if it reaches the getType method, rendering already started. As I said above, such fields could be ignored in getSubfields.

radekmie commented 1 year ago

No response so far, so I'm closing. Feel free to comment further, though!