Skip to content

Commit 207ca63

Browse files
committed
Refactor output and annotation handling to fix bug
1 parent 0825eb0 commit 207ca63

File tree

462 files changed

+6362
-5711
lines changed

Some content is hidden

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

462 files changed

+6362
-5711
lines changed

README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,9 @@ addKeyword({
402402
// );
403403
},
404404

405-
interpret: (implies, instance, ast, dynamicAnchors, quiet) => {
405+
interpret: (implies, instance, context) => {
406406
return implies.reduce((valid, schema) => {
407-
return !valid || Validation.interpret(schema, instance, ast, dynamicAnchors, quiet);
407+
return !valid || Validation.interpret(schema, instance, context);
408408
}, true);
409409
}
410410
});
@@ -524,23 +524,30 @@ These are available from the `@hyperjump/json-schema/experimental` export.
524524
is needed for compiling sub-schemas. The `parentSchema` parameter is
525525
primarily useful for looking up the value of an adjacent keyword that
526526
might effect this one.
527-
* interpret: (compiledKeywordValue: any, instance: JsonNode, ast: AST, dynamicAnchors: object, quiet: boolean, schemaLocation: string) => boolean
527+
* interpret: (compiledKeywordValue: any, instance: JsonNode, context: ValidationContext) => boolean
528528
529529
This function takes the value returned by the `compile` function and
530530
the instance value that is being validated and returns whether the
531531
value is valid or not. The other parameters are only needed for
532532
validating sub-schemas.
533-
* collectEvaluatedProperties?: (compiledKeywordValue: any, instance: JsonNode, ast: AST, dynamicAnchors: object) => Set\<string> | false
533+
* collectEvaluatedProperties?: (compiledKeywordValue: any, instance: JsonNode, context: ValidationContext, isTop?: boolean) => Set\<string> | false
534534
535535
If the keyword is an applicator, it will need to implement this
536536
function for `unevaluatedProperties` to work as expected.
537-
* collectEvaluatedItems?: (compiledKeywordValue: A, instance: JsonNode, ast: AST, dynamicAnchors: object) => Set\<number> | false
537+
* collectEvaluatedItems?: (compiledKeywordValue: A, instance: JsonNode, context: ValidationContext, isTop?: boolean) => Set\<number> | false
538538
539539
If the keyword is an applicator, it will need to implement this
540540
function for `unevaluatedItems` to work as expected.
541541
* collectExternalIds?: (visited: Set\<string>, parentSchema: Browser, schema: Browser) => Set\<string>
542542
If the keyword is an applicator, it will need to implement this
543-
function to work properly with the [bundle](#bundling) feature.
543+
function to work properly with the [bundle](#bundling) feature.
544+
545+
* **ValidationContext**: object
546+
* ast: AST
547+
* dynamicAnchors: object
548+
* schemaUrl: string
549+
* errors: OutputUnit[]
550+
* annotations: OutputUnit[]
544551
* **defineVocabulary**: (id: string, keywords: { [keyword: string]: string }) => void
545552
546553
Define a vocabulary that maps keyword name to keyword URIs defined using

annotations/annotated-instance.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export const setAnnotation: (keywordUri: string, schemaLocation: string, value: string) => void;
21
export const annotation: <A>(instance: JsonNode, keyword: string, dialectUri?: string) => A[];
32
export const annotatedWith: <A extends JsonNode>(instance: A, keyword: string, dialectUri?: string) => A[];
43

annotations/annotated-instance.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@ import { getKeywordId } from "../lib/keywords.js";
55

66
const defaultDialectId = "https://json-schema.org/validation";
77

8-
export const setAnnotation = (node, keywordUri, schemaLocation, value) => {
9-
if (!(keywordUri in node.annotations)) {
10-
node.annotations[keywordUri] = {};
11-
}
12-
node.annotations[keywordUri][schemaLocation] = value;
13-
};
14-
158
export const annotation = (node, keyword, dialect = defaultDialectId) => {
169
const keywordUri = getKeywordId(keyword, dialect);
1710

annotations/index.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ValidationError } from "./validation-error.js";
2-
import { getSchema, compile, interpret as validate, BASIC } from "../lib/experimental.js";
2+
import { getSchema, compile, BASIC } from "../lib/experimental.js";
3+
import Validation from "../lib/keywords/validation.js";
34
import * as Instance from "../lib/instance.js";
45

56

@@ -12,11 +13,24 @@ export const annotate = async (schemaUri, json = undefined, outputFormat = undef
1213
};
1314

1415
export const interpret = ({ ast, schemaUri }, instance, outputFormat = BASIC) => {
15-
const result = validate({ ast, schemaUri }, instance, outputFormat);
16-
if (!result.valid) {
16+
const errors = [];
17+
const annotations = [];
18+
const valid = Validation.interpret(schemaUri, instance, { ast, schemaUri, dynamicAnchors: {}, errors, annotations });
19+
20+
if (!valid) {
21+
const result = outputFormat === BASIC && !valid ? { valid, errors } : { valid };
1722
throw new ValidationError(result);
1823
}
1924

25+
for (const annotation of annotations) {
26+
const node = Instance.get(annotation.instanceLocation, instance);
27+
const keyword = annotation.keyword;
28+
if (!node.annotations[keyword]) {
29+
node.annotations[keyword] = [];
30+
}
31+
node.annotations[keyword].push(annotation.annotation);
32+
}
33+
2034
return instance;
2135
};
2236

bundle/generate-snapshots.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { writeFile, mkdir, rm } from "node:fs/promises";
2-
import { isCompatible, md5, loadSchemas, testSuite, toOutput, unloadSchemas } from "./test-utils.js";
3-
import { compile, getSchema, interpret } from "../lib/experimental.js";
2+
import { isCompatible, md5, loadSchemas, testSuite, unloadSchemas } from "./test-utils.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";
@@ -23,11 +23,17 @@ const snapshotGenerator = async (version, dialect) => {
2323
let testIndex = 0;
2424
for (const test of testCase.tests) {
2525
loadSchemas(testCase, mainSchemaUri, dialect);
26+
2627
const schema = await getSchema(mainSchemaUri);
27-
const compiledSchema = await compile(schema);
28+
const { ast, schemaUri } = await compile(schema);
29+
2830
const instance = Instance.fromJs(test.instance);
29-
interpret(compiledSchema, instance);
30-
const expectedOutput = toOutput(instance);
31+
const errors = [];
32+
const annotations = [];
33+
const valid = Validation.interpret(schemaUri, instance, { ast, schemaUri, dynamicAnchors: {}, errors, annotations });
34+
35+
const expectedOutput = { valid, errors, annotations };
36+
3137
unloadSchemas(testCase, mainSchemaUri);
3238

3339
const testId = md5(`${version}|${dialect}|${testCase.description}|${testIndex}`);
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
{
2-
"#": {
3-
"errors": {
4-
"https://bundler.hyperjump.io/main#/type": "https://json-schema.org/keyword/type",
5-
"https://bundler.hyperjump.io/main#": "https://json-schema.org/evaluation/validate"
6-
},
7-
"annotations": {}
8-
}
2+
"valid": false,
3+
"errors": [
4+
{
5+
"keyword": "https://json-schema.org/keyword/type",
6+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/main#/type",
7+
"instanceLocation": "#"
8+
}
9+
],
10+
"annotations": []
911
}
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
2-
"#": {
3-
"errors": {},
4-
"annotations": {
5-
"https://json-schema.org/keyword/title": {
6-
"https://bundler.hyperjump.io/string#": "String"
7-
}
2+
"valid": true,
3+
"errors": [],
4+
"annotations": [
5+
{
6+
"keyword": "https://json-schema.org/keyword/title",
7+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/string#/title",
8+
"instanceLocation": "#",
9+
"annotation": "String"
810
}
9-
}
11+
]
1012
}
Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
{
2-
"#": {
3-
"errors": {
4-
"https://bundler.hyperjump.io/main#/unevaluatedProperties": "https://json-schema.org/keyword/unevaluatedProperties",
5-
"https://bundler.hyperjump.io/main#": "https://json-schema.org/evaluation/validate"
2+
"valid": false,
3+
"errors": [
4+
{
5+
"keyword": "https://json-schema.org/keyword/unevaluatedProperties",
6+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/main#/unevaluatedProperties",
7+
"instanceLocation": "#"
68
},
7-
"annotations": {}
8-
},
9-
"#/foo": {
10-
"errors": {
11-
"https://bundler.hyperjump.io/string#/type": "https://json-schema.org/keyword/type",
12-
"https://bundler.hyperjump.io/string#": "https://json-schema.org/evaluation/validate",
13-
"https://bundler.hyperjump.io/main#/unevaluatedProperties/$ref": "https://json-schema.org/keyword/ref",
14-
"https://bundler.hyperjump.io/main#/unevaluatedProperties": "https://json-schema.org/evaluation/validate"
9+
{
10+
"keyword": "https://json-schema.org/keyword/ref",
11+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/main#/unevaluatedProperties/$ref",
12+
"instanceLocation": "#/foo"
1513
},
16-
"annotations": {
17-
"https://json-schema.org/keyword/title": {
18-
"https://bundler.hyperjump.io/string#": "String"
19-
}
14+
{
15+
"keyword": "https://json-schema.org/keyword/type",
16+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/string#/type",
17+
"instanceLocation": "#/foo"
2018
}
21-
}
19+
],
20+
"annotations": []
2221
}
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
{
2-
"#": {
3-
"errors": {
4-
"https://bundler.hyperjump.io/main#/not": "https://json-schema.org/keyword/not",
5-
"https://bundler.hyperjump.io/main#": "https://json-schema.org/evaluation/validate"
6-
},
7-
"annotations": {
8-
"https://json-schema.org/keyword/title": {
9-
"https://bundler.hyperjump.io/string#": "String"
10-
}
2+
"valid": false,
3+
"errors": [
4+
{
5+
"keyword": "https://json-schema.org/keyword/not",
6+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/main#/not",
7+
"instanceLocation": "#"
118
}
12-
}
9+
],
10+
"annotations": []
1311
}
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
{
2-
"#": {
3-
"errors": {},
4-
"annotations": {}
5-
},
6-
"#/0": {
7-
"errors": {},
8-
"annotations": {}
9-
}
2+
"valid": true,
3+
"errors": [],
4+
"annotations": []
105
}
Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
{
2-
"#": {
3-
"errors": {
4-
"https://bundler.hyperjump.io/main#/properties": "https://json-schema.org/keyword/properties",
5-
"https://bundler.hyperjump.io/main#": "https://json-schema.org/evaluation/validate"
2+
"valid": false,
3+
"errors": [
4+
{
5+
"keyword": "https://json-schema.org/keyword/properties",
6+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/main#/properties",
7+
"instanceLocation": "#"
68
},
7-
"annotations": {}
8-
},
9-
"#/foo": {
10-
"errors": {
11-
"https://bundler.hyperjump.io/string#/type": "https://json-schema.org/keyword/type",
12-
"https://bundler.hyperjump.io/string#": "https://json-schema.org/evaluation/validate",
13-
"https://bundler.hyperjump.io/main#/$defs/string/$ref": "https://json-schema.org/keyword/ref",
14-
"https://bundler.hyperjump.io/main#/$defs/string": "https://json-schema.org/evaluation/validate",
15-
"https://bundler.hyperjump.io/main#/properties/foo/$ref": "https://json-schema.org/keyword/ref",
16-
"https://bundler.hyperjump.io/main#/properties/foo": "https://json-schema.org/evaluation/validate"
9+
{
10+
"keyword": "https://json-schema.org/keyword/ref",
11+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/main#/properties/foo/$ref",
12+
"instanceLocation": "#/foo"
1713
},
18-
"annotations": {
19-
"https://json-schema.org/keyword/title": {
20-
"https://bundler.hyperjump.io/string#": "String"
21-
}
14+
{
15+
"keyword": "https://json-schema.org/keyword/ref",
16+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/main#/$defs/string/$ref",
17+
"instanceLocation": "#/foo"
18+
},
19+
{
20+
"keyword": "https://json-schema.org/keyword/type",
21+
"absoluteKeywordLocation": "https://bundler.hyperjump.io/string#/type",
22+
"instanceLocation": "#/foo"
2223
}
23-
}
24+
],
25+
"annotations": []
2426
}
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
{
2-
"#": {
3-
"errors": {
4-
"https://bundler.hyperjump.io/main#/if": "https://json-schema.org/evaluation/validate"
5-
},
6-
"annotations": {}
7-
}
2+
"valid": true,
3+
"errors": [],
4+
"annotations": []
85
}
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
{
2-
"#": {
3-
"errors": {},
4-
"annotations": {}
5-
},
6-
"#/foo": {
7-
"errors": {},
8-
"annotations": {
9-
"https://json-schema.org/keyword/title": {
10-
"https://bundler.hyperjump.io/string#": "String"
11-
}
2+
"valid": true,
3+
"errors": [],
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"
1210
}
13-
}
11+
]
1412
}
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
{
2-
"#": {
3-
"errors": {},
4-
"annotations": {}
5-
},
6-
"#/foo": {
7-
"errors": {},
8-
"annotations": {
9-
"https://json-schema.org/keyword/title": {
10-
"https://bundler.hyperjump.io/string#": "String"
11-
}
2+
"valid": true,
3+
"errors": [],
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"
1210
}
13-
}
11+
]
1412
}
Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
{
2-
"#": {
3-
"errors": {},
4-
"annotations": {}
5-
},
6-
"#/foo": {
7-
"errors": {},
8-
"annotations": {}
9-
},
10-
"#/foo/0": {
11-
"errors": {},
12-
"annotations": {}
13-
}
2+
"valid": true,
3+
"errors": [],
4+
"annotations": []
145
}
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
{
2-
"#": {
3-
"errors": {
4-
"https://bundler.hyperjump.io/main#/if": "https://json-schema.org/evaluation/validate"
5-
},
6-
"annotations": {}
7-
}
2+
"valid": true,
3+
"errors": [],
4+
"annotations": []
85
}

0 commit comments

Comments
 (0)