@hyperjump/json-schema 1.7.1 → 1.7.3

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.
@@ -5,8 +5,8 @@ import { getKeywordId } from "../lib/keywords.js";
5
5
 
6
6
  const defaultDialectId = "https://json-schema.org/validation";
7
7
 
8
- export const nil = { ...nilInstance, annotations: {} };
9
- export const cons = (instance, id = undefined) => ({
8
+ export const nil = { ...nilInstance, annotations: {} }; // eslint-disable-line import/export
9
+ export const cons = (instance, id = undefined) => ({ // eslint-disable-line import/export
10
10
  ...nil,
11
11
  id: id ? toAbsoluteIri(id) : "",
12
12
  instance,
@@ -47,4 +47,4 @@ export const annotatedWith = (instance, keyword, dialectId = defaultDialectId) =
47
47
  return instances;
48
48
  };
49
49
 
50
- export * from "../lib/instance.js";
50
+ export * from "../lib/instance.js"; // eslint-disable-line import/export
package/lib/index.js CHANGED
@@ -94,6 +94,7 @@ addKeyword(exclusiveMinimum);
94
94
  addKeyword(format);
95
95
  addKeyword(id);
96
96
  addKeyword(if_);
97
+ addKeyword(itemPattern);
97
98
  addKeyword(items);
98
99
  addKeyword(maxContains);
99
100
  addKeyword(maxItems);
@@ -118,7 +119,6 @@ addKeyword(readOnly);
118
119
  addKeyword(ref);
119
120
  addKeyword(requireAllExcept);
120
121
  addKeyword(required);
121
- addKeyword(itemPattern);
122
122
  addKeyword(title);
123
123
  addKeyword(then);
124
124
  addKeyword(type);
package/lib/instance.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { append as pointerAppend, get as pointerGet } from "@hyperjump/json-pointer";
2
2
  import { toAbsoluteIri } from "@hyperjump/uri";
3
- import { jsonTypeOf } from "./common.js";
3
+ import { jsonTypeOf, uriFragment } from "./common.js";
4
4
  import { Reference } from "@hyperjump/browser/jref";
5
5
 
6
6
 
@@ -17,7 +17,7 @@ export const get = (url, instance = nil) => {
17
17
  throw Error(`No JSON document found at '${url.split("#")[0]}'`);
18
18
  }
19
19
 
20
- const pointer = url.substr(1);
20
+ const pointer = uriFragment(url);
21
21
  return {
22
22
  ...instance,
23
23
  pointer: pointer,
@@ -16,17 +16,31 @@ const interpret = (allOf, instance, ast, dynamicAnchors, quiet) => {
16
16
  };
17
17
 
18
18
  const collectEvaluatedProperties = (allOf, instance, ast, dynamicAnchors) => {
19
- return allOf.reduce((acc, schemaUrl) => {
20
- const propertyNames = acc && Validation.collectEvaluatedProperties(schemaUrl, instance, ast, dynamicAnchors);
21
- return propertyNames !== false && [...acc, ...propertyNames];
22
- }, []);
19
+ const evaluatedPropertyNames = new Set();
20
+ for (const schemaUrl of allOf) {
21
+ const propertyNames = Validation.collectEvaluatedProperties(schemaUrl, instance, ast, dynamicAnchors);
22
+ if (!propertyNames) {
23
+ return false;
24
+ }
25
+
26
+ propertyNames.forEach(evaluatedPropertyNames.add, evaluatedPropertyNames);
27
+ }
28
+
29
+ return evaluatedPropertyNames;
23
30
  };
24
31
 
25
32
  const collectEvaluatedItems = (allOf, instance, ast, dynamicAnchors) => {
26
- return allOf.reduce((acc, schemaUrl) => {
27
- const itemIndexes = acc !== false && Validation.collectEvaluatedItems(schemaUrl, instance, ast, dynamicAnchors);
28
- return itemIndexes !== false && new Set([...acc, ...itemIndexes]);
29
- }, new Set());
33
+ const evaluatedItemIndexes = new Set();
34
+ for (const schemaUrl of allOf) {
35
+ const itemIndexes = Validation.collectEvaluatedItems(schemaUrl, instance, ast, dynamicAnchors);
36
+ if (!itemIndexes) {
37
+ return false;
38
+ }
39
+
40
+ itemIndexes.forEach(evaluatedItemIndexes.add, evaluatedItemIndexes);
41
+ }
42
+
43
+ return evaluatedItemIndexes;
30
44
  };
31
45
 
32
46
  export default { id, compile, interpret, collectEvaluatedProperties, collectEvaluatedItems };
@@ -17,17 +17,29 @@ const interpret = (anyOf, instance, ast, dynamicAnchors, quiet) => {
17
17
  };
18
18
 
19
19
  const collectEvaluatedProperties = (anyOf, instance, ast, dynamicAnchors) => {
20
- return anyOf.reduce((acc, schemaUrl) => {
20
+ let evaluatedPropertyNames = false;
21
+ for (const schemaUrl of anyOf) {
21
22
  const propertyNames = Validation.collectEvaluatedProperties(schemaUrl, instance, ast, dynamicAnchors);
22
- return propertyNames === false ? acc : [...acc || [], ...propertyNames];
23
- }, false);
23
+ if (propertyNames) {
24
+ evaluatedPropertyNames ||= new Set();
25
+ propertyNames.forEach(evaluatedPropertyNames.add, evaluatedPropertyNames);
26
+ }
27
+ }
28
+
29
+ return evaluatedPropertyNames;
24
30
  };
25
31
 
26
32
  const collectEvaluatedItems = (anyOf, instance, ast, dynamicAnchors) => {
27
- return anyOf.reduce((acc, schemaUrl) => {
33
+ let evaluatedItemIndexes = false;
34
+ for (const schemaUrl of anyOf) {
28
35
  const itemIndexes = Validation.collectEvaluatedItems(schemaUrl, instance, ast, dynamicAnchors);
29
- return itemIndexes === false ? acc : new Set([...acc || [], ...itemIndexes]);
30
- }, false);
36
+ if (itemIndexes) {
37
+ evaluatedItemIndexes ||= new Set();
38
+ itemIndexes.forEach(evaluatedItemIndexes.add, evaluatedItemIndexes);
39
+ }
40
+ }
41
+
42
+ return evaluatedItemIndexes;
31
43
  };
32
44
 
33
45
  export default { id, compile, interpret, collectEvaluatedProperties, collectEvaluatedItems };
@@ -27,27 +27,35 @@ const interpret = (oneOf, instance, ast, dynamicAnchors, quiet) => {
27
27
  };
28
28
 
29
29
  const collectEvaluatedProperties = (oneOf, instance, ast, dynamicAnchors) => {
30
- let validCount = 0;
31
- return oneOf.reduce((acc, schemaUrl) => {
32
- if (validCount > 1) {
33
- return false;
30
+ let evaluatedProperties = false;
31
+ for (const schemaUrl of oneOf) {
32
+ const propertyNames = Validation.collectEvaluatedProperties(schemaUrl, instance, ast, dynamicAnchors);
33
+ if (propertyNames) {
34
+ if (evaluatedProperties) {
35
+ return false;
36
+ }
37
+
38
+ evaluatedProperties = propertyNames;
34
39
  }
40
+ }
35
41
 
36
- const propertyNames = Validation.collectEvaluatedProperties(schemaUrl, instance, ast, dynamicAnchors);
37
- return propertyNames ? validCount++ === 0 && propertyNames : acc;
38
- }, false);
42
+ return evaluatedProperties;
39
43
  };
40
44
 
41
45
  const collectEvaluatedItems = (oneOf, instance, ast, dynamicAnchors) => {
42
- let validCount = 0;
43
- return oneOf.reduce((acc, schemaUrl) => {
44
- if (validCount > 1) {
45
- return false;
46
+ let evaluatedItemIndexes = false;
47
+ for (const schemaUrl of oneOf) {
48
+ const itemIndexes = Validation.collectEvaluatedItems(schemaUrl, instance, ast, dynamicAnchors);
49
+ if (itemIndexes) {
50
+ if (evaluatedItemIndexes) {
51
+ return false;
52
+ }
53
+
54
+ evaluatedItemIndexes = itemIndexes;
46
55
  }
56
+ }
47
57
 
48
- const itemIndexes = Validation.collectEvaluatedItems(schemaUrl, instance, ast, dynamicAnchors);
49
- return itemIndexes ? validCount++ === 0 && itemIndexes : acc;
50
- }, false);
58
+ return evaluatedItemIndexes;
51
59
  };
52
60
 
53
61
  export default { id, compile, interpret, collectEvaluatedProperties, collectEvaluatedItems };
@@ -30,17 +30,22 @@ const interpret = (propertyDependencies, instance, ast, dynamicAnchors, quiet) =
30
30
  };
31
31
 
32
32
  const collectEvaluatedProperties = (propertyDependencies, instance, ast, dynamicAnchors) => {
33
- return Object.entries(propertyDependencies)
34
- .reduce((acc, [propertyName, valueMappings]) => {
35
- const propertyValue = Instance.value(instance)[propertyName];
36
-
37
- if (Instance.has(propertyName, instance) && propertyValue in valueMappings) {
38
- const propertyNames = Validation.collectEvaluatedProperties(valueMappings[propertyValue], instance, ast, dynamicAnchors);
39
- return propertyNames !== false && acc.concat(propertyNames);
40
- } else {
41
- return acc;
33
+ const evaluatedPropertyNames = new Set();
34
+ for (const propertyName in propertyDependencies) {
35
+ const propertyValue = Instance.value(instance)[propertyName];
36
+
37
+ const valueMappings = propertyDependencies[propertyName];
38
+ if (Instance.has(propertyName, instance) && propertyValue in valueMappings) {
39
+ const propertyNames = Validation.collectEvaluatedProperties(valueMappings[propertyValue], instance, ast, dynamicAnchors);
40
+ if (!propertyNames) {
41
+ return false;
42
42
  }
43
- }, []);
43
+
44
+ propertyNames.forEach(evaluatedPropertyNames.add, evaluatedPropertyNames);
45
+ }
46
+ }
47
+
48
+ return evaluatedPropertyNames;
44
49
  };
45
50
 
46
51
  export default { id, compile, interpret, collectEvaluatedProperties };
@@ -77,7 +77,7 @@ const interpret = (url, instance, ast, dynamicAnchors, quiet = false) => {
77
77
  const emptyPropertyNames = new Set();
78
78
  const collectEvaluatedProperties = (url, instance, ast, dynamicAnchors, isTop = false) => {
79
79
  if (typeof ast[url] === "boolean") {
80
- return ast[url] ? [] : false;
80
+ return ast[url] ? new Set() : false;
81
81
  }
82
82
 
83
83
  const accumulatedPropertyNames = new Set();
@@ -95,7 +95,7 @@ const collectEvaluatedProperties = (url, instance, ast, dynamicAnchors, isTop =
95
95
  return false;
96
96
  }
97
97
 
98
- propertyNames.forEach(Set.prototype.add.bind(accumulatedPropertyNames));
98
+ propertyNames.forEach(accumulatedPropertyNames.add, accumulatedPropertyNames);
99
99
  }
100
100
 
101
101
  return accumulatedPropertyNames;
@@ -122,7 +122,7 @@ const collectEvaluatedItems = (url, instance, ast, dynamicAnchors, isTop = false
122
122
  return false;
123
123
  }
124
124
 
125
- itemIndexes.forEach(Set.prototype.add.bind(accumulatedItemIndexes));
125
+ itemIndexes.forEach(accumulatedItemIndexes.add, accumulatedItemIndexes);
126
126
  }
127
127
 
128
128
  return accumulatedItemIndexes;
package/lib/schema.js CHANGED
@@ -14,20 +14,18 @@ export const schemaPlugin = {
14
14
 
15
15
  return buildSchemaDocument(await response.json(), response.url, contextDialectId);
16
16
  },
17
- fileMatcher: (path) => path.endsWith(".schema.json")
17
+ fileMatcher: (path) => /(\.|\/)schema\.json$/.test(path)
18
18
  };
19
19
 
20
20
  const schemaRegistry = {};
21
21
 
22
22
  export const getSchema = async (uri, browser = undefined) => {
23
23
  if (!browser) {
24
- const cache = {};
24
+ browser = { _cache: {} };
25
25
 
26
26
  for (const uri in schemaRegistry) {
27
- cache[uri] = schemaRegistry[uri];
27
+ browser._cache[uri] = schemaRegistry[uri];
28
28
  }
29
-
30
- browser = { _cache: cache };
31
29
  }
32
30
 
33
31
  const schema = await browserGet(uri, browser);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyperjump/json-schema",
3
- "version": "1.7.1",
3
+ "version": "1.7.3",
4
4
  "description": "A JSON Schema validator with support for custom keywords, vocabularies, and dialects",
5
5
  "type": "module",
6
6
  "main": "./stable/index.js",
@@ -22,7 +22,8 @@
22
22
  "scripts": {
23
23
  "clean": "xargs -a .gitignore rm -rf",
24
24
  "lint": "eslint lib stable draft-* openapi-* bundle annotations",
25
- "test": "vitest --watch=false"
25
+ "test": "vitest --watch=false",
26
+ "check-types": "tsc --noEmit"
26
27
  },
27
28
  "repository": {
28
29
  "type": "git",