-
-
Notifications
You must be signed in to change notification settings - Fork 163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Getting concrete errors in unions is too hard #216
Comments
The reason I suspect the problem is using the union type is because
|
The problem with For example, if your const UnionSchema = union(
[string('Please enter a string.'), number('Please enter a number.')],
'Please enter a string or number.'
); However, I am aware that this is not optimal for objects. So I am interested in improving this. Do you have any ideas? Do you know how this works with other schema libraries? Feel free to create minimal code examples with your desired API. |
Yes, I see your point, and makes complete sense, I was thinking only in my particular use-case. sum('type', // This is the tag, all members must have it
[object({type: 'A'}), object({type: 'B', something: string()}], // Sum of schemas
'X does not match any valid input' // Fallback message
) |
Thank you for your answer! Yes, we need a solution like this. Seems similar to Zod's |
Yes, here is another example: https://github.com/gcanti/io-ts/blob/master/Decoder.md#the-sum-combinator For me the critical part is extracting the proper errors, just in case I was not clear about that |
Is this related related: This error is thrown only when client-side validation fails, if client-side validation passes then everything works properly. Context: I'm using it with Qwik + ModularForms + ValiBot + Union My Union implementation: const RegisterSchema = union([
object({
type: literal("send-otp"),
phone: string([
minLength(1, 'Please enter your phone number.'),
regex(/^\+?(?:[0-9] ?){6,14}[0-9]$/, 'Please enter a valid phone number.'),
]),
id: optional(string([
regex(/^[0-9]+$/, 'Illegal state, please try again.'),
])),
}),
object({
type: literal("register"),
id: string([
regex(/^[0-9]+$/, 'Illegal state, please try again.'),
]),
code: string([
custom(
value => !!value.toString().match(/^[0-9]{6}$/),
'Please enter a valid OTP.'
),
]),
phone: string([
minLength(1, 'Please enter your phone number.'),
regex(/^\+?(?:[0-9] ?){6,14}[0-9]$/, 'Please enter a valid phone number.'),
]),
email: optional(string([
regex(/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/, 'Please enter a valid email address.'),
])),
name: string([
minLength(1, 'Please enter your name.'),
minLength(3, 'Please enter a valid name.'),
]),
}),
]); |
No. According to docs path is optional: "path?: PathItem[];"
https://valibot.dev/guides/errors/#:~:text=//%20Optional%20info-,path%3F%3A%20PathItem%5B%5D%3B,-issues%3F%3A
I recommend you turn strict mode in your typescript
El sáb, 21 oct 2023, 14:43, Riajul Islam ***@***.***>
escribió:
… Is this related related:
Cannot read properties of undefined (reading 'map')
[image: image]
<https://user-images.githubusercontent.com/24244379/277107725-abb130b3-bec4-4e17-ac60-813f3f9f29ac.png>
[image: image]
<https://user-images.githubusercontent.com/24244379/277108614-8a3b004d-7ab4-40a4-b675-079eba6b303f.png>
—
Reply to this email directly, view it on GitHub
<#216 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AARKJWJANLTOGAOWQNWBHLTYAO7PVAVCNFSM6AAAAAA6H4LC2KVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONZTG43TSMBUG4>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
It is partially related. With Modular Forms, I assumed that an |
I already have a strict mode on in my tsconfig |
Is there any plan to develop |
Yes, I'm in the process of weighing the options right now. I find a const Schema = union(
[
object({ type: literal('a'), foo: number() }),
object({ type: literal('b'), bar: boolean() }),
],
(input, [a, b]) => (input?.type === 'a' ? a : b)
); The biggest problem here is that My current assumption is to add |
I have implemented |
Awesome, thanks for the update!
…On Mon, Oct 23, 2023 at 9:49 PM Fabian Hiller ***@***.***> wrote:
I have implemented discriminatedUnion. The API is expected to be
available in the next release in a few days.
—
Reply to this email directly, view it on GitHub
<#216 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AARKJWPXUXNAEUEAPR7ZDETYA3C6BAVCNFSM6AAAAAA6H4LC2KVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONZVHEYTKMBUGM>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
--
---
https://danielorodriguez.com
|
@fabian-hiller I don't see any reference to the discriminated union in the latest release. Was it included? |
It is not public yet. I will try to publish it over the weekend. |
For those interested, the way I'm working around this is by creating intermediate more permissive schemas. For example, the fields that are unions I have them as unknown, then I specify them in a derived schema. Then I perform a quick validation with the final schema, and if that fails I start from the more permissive to the more restrictive wrapping in my own errors |
@danielo515 thank you for the info. Can you provide a code example? I plan to release |
v0.20.0 is now available |
Hello, first of all, thank you for the work you are putting into this library.
Currently I am trying to use it to validate an array of objects that can be of a variety of shapes, hence I use union to specify all the possible types.
When I get an error in a "first level" I get a quite readable message using my very basic "flatten function". Something like:
field name should not be empty at 4.name
However, when the error happens at a nested property, despite I am specifying proper specific errors for everything I get a very generic message:
Invalid type at 4.input
I think the cause is the usage of union. Here is a basic example:
I will be very grateful if you either provide a nicer way to reach this errors or a workaround to implement it myself.
Cheers
The text was updated successfully, but these errors were encountered: