Skip to content

fix date string zod validation #355

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

Merged
merged 8 commits into from
Apr 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/schema/src/plugins/zod/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export default class Transformer {
} else if (inputType.type === 'Boolean') {
result.push(this.wrapWithZodValidators('z.boolean()', field, inputType));
} else if (inputType.type === 'DateTime') {
result.push(this.wrapWithZodValidators('z.date()', field, inputType));
result.push(this.wrapWithZodValidators('z.union([z.date(), z.string().datetime()])', field, inputType));
Copy link
Member

@ymc9 ymc9 Apr 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks for working on this @Xemah !

I tried it out, and there's a problem with the distribution of union over array:

image

image

Here the inferred type doesn't conform to Prisma filter's typing. The union needs to wrap around array. Maybe we should refactor wrapWithZodValidators and and pass in a list of validators so we can deal with the array distribution inside of it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, we have to do like if its a union, then use '.array()' for each of the rule inside union
Like if it's z.union([z.date(), z.string().datetime()]), it should do z.union([z.date().array(), z.string().datetime().array()])

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think so.

} else if (inputType.type === 'Json') {
this.hasJson = true;

Expand Down