1.0.3 • Published 1 year ago
deref-json-schema v1.0.3
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
feed.schema.json
that references a subschema filefeed_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
DerefSchema
with base schema
const schema = require('./feed.schema.json');
const schemaDeref = DerefSchema.create(schema);
Creating a DerefSchema
will search schema
, find a $ref
with value 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: 100 } },
{ id: '1', price: { units: 100, currency: 'USD' }, time: { seconds: 200 } }
]
});