You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constSimpleSchema=V.object({x: V.number()});typeTSimpleInput=V.InferInput<typeofSimpleSchema>;// { x: number; } but in practice { x: number; [key: string]: any; }typeTSimpleOutput=V.InferOutput<typeofSimpleSchema>;// { x: number; }console.debug("simple object parse result:",V.safeParse(SimpleSchema,{x: 5,y: 7}));
So the inferred input type is stricter than what safeParse will allow.
This bug (in my opinion) is also reflected in the intersection docs: https://valibot.dev/guides/intersections/, which further suggests that the implementation is not in line with people's intuition about how it should work:
because when parsing, it will not complain about extra properties, whereas the typescript compiler would complain about extra properties in the first version.
This gets worse when using a strictObject:
constfirstSchema=v.strictObject({a: v.number(),b: v.string(),});typeTFirstInput=v.InferInput<typeoffirstSchema>;// { a: number; b: string }constsecondSchema=v.strictObject({c: v.number(),});typeTSecondInput=v.InferInput<typeofsecondSchema>;// { c: number; }constintersectSchema=v.intersect([firstSchema,secondSchema]);typeTIntersectInput=v.InferInput<typeofintersectSchema>;// { a: number; b: string; c: number; }console.debug("intersectSchema parse result:",v.safeParse(intersectSchema,{a: 1,b: "foo",c: 3}));// => {// typed: false,// success: false,// output: { a: 1, b: "foo", c: 3 },// issues: [// 'Invalid key: Expected never but received "c"',// 'Invalid key: Expected never but received "a"',// ]
This is not inline with the inferred types, but also unintuitive (as well as useless) from a user perspective.
(Unfortunately zod has the exact same bug)
REMARK: if this is how it is supposed to work, then toJsonSchema should also be updated to avoid simply producing { allOf: [ { additionalProperties: false }, { ... } ] } as that would have the same problem.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'll start with a simple example:
So the inferred input type is stricter than what safeParse will allow.
This bug (in my opinion) is also reflected in the intersection docs: https://valibot.dev/guides/intersections/, which further suggests that the implementation is not in line with people's intuition about how it should work:
These docs use the following example:
but in reality we get:
because when parsing, it will not complain about extra properties, whereas the typescript compiler would complain about extra properties in the first version.
This gets worse when using a strictObject:
This is not inline with the inferred types, but also unintuitive (as well as useless) from a user perspective.
(Unfortunately zod has the exact same bug)
REMARK: if this is how it is supposed to work, then
toJsonSchemashould also be updated to avoid simply producing{ allOf: [ { additionalProperties: false }, { ... } ] }as that would have the same problem.I let Github Copilot crunch on the issue to make it behave the way I would expect it to behave: https://github.com/mrft/valibot/tree/feat-intersect-strict
Beta Was this translation helpful? Give feedback.
All reactions