@hyperjump/json-schema 1.2.3 → 1.2.4
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.
- package/lib/core.js +21 -12
- package/lib/fetch.js +1 -1
- package/lib/index.js +2 -0
- package/lib/keywords/meta-data.js +4 -1
- package/lib/keywords/unknown.js +4 -0
- package/lib/keywords/validation.js +6 -3
- package/lib/keywords.js +5 -13
- package/lib/media-types.d.ts +1 -0
- package/package.json +2 -3
package/lib/core.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import curry from "just-curry-it";
|
|
2
|
-
import
|
|
3
|
-
import
|
|
2
|
+
import { subscribe, unsubscribe } from "./pubsub.js";
|
|
3
|
+
import {
|
|
4
|
+
setMetaSchemaOutputFormat,
|
|
5
|
+
getShouldValidateSchema,
|
|
6
|
+
isExperimentalKeywordEnabled,
|
|
7
|
+
setExperimentalKeywordEnabled,
|
|
8
|
+
getMetaSchemaOutputFormat
|
|
9
|
+
} from "./configuration.js";
|
|
4
10
|
import * as Instance from "./instance.js";
|
|
5
11
|
import { InvalidSchemaError } from "./invalid-schema-error.js";
|
|
6
12
|
import * as Schema from "./schema.js";
|
|
@@ -8,7 +14,7 @@ import Validation from "./keywords/validation.js";
|
|
|
8
14
|
|
|
9
15
|
|
|
10
16
|
export const FLAG = "FLAG", BASIC = "BASIC", DETAILED = "DETAILED", VERBOSE = "VERBOSE";
|
|
11
|
-
|
|
17
|
+
setMetaSchemaOutputFormat(FLAG);
|
|
12
18
|
|
|
13
19
|
export const validate = async (url, value = undefined, outputFormat = undefined) => {
|
|
14
20
|
const compiled = await compile(url);
|
|
@@ -30,9 +36,12 @@ export const interpret = curry(({ ast, schemaUri }, value, outputFormat = FLAG)
|
|
|
30
36
|
}
|
|
31
37
|
|
|
32
38
|
const output = [];
|
|
33
|
-
const subscriptionToken =
|
|
34
|
-
|
|
35
|
-
|
|
39
|
+
const subscriptionToken = subscribe("result", outputHandler(outputFormat, output));
|
|
40
|
+
try {
|
|
41
|
+
Validation.interpret(schemaUri, value, ast, {});
|
|
42
|
+
} finally {
|
|
43
|
+
unsubscribe("result", subscriptionToken);
|
|
44
|
+
}
|
|
36
45
|
|
|
37
46
|
return output[0];
|
|
38
47
|
});
|
|
@@ -70,26 +79,26 @@ const outputHandler = (outputFormat, output) => {
|
|
|
70
79
|
};
|
|
71
80
|
|
|
72
81
|
const metaValidators = {};
|
|
73
|
-
|
|
74
|
-
if (
|
|
82
|
+
subscribe("validate.metaValidate", async (message, schema) => {
|
|
83
|
+
if (getShouldValidateSchema() && !schema.validated) {
|
|
75
84
|
Schema.markValidated(schema.id);
|
|
76
85
|
|
|
77
86
|
// Compile
|
|
78
87
|
if (!(schema.dialectId in metaValidators)) {
|
|
79
88
|
// Dynamic references are experimental, but are necessary for meta-validation
|
|
80
89
|
const dyanmicRefKeywordId = "https://json-schema.org/keyword/dynamicRef";
|
|
81
|
-
const isDynamicRefEnabled =
|
|
82
|
-
|
|
90
|
+
const isDynamicRefEnabled = isExperimentalKeywordEnabled(dyanmicRefKeywordId);
|
|
91
|
+
setExperimentalKeywordEnabled(dyanmicRefKeywordId, true);
|
|
83
92
|
|
|
84
93
|
const compiledSchema = await compile(schema.dialectId);
|
|
85
94
|
metaValidators[schema.dialectId] = interpret(compiledSchema);
|
|
86
95
|
|
|
87
|
-
|
|
96
|
+
setExperimentalKeywordEnabled(dyanmicRefKeywordId, isDynamicRefEnabled);
|
|
88
97
|
}
|
|
89
98
|
|
|
90
99
|
// Interpret
|
|
91
100
|
const schemaInstance = Instance.cons(schema.schema, schema.id);
|
|
92
|
-
const metaResults = metaValidators[schema.dialectId](schemaInstance,
|
|
101
|
+
const metaResults = metaValidators[schema.dialectId](schemaInstance, getMetaSchemaOutputFormat());
|
|
93
102
|
if (!metaResults.valid) {
|
|
94
103
|
throw new InvalidSchemaError(metaResults);
|
|
95
104
|
}
|
package/lib/fetch.js
CHANGED
package/lib/index.js
CHANGED
|
@@ -57,6 +57,7 @@ import type from "./keywords/type.js";
|
|
|
57
57
|
import unevaluatedItems from "./keywords/unevaluatedItems.js";
|
|
58
58
|
import unevaluatedProperties from "./keywords/unevaluatedProperties.js";
|
|
59
59
|
import uniqueItems from "./keywords/uniqueItems.js";
|
|
60
|
+
import unknown from "./keywords/unknown.js";
|
|
60
61
|
import vocabulary from "./keywords/vocabulary.js";
|
|
61
62
|
import writeOnly from "./keywords/writeOnly.js";
|
|
62
63
|
|
|
@@ -125,6 +126,7 @@ addKeyword(type);
|
|
|
125
126
|
addKeyword(unevaluatedItems);
|
|
126
127
|
addKeyword(unevaluatedProperties);
|
|
127
128
|
addKeyword(uniqueItems);
|
|
129
|
+
addKeyword(unknown);
|
|
128
130
|
addKeyword(vocabulary);
|
|
129
131
|
addKeyword(writeOnly);
|
|
130
132
|
|
|
@@ -27,7 +27,7 @@ const compile = async (schema, ast) => {
|
|
|
27
27
|
|
|
28
28
|
const schemaValue = Schema.value(schema);
|
|
29
29
|
if (!["object", "boolean"].includes(typeof schemaValue)) {
|
|
30
|
-
throw Error(`No schema found at '${
|
|
30
|
+
throw Error(`No schema found at '${url}'`);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
ast[url] = [
|
|
@@ -38,12 +38,15 @@ const compile = async (schema, ast) => {
|
|
|
38
38
|
Pact.map(async ([keyword, keywordSchema]) => {
|
|
39
39
|
const keywordId = getKeywordId(schema.dialectId, keyword);
|
|
40
40
|
if (!keywordId) {
|
|
41
|
-
throw Error(`Encountered unknown keyword '${keyword}' at ${
|
|
41
|
+
throw Error(`Encountered unknown keyword '${keyword}' at ${url}`);
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
const keywordHandler = getKeyword(keywordId);
|
|
45
|
+
if (!keywordHandler) {
|
|
46
|
+
throw Error(`Encountered unsupported keyword ${keyword} at '${url}'. You can provide an implementation for the '${keywordId}' keyword using the 'addKeyword' function.`);
|
|
47
|
+
}
|
|
45
48
|
if (keywordHandler.experimental && !isExperimentalKeywordEnabled(keywordId)) {
|
|
46
|
-
throw Error(`Encountered experimental keyword ${keyword} at '${
|
|
49
|
+
throw Error(`Encountered experimental keyword ${keyword} at '${url}'. You can enable this keyword with: setExperimentalKeywordEnabled('${keywordId}', true)`);
|
|
47
50
|
}
|
|
48
51
|
|
|
49
52
|
const keywordAst = await keywordHandler.compile(keywordSchema, ast, schema);
|
package/lib/keywords.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
import metaData from "./keywords/meta-data.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
1
|
const _keywords = {};
|
|
5
|
-
export const getKeyword = (id) => _keywords[id]
|
|
2
|
+
export const getKeyword = (id) => _keywords[id];
|
|
6
3
|
|
|
7
4
|
export const addKeyword = (keywordHandler) => {
|
|
8
5
|
_keywords[keywordHandler.id] = keywordHandler;
|
|
@@ -16,7 +13,7 @@ export const defineVocabulary = (id, keywords) => {
|
|
|
16
13
|
const _dialects = {};
|
|
17
14
|
const _allowUnknownKeywords = {};
|
|
18
15
|
export const getKeywordId = (dialectId, keyword) => _dialects[dialectId]?.[keyword]
|
|
19
|
-
|| _allowUnknownKeywords[dialectId] &&
|
|
16
|
+
|| _allowUnknownKeywords[dialectId] && "https://json-schema.org/keyword/unknown";
|
|
20
17
|
export const getKeywordName = (dialectId, keywordId) => {
|
|
21
18
|
for (const keyword in _dialects[dialectId]) {
|
|
22
19
|
if (_dialects[dialectId][keyword] === keywordId) {
|
|
@@ -36,14 +33,9 @@ export const loadDialect = (dialectId, dialect, allowUnknownKeywords = false) =>
|
|
|
36
33
|
if (vocabularyId in _vocabularies) {
|
|
37
34
|
Object.entries(_vocabularies[vocabularyId])
|
|
38
35
|
.forEach(([keyword, keywordId]) => {
|
|
39
|
-
if (!(keywordId in _keywords)) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
throw Error(`The '${keywordId}' keyword is not supported. This keyword was included in the '${vocabularyId}' vocabulary which is required by the '${dialectId}' dialect.`);
|
|
43
|
-
} else {
|
|
44
|
-
// Allow keyword to be ignored
|
|
45
|
-
keywordId = `https://json-schema.org/keyword/unknown#${keyword}`;
|
|
46
|
-
}
|
|
36
|
+
if (!(keywordId in _keywords) && !isRequired) {
|
|
37
|
+
// Allow keyword to be ignored
|
|
38
|
+
keywordId = "https://json-schema.org/keyword/unknown";
|
|
47
39
|
}
|
|
48
40
|
_dialects[dialectId][keyword] = keywordId;
|
|
49
41
|
});
|
package/lib/media-types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyperjump/json-schema",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.4",
|
|
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",
|
|
@@ -56,7 +56,6 @@
|
|
|
56
56
|
"eslint-plugin-import": "*",
|
|
57
57
|
"json-schema-test-suite": "github:json-schema-org/JSON-Schema-Test-Suite",
|
|
58
58
|
"mocha": "*",
|
|
59
|
-
"nock": "*",
|
|
60
59
|
"ts-node": "*",
|
|
61
60
|
"typescript": "*",
|
|
62
61
|
"yaml": "*"
|
|
@@ -67,7 +66,7 @@
|
|
|
67
66
|
"@hyperjump/uri": "^1.0.0",
|
|
68
67
|
"content-type": "^1.0.4",
|
|
69
68
|
"fastest-stable-stringify": "^2.0.2",
|
|
70
|
-
"
|
|
69
|
+
"undici": "^5.19.1",
|
|
71
70
|
"uuid": "^9.0.0"
|
|
72
71
|
},
|
|
73
72
|
"engines": {
|