@sourcemeta/blaze 15.1.0 → 15.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -29
- package/describe.mjs +1551 -0
- package/index.d.mts +37 -0
- package/index.mjs +187 -6
- package/opcodes.mjs +206 -0
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -18,30 +18,24 @@ npm install --save @sourcemeta/blaze
|
|
|
18
18
|
|
|
19
19
|
## Usage
|
|
20
20
|
|
|
21
|
-
Blaze evaluates pre-compiled schema templates. Compile your JSON Schema
|
|
21
|
+
Blaze evaluates pre-compiled schema templates. Compile your JSON Schema with
|
|
22
22
|
the [JSON Schema CLI](https://github.com/sourcemeta/jsonschema) (see the
|
|
23
23
|
[`compile`](https://github.com/sourcemeta/jsonschema/blob/main/docs/compile.markdown)
|
|
24
|
-
command)
|
|
24
|
+
command), then load the resulting template and validate instances against it.
|
|
25
|
+
The mode (fast or exhaustive) is fixed at compile time, not evaluation time.
|
|
25
26
|
|
|
26
27
|
```sh
|
|
27
28
|
npm install --global @sourcemeta/jsonschema
|
|
28
|
-
|
|
29
|
-
cat > schema.json <<'EOF'
|
|
30
|
-
{
|
|
31
|
-
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
32
|
-
"type": "object",
|
|
33
|
-
"properties": {
|
|
34
|
-
"name": { "type": "string" },
|
|
35
|
-
"age": { "type": "integer" }
|
|
36
|
-
},
|
|
37
|
-
"required": [ "name" ]
|
|
38
|
-
}
|
|
39
|
-
EOF
|
|
40
|
-
|
|
41
29
|
jsonschema compile schema.json --fast > template.json
|
|
42
30
|
```
|
|
43
31
|
|
|
44
|
-
|
|
32
|
+
Pass a string as the second argument to `validate` to receive a JSON object
|
|
33
|
+
that follows the JSON Schema [Standard Output Format](https://json-schema.org/draft/2020-12/json-schema-core#name-output-formatting)
|
|
34
|
+
instead of a boolean. Two formats are supported:
|
|
35
|
+
[`'flag'`](https://json-schema.org/draft/2020-12/json-schema-core#name-flag)
|
|
36
|
+
(validity only) and
|
|
37
|
+
[`'basic'`](https://json-schema.org/draft/2020-12/json-schema-core#name-basic)
|
|
38
|
+
(errors and annotations):
|
|
45
39
|
|
|
46
40
|
```javascript
|
|
47
41
|
import { readFileSync } from "node:fs";
|
|
@@ -50,24 +44,25 @@ import { Blaze } from "@sourcemeta/blaze";
|
|
|
50
44
|
const template =
|
|
51
45
|
JSON.parse(readFileSync("template.json", "utf-8"));
|
|
52
46
|
const evaluator = new Blaze(template);
|
|
47
|
+
const instance = { name: "John", age: 30 };
|
|
53
48
|
|
|
54
|
-
//
|
|
55
|
-
|
|
56
|
-
```
|
|
49
|
+
// Plain boolean
|
|
50
|
+
evaluator.validate(instance);
|
|
57
51
|
|
|
58
|
-
|
|
52
|
+
// { valid: true } or { valid: false }
|
|
53
|
+
evaluator.validate(instance, "flag");
|
|
59
54
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
(type, valid, instruction,
|
|
64
|
-
evaluatePath, instanceLocation, annotation) => {
|
|
65
|
-
console.log(type, evaluatePath,
|
|
66
|
-
instanceLocation, valid);
|
|
67
|
-
});
|
|
68
|
-
console.log(result); // true or false
|
|
55
|
+
// { valid: true, annotations?: [ ... ] }
|
|
56
|
+
// { valid: false, errors: [ ... ] }
|
|
57
|
+
evaluator.validate(instance, "basic");
|
|
69
58
|
```
|
|
70
59
|
|
|
60
|
+
For lower-level integration, `validate` also accepts a callback that fires for
|
|
61
|
+
every instruction with `(type, valid, instruction, evaluatePath,
|
|
62
|
+
instanceLocation, annotation)`. The exported `describe(valid, instruction,
|
|
63
|
+
evaluatePath, instanceLocation, instance, annotation)` helper turns any such
|
|
64
|
+
event into a human-readable message.
|
|
65
|
+
|
|
71
66
|
### Parsing large integers
|
|
72
67
|
|
|
73
68
|
JavaScript's
|