Package Exports
- @sourcemeta/blaze
Readme
@sourcemeta/blaze
A pure JavaScript port of the evaluator from Blaze, a high-performance C++ JSON Schema validator. Zero dependencies. Supports Draft 4, Draft 6, Draft 7, 2019-09, and 2020-12 with schema-specific code generation for near-native speed.
Install
npm install --save @sourcemeta/blazeUsage
Blaze evaluates pre-compiled schema templates. Compile your JSON Schema using the jsonschema CLI (compile docs):
npm install --global @sourcemeta/jsonschema
cat > schema.json <<'EOF'
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" }
},
"required": [ "name" ]
}
EOF
jsonschema compile schema.json --fast > template.jsonThen validate instances:
import { readFileSync } from "node:fs";
import { Blaze } from "@sourcemeta/blaze";
const template =
JSON.parse(readFileSync("template.json", "utf-8"));
const evaluator = new Blaze(template);
// true or false
console.log(evaluator.validate({ name: "John", age: 30 }));With an evaluation callback for tracing:
const instance = { name: "John", age: 30 };
const result = evaluator.validate(instance,
(type, valid, instruction,
evaluatePath, instanceLocation, annotation) => {
console.log(type, evaluatePath,
instanceLocation, valid);
});
console.log(result); // true or false