Hyperjump - JSON Schema Validator
JSON Schema Validator (JSV) is built on JSON Schema Core.
- Supported JSON Schema Versions
- draft-04 | draft-06 | draft-07 | Draft 2019-09
- Create your own version with JSC
- Schemas can reference other schemas using a different draft
- Supported vocabularies (new in Draft 2019-09)
- Draft 2019-09: core | applicator | validation | meta-data | content
- Create your own vocabulary with JSC
- Output formats
- Draft 2019-09: FLAG, BASIC, DETAILED, VERBOSE
- Create your own output format with JSC
- Load schemas from filesystem (file://), network (http(s)://), or JavaScript
- Build non-validation JSON-Schema based tools with JSC
Install
JSV is designed to run in a vanilla node.js environment, but has no dependencies
on node.js specific libraries so it can be bundled for the browser. No
compilers, preprocessors, or bundlers are used.
Node.js
npm install @hyperjump/json-schema
Browser
When in a browser context, JSV is designed to use the browser's fetch
implementation instead of a node.js fetch clone. The Webpack bundler does this
properly without any extra configuration, but if you are using the Rollup
bundler you will need to include the browser: true
option in your Rollup
configuration.
plugins: [
resolve({
browser: true
}),
commonjs()
]
Versioning
This project is in beta and there may be breaking changes at any time. When it's
stable enough, I'll publish v1.0.0 and follow semantic versioning from there on
out.
Usage
const JsonSchema = require("@hyperjump/json-schema");
const schemaJson = {
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "http://example.com/schemas/string",
"type": "string"
}
JsonSchema.add(schemaJson);
const schema = await JsonSchema.get("http://example.com/schemas/string");
const schema = await JsonSchema.get("http://example.com/schemas/string");
const schema = await JsonSchema.get("file:///path/to/my/schemas/string.schema.json");
const output = await JsonSchema.validate(schema, "foo");
if (output.valid) {
console.log("Instance is valid :-)");
} else {
console.log("Instance is invalid :-(");
}
const isString = await JsonSchema.validate(schema);
const output = isString("foo");
const output = await JsonSchema.validate(schema, "foo", JsonSchema.VERBOSE);
JsonSchema.setMetaOutputFormat(JsonSchema.FLAG);
JsonSchema.setShouldMetaValidate(false);
API
-
add: (schema: object, url?: URI, schemaVersion?: string) => undefined
Load a schema. See JSC - $id
and JSC - $schema
for more information.
-
get: (url: URI, contextDoc?: SDoc, recursive: boolean = false) => Promise
Fetch a schema. Schemas can come from an HTTP request, a file, or a schema
that was added with add
.
-
validate: (schema: SDoc, instance: any, outputFormat: OutputFormat = FLAG) => OutputUnit
Validate an instance against a schema. The function is curried to allow
compiling the schema once and applying it to multiple instances.
-
setMetaOutputFormat: (outputFormat: OutputFormat = DETAILED) => undefined
Set the output format for meta-validation. Meta-validation output is only
returned if meta-validation results in an error.
-
setShouldMetaValidate: (isEnabled: boolean) => undefined
Enable or disable meta-validation.
-
OutputFormat: [FLAG | BASIC | DETAILED | VERBOSE]
JSC - Output for
more information on output formats.
Not (yet) Supported
This implementation supports all required features of JSON Schema draft 2019-09.
The following optional features are not supported yet.
Contributing
Tests
Run the tests
npm test
Run the tests with a continuous test runner
npm test -- --watch