default value #567
Replies: 4 comments 14 replies
-
More details here: https://valibot.dev/guides/optionals/#default-values |
Beta Was this translation helpful? Give feedback.
-
This is really hard to find and hampers overall adaption of Valibot. Spend several minutes going over the docs using search, and only the useless type defintion (https://valibot.dev/api/Default/) turned up, and I had to google and searching github for something that is a basic func. Please rethink adding a ![]() |
Beta Was this translation helpful? Give feedback.
-
@fabian-hiller one other rather unintuitive behavior I've found was that I have a required data, but I want to give it a default value to be used with getDefaults(). The problem is that the InferOutput will now add "myType | undefined", as it is marked as optional. You therefore has to wrap it around a nonOptional to remove the undefined. If adding a v.default() is not possible, would it be possible maybe to consider adding a second parameter to nonOptional, to set a value that will be used with |
Beta Was this translation helpful? Give feedback.
-
Hello @fabian-hiller , I'm sorry for pinging you again on this, but the more I use this API (optional) the more I feel it is an incorrect abstraction. In some cases, I have a schema where a value is required (in the sense that I must receive a value complying with the schema), but where I still need a default value (the To use this schema, this ultimately translate as this: const Schema = v.object({
required: v.pipe(v.optional(v.string(), ''), v.email()),
optional: v.optional(v.union([v.literal(''), v.pipe(v.string(), v.email())]), ''),
}); What confuses me a lot is how both actually have something called "optional" while, in the first case, it is actually not optional. There is something conceptually wrong in the naming that consistently make me think twice in the order I am doing thing (and when I have to constantly think about what I'm doing, it often means that the API is incorrect somewhere :D). Something that is not optional should not have something called optional, in my opinion. This looks much more logical and remove confusion: this shows at first glance that the required field is not optional: const Schema = v.object({
required: v.default(v.pipe(v.string(), v.email()), ''),
optional: v.optional(v.union([v.literal(''), v.pipe(v.string(), v.email())]), ''),
}); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm leaving this as if someone is also looking for how to set the default value, and under the keyword "default value valibot" nothing else would pop up for them. It's done with
v.optional
. Finding this took me significantly longer than it should have.Beta Was this translation helpful? Give feedback.
All reactions