How to handle checkbox? #254
Answered
by
fabian-hiller
jdgamble555
asked this question in
Q&A
-
I have a checkbox How can I handle this with valibot? I need to validate a boolean value. const TaskSchema = object({
name: string(),
completed: boolean()
});
const result = safeParse(TaskSchema, Object.fromEntries(formData));
... J |
Beta Was this translation helpful? Give feedback.
Answered by
fabian-hiller
Nov 19, 2023
Replies: 2 comments 7 replies
-
I recommend to use |
Beta Was this translation helpful? Give feedback.
7 replies
Answer selected by
fabian-hiller
-
another option is rewriting zfd's checkbox type CheckboxOpts = {
trueValue?: string;
};
export const checkbox = ({ trueValue = "on" }: CheckboxOpts = {}) =>
v.union([
v.pipe(v.literal(trueValue), v.transform(() => true)),
v.pipe(v.undefined(), v.transform(() => false))
]); or you could allow export const checkbox = ({ trueValue = "on" }: CheckboxOpts = {}) =>
v.union([
v.boolean(),
v.pipe(v.literal(trueValue), v.transform(() => true)),
v.pipe(v.undefined(), v.transform(() => false))
]); here's a playground or even more general |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I recommend to use
decode-formdata
for that: https://github.com/fabian-hiller/decode-formdata