Skip to content

Commit 6aa71be

Browse files
committed
Improve setting annotation in keyword handler
1 parent 5bcd959 commit 6aa71be

23 files changed

+73
-199
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,10 @@ These are available from the `@hyperjump/json-schema/experimental` export.
547547
* collectExternalIds?: (visited: Set\<string>, parentSchema: Browser, schema: Browser) => Set\<string>
548548
If the keyword is an applicator, it will need to implement this
549549
function to work properly with the [bundle](#bundling) feature.
550+
* annotation?: (compiledKeywordValue: any) => any
551+
552+
If the keyword is an annotation, it will need to implement this
553+
function to return the annotation.
550554
551555
* **ValidationContext**: object
552556
* ast: AST
@@ -696,7 +700,7 @@ Schema.
696700
697701
### Usage
698702
An annotated JSON document is represented as a
699-
(JsonNode)[#/instance-api-experimental] AST. You can use this AST to traverse
703+
(JsonNode)[#instance-api-experimental] AST. You can use this AST to traverse
700704
the data structure and get annotations for the values it represents.
701705
702706
```javascript

annotations/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const annotate = async (schemaUri, json = undefined, outputFormat = undef
1616
export const interpret = ({ ast, schemaUri }, instance, outputFormat = BASIC) => {
1717
const errors = [];
1818
const annotations = [];
19-
const context = { ast, schemaUri, dynamicAnchors: {}, errors, annotations, outputFormat };
19+
const context = { ast, dynamicAnchors: {}, errors, annotations, outputFormat };
2020
const valid = Validation.interpret(schemaUri, instance, context);
2121

2222
if (!valid) {

bundle/generate-snapshots.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const snapshotGenerator = async (version, dialect) => {
3030
const instance = Instance.fromJs(test.instance);
3131
const errors = [];
3232
const annotations = [];
33-
const context = { ast, schemaUri, dynamicAnchors: {}, errors, annotations, outputFormat: BASIC };
33+
const context = { ast, dynamicAnchors: {}, errors, annotations, outputFormat: BASIC };
3434
const valid = Validation.interpret(schemaUri, instance, context);
3535

3636
const expectedOutput = { valid, errors, annotations };

bundle/test-suite.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const testRunner = (version: number, dialect: string) => {
6060
const instance = Instance.fromJs(test.instance);
6161
const errors: OutputUnit[] = [];
6262
const annotations: OutputUnit[] = [];
63-
const context = { ast, schemaUri, dynamicAnchors: {}, errors, annotations, outputFormat: BASIC };
63+
const context = { ast, dynamicAnchors: {}, errors, annotations, outputFormat: BASIC };
6464
const valid = Validation.interpret(schemaUri, instance, context);
6565

6666
const output = { valid, errors, annotations };

lib/core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const compile = async (schema) => {
3232

3333
export const interpret = curry(({ ast, schemaUri }, instance, outputFormat = FLAG) => {
3434
const errors = [];
35-
const context = { ast, schemaUri, dynamicAnchors: {}, errors, annotations: [], outputFormat };
35+
const context = { ast, dynamicAnchors: {}, errors, annotations: [], outputFormat };
3636
const valid = Validation.interpret(schemaUri, instance, context);
3737
return outputFormat === FLAG || valid ? { valid } : { valid, errors };
3838
});

lib/experimental.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ export type Keyword<A> = {
7373
collectEvaluatedProperties?: (compiledKeywordValue: A, instance: JsonNode, context: ValidationContext, isTop?: boolean) => Set<string> | false;
7474
collectEvaluatedItems?: (compiledKeywordValue: A, instance: JsonNode, context: ValidationContext, isTop?: boolean) => Set<number> | false;
7575
collectExternalIds?: (visited: Set<string>, parentSchema: Browser<SchemaDocument>, schema: Browser<SchemaDocument>) => Promise<Set<string>>;
76+
annotation?: <B>(compiledKeywordValue: A) => B;
7677
};
7778

7879
export type ValidationContext = {
7980
ast: AST;
80-
schemaUri: string;
8181
dynamicAnchors: Anchors;
8282
errors: OutputUnit[];
8383
annotations: OutputUnit[];

lib/keywords/contentEncoding.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
import * as Browser from "@hyperjump/browser";
2-
import * as Instance from "../../annotations/annotated-instance.js";
32

43

54
const id = "https://json-schema.org/keyword/contentEncoding";
65

76
const compile = (schema) => Browser.value(schema);
7+
const interpret = () => true;
8+
const annotation = (contentEncoding) => contentEncoding;
89

9-
const interpret = (contentEncoding, instance, { annotations, schemaUri }) => {
10-
annotations.push({
11-
keyword: id,
12-
absoluteKeywordLocation: schemaUri,
13-
instanceLocation: Instance.uri(instance),
14-
annotation: contentEncoding
15-
});
16-
return true;
17-
};
18-
19-
export default { id, compile, interpret };
10+
export default { id, compile, interpret, annotation };

lib/keywords/contentMediaType.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
import * as Browser from "@hyperjump/browser";
2-
import * as Instance from "../../annotations/annotated-instance.js";
32

43

54
const id = "https://json-schema.org/keyword/contentMediaType";
65

76
const compile = (schema) => Browser.value(schema);
7+
const interpret = () => true;
8+
const annotation = (contentMediaType) => contentMediaType;
89

9-
const interpret = (contentMediaType, instance, { annotations, schemaUri }) => {
10-
annotations.push({
11-
keyword: id,
12-
absoluteKeywordLocation: schemaUri,
13-
instanceLocation: Instance.uri(instance),
14-
annotation: contentMediaType
15-
});
16-
return true;
17-
};
18-
19-
export default { id, compile, interpret };
10+
export default { id, compile, interpret, annotation };

lib/keywords/contentSchema.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
import { canonicalUri } from "../schema.js";
2-
import * as Instance from "../../annotations/annotated-instance.js";
32

43

54
const id = "https://json-schema.org/keyword/contentSchema";
65

76
const compile = (contentSchema) => canonicalUri(contentSchema);
7+
const interpret = () => true;
8+
const annotation = (contentSchema) => contentSchema;
89

9-
const interpret = (contentSchema, instance, { annotations, schemaUri }) => {
10-
annotations.push({
11-
keyword: id,
12-
absoluteKeywordLocation: schemaUri,
13-
instanceLocation: Instance.uri(instance),
14-
annotation: contentSchema
15-
});
16-
return true;
17-
};
18-
19-
export default { id, compile, interpret };
10+
export default { id, compile, interpret, annotation };

lib/keywords/default.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
import * as Browser from "@hyperjump/browser";
2-
import * as Instance from "../../annotations/annotated-instance.js";
32

43

54
const id = "https://json-schema.org/keyword/default";
65

76
const compile = (schema) => Browser.value(schema);
7+
const interpret = () => true;
8+
const annotation = (value) => value;
89

9-
const interpret = (value, instance, { annotations, schemaUri }) => {
10-
annotations.push({
11-
keyword: id,
12-
absoluteKeywordLocation: schemaUri,
13-
instanceLocation: Instance.uri(instance),
14-
annotation: value
15-
});
16-
return true;
17-
};
18-
19-
export default { id, compile, interpret };
10+
export default { id, compile, interpret, annotation };

0 commit comments

Comments
 (0)