default value as output with pipe? #657
-
import * as v from 'valibot';
const Schema = v.optional(v.pipe(v.string(), v.transform(parseInt)), '0');
const result = v.safeParse(Schema, undefined);
console.log(result); Current 'default value' implementation uses the provided default value as 'input' to the wrapped schema. However I want to provide default value as 'output' of wrapped schema so it skips the pipe and I can provide the desired default output value directly like so: const Schema = v.optional(v.pipe(v.string(), v.transform(parseInt)), 0); // number 0 instead of string '0' How should I implement this? |
Beta Was this translation helpful? Give feedback.
Answered by
fabian-hiller
Jun 16, 2024
Replies: 1 comment 1 reply
-
I would rewrite your schema. See this playground. import * as v from 'valibot';
const Schema = v.union([
v.pipe(v.string(), v.transform(parseInt)),
v.optional(v.number(), 0),
]); Another option is to use the import * as v from 'valibot';
const Schema = v.fallback(v.pipe(v.string(), v.transform(parseInt)), 0); |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
fabian-hiller
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would rewrite your schema. See this playground.
Another option is to use the
fallback
method instead of theoptional
schema, but the behavior is different. See this playground.