JSPM

@sourcemeta/blaze

14.20.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 481
  • Score
    100M100P100Q77389F
  • License LGPL-3.0-or-later

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

Package Exports

  • @sourcemeta/blaze

Readme

@sourcemeta/blaze

NPM Version NPM Downloads GitHub contributors

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/blaze

Usage

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.json

Then 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