-
-
Notifications
You must be signed in to change notification settings - Fork 216
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
Invalid issue message when using union with another schema inside it #1035
Comments
I think this fixes your problem. See this playground.
import * as v from 'valibot';
const SubSchema = v.object({
a: v.number(),
b: v.pipe(v.number(), v.picklist([1, 2, 3, 4])),
});
const Schema = v.object({
value: v.union([v.literal(-1), SubSchema]),
});
`` |
Even with your example after passing the object as a const result = v.safeParse(Schema, { value: { a: 2, b: 6} }); I get an issue message:
instead of
Your example with object as a input: playground |
You are currently using two schema functions inside The problem with your current schema is that the property const SubSchema = v.object({
a: v.number(),
b: v.pipe(
v.number(),
v.check(
(input) => [1, 2, 3, 4].includes(input),
(issue) =>
`Invalid input: Expected (1 | 2 | 3 | 4) but received ${issue.received}`,
),
),
});
const Schema = v.object({
value: v.union([v.literal(-1), SubSchema]),
}); After PR #919 is merged you can change this code to: import * as v from 'valibot';
const SubSchema = v.object({
a: v.number(),
b: v.pipe(v.number(), v.values([1, 2, 3, 4])),
});
const Schema = v.object({
value: v.union([v.literal(-1), SubSchema]),
}); Depending on your use case you could also use a range validation instead: import * as v from 'valibot';
const SubSchema = v.object({
a: v.number(),
b: v.pipe(v.number(), v.minValue(1), v.maxValue(4)),
});
const Schema = v.object({
value: v.union([v.literal(-1), SubSchema]),
}); |
Okay, now I understand the reason. For now, I will stick to the Thank you for your help and explanation. |
I will try to merge PR #919 next week. |
Hey, I came across a problem with parsing a union of a number and another schema. I have a case where I get a specific number or object with values. So I created a subSchema and a union type:
And now, parsing the valid values works correctly.
But when I parse an invalid value I expect a proper error message for both cases.
Instead, when I provide object input for the
value
field, I get a generic error that contains anotherissue
array with two issue objects - the first invalid and the second valid.I have been using valibot for a relatively short time so if this is not a bug and there is some other way to create such a scheme I would appreciate your help. I haven't found a similar issue anywhere, and it seems to me that the error message is not quite returned correctly in this case.
The text was updated successfully, but these errors were encountered: