-
Notifications
You must be signed in to change notification settings - Fork 16
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
Support ZodIntersection in useFormInputProps
, ZodPipeline in getInputProps
#36
base: main
Are you sure you want to change the base?
Conversation
New dependency changes detected. Learn more about Socket for GitHub ↗︎ 👍 No new dependency issues detected in pull request Bot CommandsTo ignore an alert, reply with a comment starting with Pull request alert summary
📊 Modified Dependency Overview:
|
07ae9f6
to
5d84562
Compare
- the inferred shape of ParamsSchema is not a valid ZodType - the generic on `getParams` is already correctly inferred from the schema
5d84562
to
99e4b44
Compare
} | ||
|
||
let inputProps: InputPropType = { | ||
name, | ||
type, | ||
} | ||
if (!def.isOptional()) inputProps.required = true | ||
if (!(def.isOptional() || optional)) inputProps.required = true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "optionality" of a type seemed to get lost when introspecting its inner type (which I think explains the discrepancy in the tests where we're asserting on the generated props for the optional d
field from the base test schema but the assertion includes required: true
).
@@ -284,7 +284,7 @@ describe('test useFormInputProps', () => { | |||
name: 'c', | |||
required: true, | |||
}) | |||
expect(inputProps('d')).toEqual({ type: 'text', name: 'd', required: true }) | |||
expect(inputProps('d')).toEqual({ type: 'text', name: 'd' }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
d
is defined as z.string().optional()
, so it should not be required.
Hey @solomonhawk, |
@lildesert I don't have any updates here - the PR is offered up for @kiliman to consider. I don't know if they are actively working on |
I'm sorry for not getting back to you sooner. I haven't used this package much lately. I have my own fork essentially with a different API. I really should get back to this and clean it up. Also, if anyone would like to maintain this going forward, that would be great! |
It's all good. I know how it goes :) I don't have time to contribute more unfortunately. I do think it's a worthwhile pursuit to have a way to derive input props from a schema definition (the main thing I used this package for) as it reduces duplication and helps prevent a divergence between the validation rules and the form impl. It's a little disappointing that building on top of |
Fixes #34
Fixes #35
NOTE: This bumps the peer dep on
zod
to3.20.0
forZodIntersection
support.useFormInputProps
that handles the case where the passed-inZodType
is something other than aZodObject
(e.g.ZodIntersection
/ZodEffects
)getInputProps
to handle bothZodEffects
(recurse into inner schema) andZodPipeline
(infer input type from the pipeline'sout
type)getParams
in example app - these were yielding type errors and didn't seem to be necessary (type is inferred from schema)ZodOptional
- see 8121879@kiliman I'm not 100% confident in these changes so a close eye would be appreciated. Per my use case and the tests it seems to address the need. I would welcome additional test cases to consider.
I don't have a good example use case for when a field is itself a
ZodIntersection
e.g.:I cleaned up a few unused variables in the process of making these changes.
I thought about support for
z.union([...schemas])
and found it difficult to determine what the correct behavior should be, given that unions modelOR
types, which could lead to confusing situations where an input's props would need to be e.g.text
ornumber
.In case anyone is curious, I found intersections to be crucial when modeling schemas with refinements on the base type (
.superRefine
calls that need multiple fields in order to layer on additional cross-field validation constraints). Without them, a failed parse on any of the schema fields prevents the refinement from even running which means you only get some of the errors.More detailed code sample
Not great, dates constraint violation isn't caught when base schema isn't successfully parsed
Better, intersection allows refinements to produce errors even when other portions of the schema are invalid