Revamp schema coercion #1152
Replies: 2 comments 10 replies
-
|
That does sounds like a bug. I think if we make sure it returns the base zod type instead of the exact schema type, then it will warn you if you try to access
Internalizing This also complicate the story in the client. How should we tell useForm how to coerce the schema? |
Beta Was this translation helpful? Give feedback.
-
|
If the main problem here is just about accessing |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Problem
The definition of
coerceFormValueasserts an explicit return type to make it identical with the input schema type. This makes it seem like a transparent wrapper to TypeScript, but the returned schema's type doesn't actually match up. This makes wrapped schemas inflexible and unfortunately brittle at runtime.Here's how they currently differ (assuming zod v4):
Workarounds
If an unsupported property like
.shapeis needed, you need to create bindings for the zod schema as well as the coerced zod schema and be mindful about naming and access:This creates cognitive overhead, since the same schema not only comes in different variations, but the variations are of different actual types, which the type system does not reveal.
Solutions
coerceFormValuemight be able to return aProxyaround the input schema. This way, Conform doesn't break as soon as validation libraries add a new property onto their schemas.Preferrably internalize
coerceFormValueand remove it from the public interface. Coerced schema definitions floating around in application code might be completely avoidable, since they are only used in two Conform-specific places anyway:useFormparseSubmissionWould an abstraction along these lines be helpful?
This might also allow proper type inference here:
Beta Was this translation helpful? Give feedback.
All reactions