1.0.5 • Published 1 year ago
deref-json-schema v1.0.5
deref-json-schema
Creates a dereferenced schema for the @cfworker/json-schema Validator. Traverses schema and calls Validator.addSchema with the schema from each unique local file path referenced in $ref properties.
Basic usage
- Start with a base schema file, for example,
feed.schema.json, that references a subschema file, for example,feed_item.schema.json.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/feed.schema.json",
"title": "Feed schema",
"description": "A feed",
"type": "object",
"properties": {
"data": {
"description": "The feed data",
"type": "array",
"items": {
"$ref": "feed_item.schema.json"
}
}
}
}- Create
DerefSchemawith base schema
const schema = require('./feed.schema.json');
const schemaDeref = DerefSchema.create(schema);Creating a DerefSchema will traverse schema and find a $ref attribute.
The function will then read the file at the value of $ref (feed_item.schema.json) and call Validator.addSchema with the JSON object from the file. This will be done recusively for all $ref schemas; schema files already added will not be added again. A basePath can be optionally supplied in DerefSchema.create to resolve $ref value file paths relative to the basePath.
- Validate
const result = schemaDeref.getValidator().validate({
data: [
{ id: '1', price: { units: 100 }, time: { seconds: 50 } },
{ id: '1', price: { units: 100, currency: 'USD' }, time: { seconds: 22 } }
]
});