Skip to content

Commit 64d8a2e

Browse files
committed
Convert output and annotations to evaluation plugins
1 parent 640b53a commit 64d8a2e

File tree

141 files changed

+1386
-224
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

141 files changed

+1386
-224
lines changed

annotations/index.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { FLAG } from "../lib/index.js";
22
import { ValidationError } from "./validation-error.js";
3-
import { getSchema, compile, BASIC } from "../lib/experimental.js";
3+
import { getSchema, compile, BASIC, DETAILED } from "../lib/experimental.js";
44
import Validation from "../lib/keywords/validation.js";
55
import * as Instance from "../lib/instance.js";
6+
import { annotationsPlugin } from "../lib/evaluation-plugins/annotations.js";
7+
import { basicOutputPlugin } from "../lib/evaluation-plugins/basic-output.js";
8+
import { detailedOutputPlugin } from "../lib/evaluation-plugins/detailed-output.js";
69

710

811
export const annotate = async (schemaUri, json = undefined, outputFormat = undefined) => {
@@ -14,17 +17,29 @@ export const annotate = async (schemaUri, json = undefined, outputFormat = undef
1417
};
1518

1619
export const interpret = ({ ast, schemaUri }, instance, outputFormat = BASIC) => {
17-
const errors = [];
18-
const annotations = [];
19-
const context = { ast, dynamicAnchors: {}, errors, annotations, outputFormat };
20+
const context = { ast, plugins: [annotationsPlugin], dynamicAnchors: {} };
21+
22+
switch (outputFormat) {
23+
case FLAG:
24+
break;
25+
case BASIC:
26+
context.plugins.push(basicOutputPlugin);
27+
break;
28+
case DETAILED:
29+
context.plugins.push(detailedOutputPlugin);
30+
break;
31+
default:
32+
throw Error(`Unsupported output format '${outputFormat}'`);
33+
}
34+
2035
const valid = Validation.interpret(schemaUri, instance, context);
2136

2237
if (!valid) {
23-
const result = outputFormat === FLAG || valid ? { valid } : { valid, errors };
38+
const result = !valid && "errors" in context ? { valid, errors: context.errors } : { valid };
2439
throw new ValidationError(result);
2540
}
2641

27-
for (const annotation of annotations) {
42+
for (const annotation of context.annotations) {
2843
const node = Instance.get(annotation.instanceLocation, instance);
2944
const keyword = annotation.keyword;
3045
if (!node.annotations[keyword]) {

bundle/generate-snapshots.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { writeFile, mkdir, rm } from "node:fs/promises";
22
import { isCompatible, md5, loadSchemas, testSuite, unloadSchemas } from "./test-utils.js";
3-
import { DETAILED, compile, getSchema, Validation } from "../lib/experimental.js";
3+
import { compile, getSchema, Validation } from "../lib/experimental.js";
44
import "../stable/index.js";
55
import "../draft-2020-12/index.js";
66
import "../draft-2019-09/index.js";
77
import "../draft-07/index.js";
88
import "../draft-06/index.js";
99
import "../draft-04/index.js";
1010
import * as Instance from "../lib/instance.js";
11+
import { detailedOutputPlugin } from "../lib/evaluation-plugins/detailed-output.js";
12+
import { annotationsPlugin } from "../lib/evaluation-plugins/annotations.js";
1113

1214

1315
const suite = testSuite("./bundle/tests");
@@ -28,12 +30,18 @@ const snapshotGenerator = async (version, dialect) => {
2830
const { ast, schemaUri } = await compile(schema);
2931

3032
const instance = Instance.fromJs(test.instance);
31-
const errors = [];
32-
const annotations = [];
33-
const context = { ast, dynamicAnchors: {}, errors, annotations, outputFormat: DETAILED };
33+
const context = {
34+
ast,
35+
plugins: [detailedOutputPlugin, annotationsPlugin],
36+
dynamicAnchors: {}
37+
};
3438
const valid = Validation.interpret(schemaUri, instance, context);
3539

36-
const expectedOutput = { valid, errors, annotations };
40+
const expectedOutput = {
41+
valid,
42+
errors: context.errors,
43+
annotations: context.annotations
44+
};
3745

3846
unloadSchemas(testCase, mainSchemaUri);
3947

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"valid": true,
33
"errors": [],
4-
"annotations": []
4+
"annotations": [
5+
{
6+
"keyword": "https://json-schema.org/keyword/title",
7+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/string#/title",
8+
"instanceLocation": "#",
9+
"annotation": "String"
10+
}
11+
]
512
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"valid": true,
33
"errors": [],
4-
"annotations": []
4+
"annotations": [
5+
{
6+
"keyword": "https://json-schema.org/keyword/title",
7+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/string#/title",
8+
"instanceLocation": "#/foo",
9+
"annotation": "String"
10+
}
11+
]
512
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"valid": true,
33
"errors": [],
4-
"annotations": []
4+
"annotations": [
5+
{
6+
"keyword": "https://json-schema.org/keyword/title",
7+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/string#/title",
8+
"instanceLocation": "#/foo",
9+
"annotation": "String"
10+
}
11+
]
512
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"valid": true,
33
"errors": [],
4-
"annotations": []
4+
"annotations": [
5+
{
6+
"keyword": "https://json-schema.org/keyword/title",
7+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/string#/title",
8+
"instanceLocation": "#/foo",
9+
"annotation": "String"
10+
}
11+
]
512
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"valid": true,
33
"errors": [],
4-
"annotations": []
4+
"annotations": [
5+
{
6+
"keyword": "https://json-schema.org/keyword/title",
7+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/string#/title",
8+
"instanceLocation": "#/foo",
9+
"annotation": "String"
10+
}
11+
]
512
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"valid": true,
33
"errors": [],
4-
"annotations": []
4+
"annotations": [
5+
{
6+
"keyword": "https://json-schema.org/keyword/title",
7+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/string#/title",
8+
"instanceLocation": "#/foo",
9+
"annotation": "String"
10+
}
11+
]
512
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"valid": true,
33
"errors": [],
4-
"annotations": []
4+
"annotations": [
5+
{
6+
"keyword": "https://json-schema.org/keyword/title",
7+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/string#/title",
8+
"instanceLocation": "#",
9+
"annotation": "String"
10+
}
11+
]
512
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"valid": true,
33
"errors": [],
4-
"annotations": []
4+
"annotations": [
5+
{
6+
"keyword": "https://json-schema.org/keyword/title",
7+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/string#/title",
8+
"instanceLocation": "#/foo",
9+
"annotation": "String"
10+
}
11+
]
512
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"valid": true,
33
"errors": [],
4-
"annotations": []
4+
"annotations": [
5+
{
6+
"keyword": "https://json-schema.org/keyword/title",
7+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/string#/title",
8+
"instanceLocation": "#",
9+
"annotation": "String"
10+
}
11+
]
512
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"valid": true,
33
"errors": [],
4-
"annotations": []
4+
"annotations": [
5+
{
6+
"keyword": "https://json-schema.org/keyword/title",
7+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/string#/title",
8+
"instanceLocation": "#/foo",
9+
"annotation": "String"
10+
}
11+
]
512
}
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
{
22
"valid": true,
33
"errors": [],
4-
"annotations": []
4+
"annotations": [
5+
{
6+
"keyword": "https://json-schema.org/keyword/title",
7+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/string#/title",
8+
"instanceLocation": "#/foo",
9+
"annotation": "String"
10+
},
11+
{
12+
"keyword": "https://json-schema.org/keyword/title",
13+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/number#/title",
14+
"instanceLocation": "#/bar",
15+
"annotation": "Number"
16+
}
17+
]
518
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"valid": true,
33
"errors": [],
4-
"annotations": []
4+
"annotations": [
5+
{
6+
"keyword": "https://json-schema.org/keyword/title",
7+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/string#/title",
8+
"instanceLocation": "#/bar",
9+
"annotation": "String"
10+
}
11+
]
512
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"valid": true,
33
"errors": [],
4-
"annotations": []
4+
"annotations": [
5+
{
6+
"keyword": "https://json-schema.org/keyword/title",
7+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/string#/title",
8+
"instanceLocation": "#/0",
9+
"annotation": "String"
10+
}
11+
]
512
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"valid": true,
33
"errors": [],
4-
"annotations": []
4+
"annotations": [
5+
{
6+
"keyword": "https://json-schema.org/keyword/title",
7+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/string#/title",
8+
"instanceLocation": "#/0",
9+
"annotation": "String"
10+
}
11+
]
512
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"valid": true,
33
"errors": [],
4-
"annotations": []
4+
"annotations": [
5+
{
6+
"keyword": "https://json-schema.org/keyword/title",
7+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/string#/title",
8+
"instanceLocation": "#/foo",
9+
"annotation": "String"
10+
}
11+
]
512
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"valid": true,
33
"errors": [],
4-
"annotations": []
4+
"annotations": [
5+
{
6+
"keyword": "https://json-schema.org/keyword/title",
7+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/string#/title",
8+
"instanceLocation": "#/foo",
9+
"annotation": "String"
10+
}
11+
]
512
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"valid": true,
33
"errors": [],
4-
"annotations": []
4+
"annotations": [
5+
{
6+
"keyword": "https://json-schema.org/keyword/title",
7+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/string#/title",
8+
"instanceLocation": "#/foo",
9+
"annotation": "String"
10+
}
11+
]
512
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"valid": true,
33
"errors": [],
4-
"annotations": []
4+
"annotations": [
5+
{
6+
"keyword": "https://json-schema.org/keyword/title",
7+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/string#/title",
8+
"instanceLocation": "#/foo",
9+
"annotation": "String"
10+
}
11+
]
512
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"valid": true,
33
"errors": [],
4-
"annotations": []
4+
"annotations": [
5+
{
6+
"keyword": "https://json-schema.org/keyword/title",
7+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/string#/title",
8+
"instanceLocation": "#/bar",
9+
"annotation": "String"
10+
}
11+
]
512
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"valid": true,
33
"errors": [],
4-
"annotations": []
4+
"annotations": [
5+
{
6+
"keyword": "https://json-schema.org/keyword/title",
7+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/string#/title",
8+
"instanceLocation": "#",
9+
"annotation": "String"
10+
}
11+
]
512
}

0 commit comments

Comments
 (0)