Replies: 1 comment 12 replies
-
|
Would this work for you? Let me know if you have any questions. const heightSchema = z.object( {
value: z.number(),
min: z.number().optional(),
max: z.number().optional(),
} ).superRefine( ( input, ctx ) => {
const { value, min, max } = input
if ( min != null && value < min ) {
ctx.addIssue( {
code: z.ZodIssueCode.too_small,
minimum: min,
type: 'number',
inclusive: true,
message: `Height must be greater than ${ min }`,
} )
}
if ( max != null && value > max ) {
ctx.addIssue( {
code: z.ZodIssueCode.too_big,
maximum: max,
type: 'number',
inclusive: true,
message: `Height must be less than ${ max }`,
} )
}
} )
console.log( heightSchema.safeParse( { value: 1 } ).success ) // true
console.log( heightSchema.safeParse( { value: 1, min: 1, max: 10 } ).success ) // true
console.log( heightSchema.safeParse( { value: 1, min: 2 } ).success ) // false
console.log( heightSchema.safeParse( { value: 11, max: 10 } ).success ) // false
console.log( heightSchema.safeParse( { value: 1, min: 2, max: 10 } ).success ) // false
console.log( heightSchema.safeParse( { value: 11, min: 2, max: 10 } ).success ) // false |
Beta Was this translation helpful? Give feedback.
12 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I've been evaluating
zodas an alternative toyupwhich is the validation library we currently use.The type for my form state contains a property
height, such as this (other properties ofFormStatehave been omitted for brevity):height.min.valuehas some validation rules, but some of these rules are dependent on other properties, but I'm not sure of what the recommended approach for implementing them withzod.if
height.min.isAppliedis true, we want to apply anumber().min().max(), other we just want to applynumber()yup, I achieve this using thewhen()method, which allows referencing the value of a sibling fieldzod, there doesn't seem to be an equivalent ofwhen(), and it's not clear how i could utilise any of the existing schema methods to achieve this behaviour. Maybe usingunion()somehow?if
height.min.isAppliedis true ANDheight.max.isAppliedis true, we want to apply an additional constraint that checks thatheight.min.valueis less thanheight.max.valueyup, I would acheive this using thetest()method, which allows you to construct a custom validation check via a callback that has access to parent properties via thefromarray property of thectxparameter of the callbackzod, there's arefine()andsuperRefine()method for doing custom validation checks, but thectxparameter of thesuperRefine()callback doesn't seem to give you access to the object tree likeyupdoes, so I don't know how you'd referenceheight.max.isAppliedandheight.max.valuefrom the schema applied toheight.min.valueOverall I'm really liking the library, especially it's leverage of TypeScript functionality such as with discriminated unions in
ZodIssue.If anyone could suggest how
zodcan be used to solve the above problems, I don't think I'd have any problems recommending it overyup.Beta Was this translation helpful? Give feedback.
All reactions