ts-json-schema-generator
ts-json-schema-generator copied to clipboard
How do you use ajv with references?
Hi,
Sorry for the dumb question but I'm struggling a bit to use this package. I'm exporting multiple Typescript interface, so I end up with a file like this:
{
"definitions": {
"Test1": {
"type": "object",
"properties": {
"nested": {"$ref": "#/definitions/Test2"}
},
"required": [ "nested" ],
"additionalProperties": false
},
"Test2": {
"type": "object",
"properties": {
"title": { "type": "string"}
},
"required": [ "title" ],
"additionalProperties": false
}
}
}
However when it's time to actually use the json schema I'm lost.
I tried using ajv but I'm failing to understand how to validate one interface with a Reference using this json schema. There is no documentation regarding this use case so I'm wondering.
const ajv = new Ajv({ allErrors: true, discriminator: true });
const validate = ajv.compile(jsonSchema); // the one above
// Validate against Test1
validate({ nested: { title: 'foobar' }});
Without a reference I can just load the schema with the appropriate key but when there is references it won't work
const ajv = new Ajv({ allErrors: true, discriminator: true });
const validate = ajv.compile(jsonSchema["definitions"]["Test2"]);
// Validate against Test2 directly
validate({ title: 'foobar' });
Is there any way to tell ajv to validate Test1 inside the schema?
Or any other way to achieve that.
Thanks ☺️