Skip to content

Commit d1de778

Browse files
committed
Cleanup annotation code I missed from refactor
1 parent 9ee3a89 commit d1de778

File tree

4 files changed

+9
-36
lines changed

4 files changed

+9
-36
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ following functions are available in addition to the functions available in the
800800
* **annotation**: (instance: JsonNode, keyword: string, dialect?: string): any[];
801801
802802
Get the annotations for a keyword for the value represented by the JsonNode.
803-
* **annotatedWith**: (instance: JsonNode, keyword: string, dialect?: string): JsonNode[];
803+
* **annotatedWith**: (instance: JsonNode, keyword: string, dialect?: string): Generator<JsonNode>;
804804
805805
Get all JsonNodes that are annotated with the given keyword.
806806
* **setAnnotation**: (instance: JsonNode, keywordId: string, value: any) => JsonNode

annotations/annotated-instance.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export const annotation: <A>(instance: JsonNode, keyword: string, dialectUri?: string) => A[];
2-
export const annotatedWith: <A extends JsonNode>(instance: A, keyword: string, dialectUri?: string) => A[];
2+
export const annotatedWith: <A extends JsonNode>(instance: A, keyword: string, dialectUri?: string) => Generator<A>;
33

44
export * from "../lib/instance.js";

annotations/annotated-instance.js

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as JsonPointer from "@hyperjump/json-pointer";
21
import * as Instance from "../lib/instance.js";
32
import { getKeywordId } from "../lib/keywords.js";
43

@@ -7,43 +6,15 @@ const defaultDialectId = "https://json-schema.org/validation";
76

87
export const annotation = (node, keyword, dialect = defaultDialectId) => {
98
const keywordUri = getKeywordId(keyword, dialect);
10-
11-
let currentNode = node.root;
12-
const errors = [...invalidSchemas(currentNode)];
13-
for (let segment of JsonPointer.pointerSegments(node.pointer)) {
14-
segment = segment === "-" && Instance.typeOf(currentNode) === "array" ? Instance.length(currentNode) : segment;
15-
currentNode = Instance.step(segment, currentNode);
16-
errors.push(...invalidSchemas(currentNode));
17-
}
18-
19-
const annotations = [];
20-
for (const schemaLocation in node.annotations[keywordUri]) {
21-
if (!errors.some((error) => schemaLocation.startsWith(error))) {
22-
annotations.unshift(node.annotations[keywordUri][schemaLocation]);
23-
}
24-
}
25-
26-
return annotations;
9+
return node.annotations[keywordUri] ?? [];
2710
};
2811

29-
const invalidSchemas = function* (node) {
30-
for (const error in node.errors) {
31-
if (node.errors[error] === "https://json-schema.org/evaluation/validate") {
32-
yield error;
33-
}
34-
}
35-
};
36-
37-
export const annotatedWith = (instance, keyword, dialectId = defaultDialectId) => {
38-
const nodes = [];
39-
12+
export const annotatedWith = function* (instance, keyword, dialectId = defaultDialectId) {
4013
for (const node of Instance.allNodes(instance)) {
4114
if (annotation(node, keyword, dialectId).length > 0) {
42-
nodes.push(node);
15+
yield node;
4316
}
4417
}
45-
46-
return nodes;
4718
};
4819

4920
export * from "../lib/instance.js";

lib/keywords/validation.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,16 @@ const interpret = (url, instance, { ast, dynamicAnchors, errors, annotations, ou
8686
errors.push(...context.errors);
8787
} else {
8888
if (keywordHandler.annotation) {
89-
schemaAnnotations.push({
89+
schemaAnnotations.unshift({
9090
keyword: keywordId,
9191
absoluteKeywordLocation: schemaUri,
9292
instanceLocation: Instance.uri(instance),
9393
annotation: keywordHandler.annotation(keywordValue)
9494
});
9595
}
96-
schemaAnnotations.push(...context.annotations);
96+
for (const contextAnnotation of context.annotations) {
97+
schemaAnnotations.unshift(contextAnnotation);
98+
}
9799
}
98100
break;
99101
case DETAILED: {

0 commit comments

Comments
 (0)