1.0.3 • Published 1 year ago

deref-json-schema v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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

  1. Start with a base schema file feed.schema.json that references a subschema file 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"
      }
    }
  }
}
  1. 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.

  1. 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 } }
    ]
});
1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago