-
Notifications
You must be signed in to change notification settings - Fork 12
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
Code generation that deduplicates validation logic #140
Comments
This comment was marked as off-topic.
This comment was marked as off-topic.
Ah, disregard the previous comment, I misunderstood. Sorry for the late reply on this. Compiling several schemas into one object to avoid duplication is an interesting request, this should definitely be viable. |
With e931d97 in place, chalker/multi-schema branch implements this as a proof-of-concept (not cleaned up) The API will likely be different though This seems to work though: const schemas = {
"a": {
"type": "object",
"properties": {
"value": { "$ref": "b" }
}
},
"b": {
"type": "array",
"items": { "$ref": "a" }
}
}
const validates = validators(Object.values(schemas), { schemas })
console.log(validates[1]([]))
console.log(validates.toModule()) This also works: const schemas = [
{
$id: "https://example.com/a",
"type": "object",
"properties": {
"value": { "$ref": "https://example.com/b" }
}
},
{
$id: "https://example.com/b",
"type": "array",
"items": { "$ref": "https://example.com/a" }
}
]
const validates = validators(schemas, { schemas })
console.log(validates[1]([]))
console.log(validates.toModule()) |
This also works now: const schemas = [
{
$id: "https://example.com/a",
"type": "object",
"properties": {
"value": { "$ref": "https://example.com/b" }
}
},
{
$id: "https://example.com/b",
"type": "array",
"items": { "$ref": "https://example.com/a" }
},
{}
]
const validates = validators(schemas)
console.log(validates[1]([]))
console.log(validates.toModule()) The API will likely change though. |
The API has changed to |
Implemented in 1272774, now both See example in tests. Not properly documented for now. |
Released in v1.0.0-rc.9 |
Use-case
Imagine you're building validator codecs for a set of inter-related schemas that define
$ref
s on each other. The (awesome) code generation results in these being treated as totally isolated IIFE-enclosed validators. These validators duplicate the validation logic of all referenced schemas.Request
It would be really interesting to have a facility whereby a set of inter-related schemas could be compiled as a unit and thereby produced smaller validation logic by referencing each other appropriately. I suspect that this would require major changes to how errors are collected as we would now have
N
validator functions instead of1
on which errors may be added.Anyway, schemasafe is a fantastic bit of work. Really appreciate the rigour, speed and comprehensiveness. I'm using it with https://github.com/ggoodman/json-schema-to-dts to build codegen typed validation codecs.
The text was updated successfully, but these errors were encountered: