Skip to content
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

Problematic Zod schema #4413

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions packages/example/src/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,7 @@ export const Index: React.FC = () => {
{type: 'a' as const, a: {a: 'hi'}},
{type: 'b' as const, b: {b: 'hi'}},
],
discriminatedUnion: {type: 'auto'},
}}
/>
{/**
Expand Down
9 changes: 9 additions & 0 deletions packages/example/src/SchemaTest/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ export const schemaTestSchema = z.object({
}),
]),
),
discriminatedUnion: z.discriminatedUnion('type', [
z.object({
type: z.literal('auto'),
}),
z.object({
type: z.literal('fixed'),
value: z.number().min(1080),
}),
]),
});

export const schemaArrayTestSchema = z.array(z.number());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export const createZodValues = (
case zodRuntime.ZodFirstPartyTypeKind.ZodString:
return '';
case zodRuntime.ZodFirstPartyTypeKind.ZodNumber:
return 0;
return (
(def as z.ZodNumberDef).checks.find((c) => c.kind === 'min')?.value ?? 0
);
Comment on lines +20 to +22
Copy link
Author

Choose a reason for hiding this comment

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

@JonnyBurger This fixes this particular issue with the min() or gte(). Would it make sense to also cover other checks like gt() and positive(), multipleOf(), etc...?

And then would it make sense to also extend the same method to other types like string and BigInt for example?

case zodRuntime.ZodFirstPartyTypeKind.ZodBigInt:
return BigInt(0);
case zodRuntime.ZodFirstPartyTypeKind.ZodBoolean:
Expand Down
Loading