Temporary coerce #455
-
Is there a way to temporarily transform or coerce an input, run it through some verifications, and return the original input? For example, create a schema that accepts a number, that passes some number-specific validations, but that can also accept a string representation of the number, and pass it through the same validations, coerced as a number. Then output the original input value, in this example either a string or a number. Right now the coerce function will irrevocably change the type. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Something like that could work. Check it out in our playground. const Schema = v.union(
[v.string([v.regex(/^-?\d+(?:\.\d+)?$/)]), v.number()],
[
v.custom((input) => Number(input) >= 0, 'Number is too small'),
v.custom((input) => Number(input) <= 100, 'Number is too big'),
]
); |
Beta Was this translation helpful? Give feedback.
Something like that could work. Check it out in our playground.