-
-
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
toJsonSchema generates unsatisfiable schema for intersect of objects #990
Comments
Thank you for creating this issue. I will investigate and fix it in the next few weeks. |
As a workaround you can try using object merging instead of import * as v from 'valibot';
const ObjectSchema1 = v.object({ foo: v.string(), baz: v.number() });
const ObjectSchema2 = v.object({ bar: v.string(), baz: v.boolean() });
const MergedSchema = v.object({
...ObjectSchema1.entries,
...ObjectSchema2.entries,
}); // { foo: string; bar: string; baz: boolean } |
That does not work (try it). You can, however, set |
Thank you for your feedback! If any of you have some time, feel free to research how other libraries, e.g. TypeBox, handle this case, or if there is an official recommendation on the official JSON schema website. |
fwiw that's pretty much what so when we combine multiple schemas (via I suppose you could make an |
It seems that TypeBox (a JSON Schema library) does exactly what we do: |
I opened #997 as one possible solution the couple of zod JSON schema generators that exist seem to do what that PR does too feel free to throw it away though if its not the direction you want to go also note that we set |
Perhaps we should not add |
since we're coming from typescript types, I think we're correct in setting especially since we support things like if we don't set it anymore, it means the typescript types and the JSON schema don't quite match up anymore and there'd be no difference between an object, and an object with rest |
My idea was to add |
that could work it would mean in an intersection of strict objects, we would still want to set |
Our implementation is still on Draft 07, where |
makes sense it just means you wont be able to convert intersections of strict objects into a schema validation will fail if you have properties from multiple sub-schemas maybe we could temporarily throw in those cases until we do update to the newer draft? otherwise the schema it will produce isn't of much use |
There is a workaround. Users could merge schemas instead of using As a small bundle size is one of the main features of Valibot I am not sure if I want to add code to throw and error in this case. |
Actually |
🤔 interesting So an intersection of two strict objects:
Is expected to fail for the input The JSON schema will fail to validate that input. You're saying valibot will too? |
Yes; try it. |
I'm not at a laptop right now so couldn't try it myself. Thanks for the playground 👍 In that case, Dumbing the schema down to always allow additional properties is the easy way out then. Then object with rest is the same as object |
It may still be meaningful to intersect strict objects with the same set of required keys, or intersect a strict object and a non-strict object with fewer required keys. What I’m saying is that adding |
Intersecting with a strict or non strict object which has extra keys can never validate in JSON schema if the input uses keys from both. That seems understood now at least So yes, strict objects can have additional properties disabled and that won't help the OP Going back to the OP, we could set additional properties to |
Removing |
Indeed it will. That is exactly what I was suggesting, the same as what Fabian suggested
Good to be agreed! |
`additionalProperties: true` was preventing validation of valid intersected objects. Fixes fabian-hiller#990. Signed-off-by: Anders Kaseorg <[email protected]>
Problem
When using
toJsonSchema
withintersect
of objects, it generates a JSON Schema that cannot be satisfied due to conflictingadditionalProperties: false
constraints.I understand that Valibot is not primarily designed for JSON Schema compatibility, but this makes the conversion feature unusable for any JSON Schema interoperability scenarios.
Reproduction
Current Behavior
The generated JSON Schema includes
additionalProperties: false
for each object inallOf
, making it impossible to satisfy both constraints simultaneously.Expected Behavior
The generated JSON Schema should allow objects that have both properties, matching Valibot's runtime validation behavior.
Possible solutions:
additionalProperties: false
from individual objects in allOfadditionalProperties: false
only at the parent levelversions
The text was updated successfully, but these errors were encountered: