Repository navigation
Replies: 1 comment
|
Validate each fetched schema inside import Ajv from "ajv";
const ajv = new Ajv({
loadSchema: async (uri) => {
const schema = await fetchSchema(uri); // your existing loader
if (!ajv.validateSchema(schema)) {
const errors = structuredClone(ajv.errors);
throw new Error(`Invalid schema ${uri}: ${JSON.stringify(errors)}`);
}
return schema;
},
});
try {
const validate = await ajv.compileAsync(rootSchema);
// Use validate(data).
} catch (error) {
console.error(error);
}This identifies the fetched schema that failed validation. Copy References: validateSchema, asynchronous schema loading. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi folks,
I want to clearly address in which subschema the problem is(e.g. by $id)?
I setup the loadSchema callback and the schema are inserted in the ajv instance if everything is fine.
If the subschema has an error - e.g. simple

type: "x"- the compileAsync rejects as expected.But the error message and ajv.errors content are scoped to the subschema
'schema is invalid: data/type must be equal to one of the allowed values, data/type must be array, data/type must match a schema in anyOf'So the
paths are all wrong - here `/type' instead of '/root/property/type' if root.property.$ref = subschema. Having the correct paths would also be helpful but a promise reject is fine but then besides the ajv.errors I would expect a scope property or something like that.But docs https://ajv.js.org/api.html#ajv-compileasync-schema-object-meta-boolean-promise-function is mentioning another callback but I cannot find it. If this callback would have the $id and the scoped messages from above then this would be even better.
Any hints to narrow down the problematic schema?
All reactions