Creates a dereferenced schema for the `@cfworker/json-schema` [Validator](https://github.com/cfworker/cfworker/blob/main/packages/json-schema/src/validator.ts). Traverses schema and calls `Validator.addSchema` with the schema from each unique local file p
npm install deref-json-schemaCreates 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.
1. Start with a base schema file, for example, feed.schema.json, that references a subschema file, for example, feed_item.schema.json.
``js`
{
"$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"
}
}
}
}DerefSchema
2. Create with base schema`js`
const schema = require('./feed.schema.json');
const schemaDeref = DerefSchema.create(schema);
Creating a DerefSchema will traverse schema and find a $ref attribute. $ref
The function will then read the file at the value of (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.
3. Validate
`js``
const result = schemaDeref.getValidator().validate({
data: [
{ id: '1', price: { units: 100 }, time: { seconds: 50 } },
{ id: '1', price: { units: 100, currency: 'USD' }, time: { seconds: 22 } }
]
});