Zod type error
Answered
Rhinelander posted this in #help-forum
RhinelanderOP
I get this error when using Zod.
Code
Error
If I try infering types from the usage it also throws error as too little arguments were provided.
Code
.refine((value, context) => {
const school = context?.parent?.school;
if (school === "ELEMENTARY") return value <= 9;
if (school === "HIGH_SCHOOL" || school === "FACULTY") return value <= 4;
return true;
}, "Vrednost razreda ni ustrezna za izbrano šolo"),Error
No overload matches this call.
Overload 1 of 2, '(check: (arg: number) => arg is any, message?: string | Partial<Omit<ZodCustomIssue, "code">> | ((arg: number) => Partial<Omit<ZodCustomIssue, "code">>) | undefined): ZodEffects<...>', gave the following error.
Argument of type '(value: any, context: any) => boolean' is not assignable to parameter of type '(arg: number) => arg is any'.
Target signature provides too few arguments. Expected 2 or more, but got 1.
Overload 2 of 2, '(check: (arg: number) => unknown, message?: string | Partial<Omit<ZodCustomIssue, "code">> | ((arg: number) => Partial<Omit<ZodCustomIssue, "code">>) | undefined): ZodEffects<...>', gave the following error.
Argument of type '(value: any, context: any) => boolean' is not assignable to parameter of type '(arg: number) => unknown'.
Target signature provides too few arguments. Expected 2 or more, but got 1.ts(2769)
Parameter 'context' implicitly has an 'any' type.ts(7006)If I try infering types from the usage it also throws error as too little arguments were provided.
Answered by Velvet ant
maybe
zod.coerce can help here https://zod.dev/?id=coercion-for-primitives4 Replies
Velvet ant
https://zod.dev/?id=arguments
checking docs:
checking docs:
As you can see, .refine takes two arguments.
The first is the validation function. This function takes one input (of type T — the inferred type of the schema) and returns any. Any truthy value will pass validation. (Prior to zod@1.6.2 the validation function had to return a boolean.)
The second argument accepts some options. You can use this to customize certain error-handling behavior:.superRefine has two arguments so maybe you're confusing bothRhinelanderOP
Here is one more question.
How do i handle input type number with zod as it returns string.
This works (but not best solution)
but i loose stuff like .positive(), .min(), .max() unless i define .refine everytime and manually check for everything.
I use shadcn and my input looks like this
I also have undesired effect when i try to write in a number 10 i cant i get 010
How do i handle input type number with zod as it returns string.
This works (but not best solution)
.refine((val) => !Number.isNaN(parseInt(val, 10)),
{message: "Got string, expected number"}) but i loose stuff like .positive(), .min(), .max() unless i define .refine everytime and manually check for everything.
I use shadcn and my input looks like this
<FormControl>
<Input
type="text"
max={100}
min={1}
placeholder="Type in your age"
{...field}
value={Number(field.value)}
/>
</FormControl>I also have undesired effect when i try to write in a number 10 i cant i get 010
Velvet ant
maybe
zod.coerce can help here https://zod.dev/?id=coercion-for-primitivesAnswer