json-schema-library 5.3.0 → 7.0.0
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/.editorconfig +1 -0
- package/.prettierignore +1 -0
- package/.prettierrc +7 -0
- package/CHANGELOG.md +106 -0
- package/README.md +811 -199
- package/TASKS.md +3 -81
- package/dist/index.d.ts +29 -71
- package/dist/jsonSchemaLibrary.js +1 -1
- package/dist/lib/SchemaService.d.ts +6 -8
- package/dist/lib/{addSchema.d.ts → addRemoteSchema.d.ts} +2 -1
- package/dist/lib/addValidator.d.ts +3 -2
- package/dist/lib/compile/getRef.d.ts +2 -1
- package/dist/lib/compile/index.d.ts +15 -1
- package/dist/lib/compile/types.d.ts +5 -0
- package/dist/lib/config/strings.d.ts +1 -39
- package/dist/lib/draft/index.d.ts +132 -0
- package/dist/lib/draft04/index.d.ts +7 -0
- package/dist/lib/draft06/compile/index.d.ts +16 -0
- package/dist/lib/draft06/index.d.ts +7 -0
- package/dist/lib/draft06/validation/keyword.d.ts +3 -0
- package/dist/lib/draft06/validation/type.d.ts +10 -0
- package/dist/lib/draft06/validation/typeKeywordMapping.d.ts +13 -0
- package/dist/lib/draft07/index.d.ts +7 -0
- package/dist/lib/each.d.ts +3 -2
- package/dist/lib/eachSchema.d.ts +2 -4
- package/dist/lib/getChildSchemaSelection.d.ts +7 -5
- package/dist/lib/getSchema.d.ts +2 -2
- package/dist/lib/getTemplate.d.ts +1 -1
- package/dist/lib/getTypeOf.d.ts +2 -1
- package/dist/lib/isValid.d.ts +1 -1
- package/dist/lib/jsoneditor/index.d.ts +7 -0
- package/dist/lib/resolveAllOf.d.ts +1 -1
- package/dist/lib/resolveAnyOf.d.ts +1 -1
- package/dist/lib/resolveOneOf.fuzzy.d.ts +1 -1
- package/dist/lib/resolveOneOf.strict.d.ts +2 -2
- package/dist/lib/{resolveRef.withOverwrite.d.ts → resolveRef.merge.d.ts} +0 -0
- package/dist/lib/schema/getTypeDefs.d.ts +6 -4
- package/dist/lib/schema/getTypeId.d.ts +1 -1
- package/dist/lib/schema/types.d.ts +7 -57
- package/dist/lib/step.d.ts +5 -5
- package/dist/lib/types.d.ts +11 -3
- package/dist/lib/utils/createCustomError.d.ts +8 -11
- package/dist/lib/utils/filter.d.ts +4 -4
- package/dist/lib/utils/flattenArray.d.ts +1 -1
- package/dist/lib/utils/merge.d.ts +3 -0
- package/dist/lib/utils/punycode.ucs2decode.d.ts +1 -1
- package/dist/lib/validate.d.ts +2 -2
- package/dist/lib/validateAsync.d.ts +1 -1
- package/dist/lib/validation/errors.d.ts +1 -3
- package/dist/lib/validation/format.d.ts +4 -11
- package/dist/lib/validation/keyword.d.ts +2 -27
- package/dist/lib/validation/type.d.ts +3 -10
- package/dist/lib/validation/typeKeywordMapping.d.ts +4 -4
- package/dist/module/index.js +23 -32
- package/dist/module/lib/SchemaService.js +7 -4
- package/dist/module/lib/{addSchema.js → addRemoteSchema.js} +2 -4
- package/dist/module/lib/addValidator.js +3 -4
- package/dist/module/lib/compile/getRef.js +1 -1
- package/dist/module/lib/compile/index.js +43 -18
- package/dist/module/lib/compile/types.js +1 -0
- package/dist/module/lib/config/strings.js +15 -2
- package/dist/module/lib/createSchemaOf.js +1 -1
- package/dist/module/lib/draft/index.js +133 -0
- package/dist/module/lib/draft04/index.js +90 -0
- package/dist/module/lib/draft06/compile/index.js +77 -0
- package/dist/module/lib/draft06/index.js +96 -0
- package/dist/module/lib/draft06/validation/keyword.js +168 -0
- package/dist/module/lib/draft06/validation/type.js +31 -0
- package/dist/module/lib/draft06/validation/typeKeywordMapping.js +15 -0
- package/dist/module/lib/draft07/index.js +96 -0
- package/dist/module/lib/each.js +2 -2
- package/dist/module/lib/eachSchema.js +28 -19
- package/dist/module/lib/getChildSchemaSelection.js +7 -6
- package/dist/module/lib/getSchema.js +4 -2
- package/dist/module/lib/getTemplate.js +42 -15
- package/dist/module/lib/jsoneditor/index.js +16 -0
- package/dist/module/lib/resolveAllOf.js +3 -4
- package/dist/module/lib/resolveOneOf.fuzzy.js +13 -3
- package/dist/module/lib/resolveOneOf.strict.js +49 -2
- package/dist/module/lib/{resolveRef.withOverwrite.js → resolveRef.merge.js} +0 -0
- package/dist/module/lib/resolveRef.strict.js +8 -0
- package/dist/module/lib/schema/getTypeDefs.js +14 -3
- package/dist/module/lib/schema/getTypeId.js +10 -6
- package/dist/module/lib/schema/types.js +33 -9
- package/dist/module/lib/step.js +67 -13
- package/dist/module/lib/types.js +7 -1
- package/dist/module/lib/utils/createCustomError.js +4 -4
- package/dist/module/lib/utils/filter.js +3 -5
- package/dist/module/lib/utils/flattenArray.js +4 -3
- package/dist/module/lib/utils/merge.js +4 -0
- package/dist/module/lib/utils/punycode.ucs2decode.js +4 -3
- package/dist/module/lib/validate.js +34 -8
- package/dist/module/lib/validateAsync.js +7 -7
- package/dist/module/lib/validation/errors.js +16 -3
- package/dist/module/lib/validation/format.js +115 -8
- package/dist/module/lib/validation/keyword.js +79 -32
- package/dist/module/lib/validation/type.js +2 -1
- package/index.ts +46 -32
- package/lib/SchemaService.ts +18 -11
- package/lib/{addSchema.ts → addRemoteSchema.ts} +3 -5
- package/lib/addValidator.ts +15 -12
- package/lib/compile/getRef.ts +3 -4
- package/lib/compile/index.ts +65 -19
- package/lib/compile/types.ts +6 -0
- package/lib/config/strings.ts +17 -3
- package/lib/createSchemaOf.ts +1 -3
- package/lib/draft/index.ts +201 -0
- package/lib/draft04/index.ts +95 -0
- package/lib/draft06/compile/index.ts +104 -0
- package/lib/draft06/index.ts +101 -0
- package/lib/draft06/validation/keyword.ts +199 -0
- package/lib/draft06/validation/type.ts +47 -0
- package/lib/draft06/validation/typeKeywordMapping.ts +15 -0
- package/lib/draft07/index.ts +101 -0
- package/lib/each.ts +11 -4
- package/lib/eachSchema.ts +45 -32
- package/lib/getChildSchemaSelection.ts +14 -7
- package/lib/getSchema.ts +18 -9
- package/lib/getTemplate.ts +155 -42
- package/lib/getTypeOf.ts +2 -1
- package/lib/isValid.ts +7 -3
- package/lib/jsoneditor/index.ts +20 -0
- package/lib/resolveAllOf.ts +10 -6
- package/lib/resolveAnyOf.ts +7 -3
- package/lib/resolveOneOf.fuzzy.ts +26 -9
- package/lib/resolveOneOf.strict.ts +63 -5
- package/lib/{resolveRef.withOverwrite.ts → resolveRef.merge.ts} +0 -0
- package/lib/resolveRef.strict.ts +9 -0
- package/lib/schema/getTypeDefs.ts +19 -7
- package/lib/schema/getTypeId.ts +11 -8
- package/lib/schema/types.ts +41 -9
- package/lib/step.ts +109 -25
- package/lib/types.ts +23 -5
- package/lib/utils/createCustomError.ts +8 -13
- package/lib/utils/filter.ts +7 -9
- package/lib/utils/flattenArray.ts +5 -4
- package/lib/utils/merge.ts +5 -0
- package/lib/utils/punycode.ucs2decode.ts +6 -5
- package/lib/validate.ts +47 -16
- package/lib/validateAsync.ts +15 -14
- package/lib/validation/errors.ts +17 -6
- package/lib/validation/format.ts +147 -13
- package/lib/validation/keyword.ts +172 -103
- package/lib/validation/type.ts +5 -1
- package/package.json +75 -63
- package/{dist/module/remotes/draft04.json → remotes/draft06.json} +47 -42
- package/remotes/draft07.json +172 -0
- package/remotes/draft2019-09.json +86 -0
- package/tsconfig.json +3 -10
- package/dist/lib/cores/CoreInterface.d.ts +0 -23
- package/dist/lib/cores/Draft04.d.ts +0 -13
- package/dist/lib/cores/JsonEditor.d.ts +0 -13
- package/dist/module/lib/cores/CoreInterface.js +0 -51
- package/dist/module/lib/cores/Draft04.js +0 -52
- package/dist/module/lib/cores/JsonEditor.js +0 -51
- package/dist/module/remotes/index.js +0 -10
- package/dist/remotes/index.d.ts +0 -5
- package/lib/cores/CoreInterface.ts +0 -76
- package/lib/cores/Draft04.ts +0 -66
- package/lib/cores/JsonEditor.ts +0 -64
- package/remotes/index.ts +0 -11
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* or seven in case of ajv https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md#type
|
|
6
6
|
* 1 null, 2 boolean, 3 object, 4 array, 5 number, 6 string (7 integer)
|
|
7
7
|
*/
|
|
8
|
-
|
|
8
|
+
const typeValidators = {
|
|
9
9
|
array: (core, schema, value, pointer) => core.typeKeywords.array
|
|
10
10
|
.filter(key => schema && schema[key] != null)
|
|
11
11
|
.map(key => core.validateKeyword[key](core, schema, value, pointer)),
|
|
@@ -28,3 +28,4 @@ export default {
|
|
|
28
28
|
.filter(key => schema && schema[key] != null)
|
|
29
29
|
.map(key => core.validateKeyword[key](core, schema, value, pointer))
|
|
30
30
|
};
|
|
31
|
+
export default typeValidators;
|
package/index.ts
CHANGED
|
@@ -1,49 +1,63 @@
|
|
|
1
|
-
import
|
|
2
|
-
import Interface from "./lib/cores/CoreInterface";
|
|
3
|
-
import Draft04 from "./lib/cores/Draft04";
|
|
4
|
-
import JsonEditor from "./lib/cores/JsonEditor";
|
|
5
|
-
import addSchema from "./lib/addSchema";
|
|
6
|
-
import addValidator from "./lib/addValidator";
|
|
7
|
-
import compileSchema from "./lib/compileSchema";
|
|
8
|
-
import createCustomError from "./lib/utils/createCustomError";
|
|
9
|
-
import createSchemaOf from "./lib/createSchemaOf";
|
|
10
|
-
import each from "./lib/each";
|
|
11
|
-
import eachSchema from "./lib/eachSchema";
|
|
12
|
-
import getChildSchemaSelection from "./lib/getChildSchemaSelection";
|
|
13
|
-
import getSchema from "./lib/getSchema";
|
|
14
|
-
import getTemplate from "./lib/getTemplate";
|
|
1
|
+
import { createError, createCustomError } from "./lib/utils/createCustomError";
|
|
15
2
|
import getTypeOf from "./lib/getTypeOf";
|
|
16
|
-
import
|
|
3
|
+
import resolveOneOf from "./lib/resolveOneOf.strict";
|
|
4
|
+
import resolveOneOfFuzzy from "./lib/resolveOneOf.fuzzy";
|
|
5
|
+
import resolveRef from "./lib/resolveRef.strict";
|
|
6
|
+
import resolveRefMerge from "./lib/resolveRef.merge";
|
|
17
7
|
import SchemaService from "./lib/SchemaService";
|
|
18
|
-
import step from "./lib/step";
|
|
19
|
-
import validate from "./lib/validate";
|
|
20
|
-
import validateAsync from "./lib/validateAsync";
|
|
21
8
|
import settings from "./lib/config/settings";
|
|
9
|
+
import strings from "./lib/config/strings";
|
|
10
|
+
import validateAsync from "./lib/validateAsync";
|
|
11
|
+
import render from "./lib/utils/render";
|
|
12
|
+
import { Draft } from "./lib/draft";
|
|
13
|
+
import { Draft04, draft04Config } from "./lib/draft04";
|
|
14
|
+
import { Draft06, draft06Config } from "./lib/draft06";
|
|
15
|
+
import { Draft07, draft07Config } from "./lib/draft07";
|
|
16
|
+
import { JsonEditor, draftJsonEditorConfig } from "./lib/jsoneditor";
|
|
17
|
+
import { isJSONError } from "./lib/types";
|
|
22
18
|
|
|
23
19
|
const config = { strings };
|
|
24
20
|
|
|
25
21
|
export {
|
|
26
22
|
config,
|
|
27
|
-
|
|
23
|
+
Draft,
|
|
28
24
|
Draft04, // core implementing draft04 specs
|
|
25
|
+
draft04Config, // config implementing draft04 specs
|
|
26
|
+
Draft06, // core implementing draft06 specs
|
|
27
|
+
draft06Config, // config implementing draft06 specs
|
|
28
|
+
Draft07, // core implementing draft07 specs
|
|
29
|
+
draft07Config, // config implementing draft07 specs
|
|
29
30
|
JsonEditor, // adjusted core of draft04 to better support the json-editor
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
compileSchema,
|
|
31
|
+
draftJsonEditorConfig, // adjusted config of draft04 to better support the json-editor
|
|
32
|
+
createError,
|
|
33
33
|
createCustomError,
|
|
34
|
-
createSchemaOf, // creates a simple schema based on the given data
|
|
35
|
-
each, // iterate over data, receiving each data-entry with its schema
|
|
36
|
-
eachSchema, // iterates over a json-schemas type definitions
|
|
37
|
-
getChildSchemaSelection, // get available child schemas
|
|
38
|
-
getSchema, // get schema of datapointer
|
|
39
|
-
getTemplate, // create data which is valid to the given schema
|
|
40
34
|
getTypeOf, // returns the javascript datatype
|
|
41
|
-
|
|
35
|
+
isJSONError,
|
|
36
|
+
render,
|
|
37
|
+
resolveRef,
|
|
38
|
+
resolveRefMerge,
|
|
39
|
+
resolveOneOf,
|
|
40
|
+
resolveOneOfFuzzy,
|
|
42
41
|
settings,
|
|
43
42
|
SchemaService,
|
|
44
|
-
step, // steps into a json-schema, returning the matching child-schema
|
|
45
|
-
validate, // validates data by a schema
|
|
46
43
|
validateAsync // async validation of data by a schema
|
|
47
44
|
};
|
|
48
45
|
|
|
49
|
-
|
|
46
|
+
import { DraftConfig } from "./lib/draft";
|
|
47
|
+
import { EachCallback } from "./lib/each";
|
|
48
|
+
import { EachSchemaCallback } from "./lib/eachSchema";
|
|
49
|
+
import { ErrorData, CreateError } from "./lib/utils/createCustomError";
|
|
50
|
+
import { JSONSchema, JSONPointer, JSONError, JSONValidator, JSONTypeValidator } from "./lib/types";
|
|
51
|
+
|
|
52
|
+
export type {
|
|
53
|
+
CreateError,
|
|
54
|
+
DraftConfig,
|
|
55
|
+
EachCallback,
|
|
56
|
+
EachSchemaCallback,
|
|
57
|
+
ErrorData,
|
|
58
|
+
JSONError,
|
|
59
|
+
JSONPointer,
|
|
60
|
+
JSONSchema,
|
|
61
|
+
JSONTypeValidator,
|
|
62
|
+
JSONValidator
|
|
63
|
+
};
|
package/lib/SchemaService.ts
CHANGED
|
@@ -1,24 +1,23 @@
|
|
|
1
1
|
import getSchema from "./getSchema";
|
|
2
|
-
import Core from "./
|
|
2
|
+
import { JsonEditor as Core } from "./jsoneditor";
|
|
3
3
|
import gp from "gson-pointer";
|
|
4
4
|
import copy from "./utils/copy";
|
|
5
5
|
import { JSONSchema, JSONPointer } from "./types";
|
|
6
6
|
|
|
7
|
-
|
|
8
7
|
export default class SchemaService {
|
|
9
8
|
core: Core;
|
|
10
9
|
schema: JSONSchema;
|
|
11
|
-
data:
|
|
12
|
-
cache:
|
|
10
|
+
data: unknown;
|
|
11
|
+
cache: Record<string, JSONSchema>;
|
|
13
12
|
|
|
14
|
-
constructor(schema: JSONSchema, data) {
|
|
13
|
+
constructor(schema: JSONSchema, data: unknown) {
|
|
15
14
|
this.core = new Core(schema);
|
|
16
15
|
this.schema = schema;
|
|
17
16
|
this.data = data;
|
|
18
17
|
this.cache = {};
|
|
19
18
|
}
|
|
20
19
|
|
|
21
|
-
updateData(data:
|
|
20
|
+
updateData(data: unknown) {
|
|
22
21
|
this.data = data;
|
|
23
22
|
this.cache = {};
|
|
24
23
|
}
|
|
@@ -29,17 +28,20 @@ export default class SchemaService {
|
|
|
29
28
|
this.cache = {};
|
|
30
29
|
}
|
|
31
30
|
|
|
32
|
-
get(pointer: JSONPointer, data:
|
|
33
|
-
if (data) {
|
|
31
|
+
get(pointer: JSONPointer, data: unknown): JSONSchema {
|
|
32
|
+
if (data) {
|
|
33
|
+
// possibly separate entry point
|
|
34
34
|
const schema = getSchema(this.core, pointer, data, this.schema);
|
|
35
35
|
return copy(schema);
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
if (pointer === "#") {
|
|
38
|
+
if (pointer === "#") {
|
|
39
|
+
// root
|
|
39
40
|
return this.schema;
|
|
40
41
|
}
|
|
41
42
|
|
|
42
|
-
if (this.cache[pointer]) {
|
|
43
|
+
if (this.cache[pointer]) {
|
|
44
|
+
// return cached result
|
|
43
45
|
return this.cache[pointer];
|
|
44
46
|
}
|
|
45
47
|
|
|
@@ -55,7 +57,12 @@ export default class SchemaService {
|
|
|
55
57
|
|
|
56
58
|
// step from parent to child
|
|
57
59
|
const key = gp.split(pointer).pop();
|
|
58
|
-
let schema = getSchema(
|
|
60
|
+
let schema = getSchema(
|
|
61
|
+
this.core,
|
|
62
|
+
key,
|
|
63
|
+
gp.get(this.data, parentPointer),
|
|
64
|
+
this.cache[parentPointer]
|
|
65
|
+
);
|
|
59
66
|
schema = copy(schema);
|
|
60
67
|
if (schema.variableSchema !== true) {
|
|
61
68
|
this.cache[pointer] = schema;
|
|
@@ -1,14 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
import compileSchema from "./compileSchema";
|
|
1
|
+
import { Draft } from "./draft";
|
|
3
2
|
import { JSONSchema } from "./types";
|
|
4
3
|
|
|
5
|
-
|
|
6
4
|
/**
|
|
7
5
|
* register a json-schema to be referenced from another json-schema
|
|
8
6
|
* @param url base-url of json-schema (aka id)
|
|
9
7
|
* @param schema
|
|
10
8
|
*/
|
|
11
|
-
export default function
|
|
9
|
+
export default function addRemoteSchema(draft: Draft, url: string, schema: JSONSchema): void {
|
|
12
10
|
schema.id = schema.id || url;
|
|
13
|
-
remotes[url] = compileSchema(schema);
|
|
11
|
+
draft.remotes[url] = draft.compileSchema(schema);
|
|
14
12
|
}
|
package/lib/addValidator.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import Core from "./
|
|
1
|
+
import { Draft as Core } from "./draft";
|
|
2
2
|
import { JSONValidator } from "./types";
|
|
3
|
-
|
|
3
|
+
import { CreateError } from "./utils/createCustomError";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* @throws Error
|
|
@@ -10,14 +10,15 @@ import { JSONValidator } from "./types";
|
|
|
10
10
|
* @param errorId id of error @see /lib/validation/errors
|
|
11
11
|
* @param errorCreator - function returning an error-object @see /lib/utils/createCustomError
|
|
12
12
|
*/
|
|
13
|
-
function addError(core: Core, errorId: string, errorCreator) {
|
|
13
|
+
function addError(core: Core, errorId: string, errorCreator: CreateError) {
|
|
14
14
|
if (typeof errorCreator !== "function") {
|
|
15
|
-
throw new Error(
|
|
15
|
+
throw new Error(
|
|
16
|
+
`Error callback 'errorCreator' must be of type function. Received '${typeof errorCreator}'`
|
|
17
|
+
);
|
|
16
18
|
}
|
|
17
19
|
core.errors[errorId] = errorCreator;
|
|
18
20
|
}
|
|
19
21
|
|
|
20
|
-
|
|
21
22
|
/**
|
|
22
23
|
* Adds a custom format validator. Existing format may not be overriden (may still be modified manually)
|
|
23
24
|
* @param core
|
|
@@ -28,14 +29,12 @@ function addFormat(core: Core, formatType: string, validationFunction: JSONValid
|
|
|
28
29
|
if (typeof validationFunction !== "function") {
|
|
29
30
|
throw new Error(`Validation function expected. Received ${typeof validationFunction}`);
|
|
30
31
|
}
|
|
31
|
-
if (core.validateFormat[formatType]
|
|
32
|
-
|
|
33
|
-
return;
|
|
32
|
+
if (core.validateFormat[formatType]) {
|
|
33
|
+
throw new Error(`A format '${formatType}' is already registered to validation`);
|
|
34
34
|
}
|
|
35
|
-
|
|
35
|
+
core.validateFormat[formatType] = validationFunction;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
|
|
39
38
|
/**
|
|
40
39
|
* Adds a custom keyword validation to a specific type. May not override existing keywords.
|
|
41
40
|
*
|
|
@@ -44,7 +43,12 @@ function addFormat(core: Core, formatType: string, validationFunction: JSONValid
|
|
|
44
43
|
* @param keyword - The keyword to add, i.e. `minWidth: ...`
|
|
45
44
|
* @param validationFunction - called with (core, schema, value, pointer)
|
|
46
45
|
*/
|
|
47
|
-
function addKeyword(
|
|
46
|
+
function addKeyword(
|
|
47
|
+
core: Core,
|
|
48
|
+
datatype: string,
|
|
49
|
+
keyword: string,
|
|
50
|
+
validationFunction: JSONValidator
|
|
51
|
+
) {
|
|
48
52
|
if (typeof validationFunction !== "function") {
|
|
49
53
|
throw new Error(`Validation function expected. Received ${typeof validationFunction}`);
|
|
50
54
|
}
|
|
@@ -57,7 +61,6 @@ function addKeyword(core: Core, datatype: string, keyword: string, validationFun
|
|
|
57
61
|
core.validateKeyword[keyword] = validationFunction;
|
|
58
62
|
}
|
|
59
63
|
|
|
60
|
-
|
|
61
64
|
export default {
|
|
62
65
|
error: addError,
|
|
63
66
|
format: addFormat,
|
package/lib/compile/getRef.ts
CHANGED
|
@@ -2,16 +2,15 @@ import { get } from "gson-pointer";
|
|
|
2
2
|
import splitRef from "./splitRef";
|
|
3
3
|
import getTypeOf from "../getTypeOf";
|
|
4
4
|
import { JSONSchema } from "../types";
|
|
5
|
-
|
|
5
|
+
import { Context } from "./types";
|
|
6
6
|
|
|
7
7
|
const suffixes = /(#|\/)+$/g;
|
|
8
|
-
const isObject = val =>
|
|
9
|
-
|
|
8
|
+
const isObject = (val: unknown): val is Record<string, any> => getTypeOf(val) === "object";
|
|
10
9
|
|
|
11
10
|
// 1. combined is known
|
|
12
11
|
// 2. base or pointer is known
|
|
13
12
|
// 3. base + pointer is known
|
|
14
|
-
export default function getRef(context, rootSchema: JSONSchema, $ref) {
|
|
13
|
+
export default function getRef(context: Context, rootSchema: JSONSchema, $ref: string): JSONSchema {
|
|
15
14
|
if (isObject($ref)) {
|
|
16
15
|
$ref = $ref.__ref || $ref.$ref;
|
|
17
16
|
}
|
package/lib/compile/index.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
/* eslint max-statements-per-line: ["error", { "max": 2 }] */
|
|
2
|
-
import eachSchema from "../eachSchema";
|
|
3
|
-
import
|
|
2
|
+
import { eachSchema } from "../eachSchema";
|
|
3
|
+
import { Draft } from "../draft";
|
|
4
4
|
import joinScope from "./joinScope";
|
|
5
5
|
import getRef from "./getRef";
|
|
6
6
|
import { JSONSchema } from "../types";
|
|
7
|
+
import { Context } from "./types";
|
|
7
8
|
|
|
8
9
|
const COMPILED = "__compiled";
|
|
9
10
|
const COMPILED_REF = "__ref";
|
|
@@ -11,24 +12,64 @@ const GET_REF = "getRef";
|
|
|
11
12
|
const GET_ROOT = "getRoot";
|
|
12
13
|
const suffixes = /(#|\/)+$/g;
|
|
13
14
|
|
|
15
|
+
/**
|
|
16
|
+
* compiles the input root schema for `$ref` resolution and returns it again
|
|
17
|
+
* @attention this modifies input schema but maintains object-structure
|
|
18
|
+
*
|
|
19
|
+
* for a compiled json-schema you can call getRef on any contained schema (location of type).
|
|
20
|
+
* this resolves a $ref target to a valid schema (for a valid $ref)
|
|
21
|
+
*
|
|
22
|
+
* @param draft
|
|
23
|
+
* @param schemaToCompile - json-schema to compile
|
|
24
|
+
* @param [rootSchema] - compiled root json-schema to use for definitions resolution
|
|
25
|
+
* @param [force] = false - force compile json-schema
|
|
26
|
+
* @return compiled copy of input json-schema
|
|
27
|
+
*/
|
|
28
|
+
export default function compileSchema(
|
|
29
|
+
draft: Draft,
|
|
30
|
+
schemaToCompile: JSONSchema,
|
|
31
|
+
rootSchema: JSONSchema = schemaToCompile,
|
|
32
|
+
force = false
|
|
33
|
+
): JSONSchema {
|
|
34
|
+
if (!schemaToCompile || schemaToCompile[COMPILED] !== undefined) {
|
|
35
|
+
return schemaToCompile;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const context: Context = { ids: {}, remotes: draft.remotes };
|
|
39
|
+
const rootSchemaAsString = JSON.stringify(schemaToCompile);
|
|
40
|
+
const compiledSchema: JSONSchema = JSON.parse(rootSchemaAsString);
|
|
14
41
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
Object.defineProperty(rootSchema, COMPILED, { enumerable: false, value: true });
|
|
21
|
-
Object.defineProperty(rootSchema, GET_REF, { enumerable: false, value: getRef.bind(null, context, rootSchema) });
|
|
42
|
+
Object.defineProperty(compiledSchema, COMPILED, { enumerable: false, value: true });
|
|
43
|
+
Object.defineProperty(compiledSchema, GET_REF, {
|
|
44
|
+
enumerable: false,
|
|
45
|
+
value: getRef.bind(null, context, compiledSchema)
|
|
46
|
+
});
|
|
22
47
|
|
|
48
|
+
// bail early, when no $refs are defined
|
|
23
49
|
if (force === false && rootSchemaAsString.includes("$ref") === false) {
|
|
24
|
-
|
|
25
|
-
return rootSchema;
|
|
50
|
+
return compiledSchema;
|
|
26
51
|
}
|
|
27
52
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
53
|
+
// compile this schema under rootSchema, making definitions available to $ref-resolution
|
|
54
|
+
if (schemaToCompile !== rootSchema) {
|
|
55
|
+
Object.defineProperty(compiledSchema, "definitions", {
|
|
56
|
+
enumerable: false,
|
|
57
|
+
value: Object.assign(
|
|
58
|
+
{},
|
|
59
|
+
rootSchema.definitions,
|
|
60
|
+
rootSchema.$defs,
|
|
61
|
+
schemaToCompile.definitions,
|
|
62
|
+
schemaToCompile.$defs
|
|
63
|
+
)
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const scopes: Record<string, string> = {};
|
|
68
|
+
const getRoot = () => compiledSchema;
|
|
69
|
+
eachSchema(compiledSchema, (schema, pointer) => {
|
|
70
|
+
if (schema.id) {
|
|
71
|
+
context.ids[schema.id.replace(suffixes, "")] = pointer;
|
|
72
|
+
}
|
|
32
73
|
|
|
33
74
|
// build up scopes and add them to $ref-resolution map
|
|
34
75
|
pointer = `#${pointer}`.replace(/##+/, "#");
|
|
@@ -37,10 +78,15 @@ export default function compile(rootSchema: JSONSchema, force = false): JSONSche
|
|
|
37
78
|
const previousScope = scopes[previousPointer] || scopes[parentPointer];
|
|
38
79
|
const scope = joinScope(previousScope, schema.id);
|
|
39
80
|
scopes[pointer] = scope;
|
|
40
|
-
if (context.ids[scope] == null) {
|
|
81
|
+
if (context.ids[scope] == null) {
|
|
82
|
+
context.ids[scope] = pointer;
|
|
83
|
+
}
|
|
41
84
|
|
|
42
|
-
if (schema.$ref) {
|
|
43
|
-
Object.defineProperty(schema, COMPILED_REF, {
|
|
85
|
+
if (schema.$ref && !schema[COMPILED_REF]) {
|
|
86
|
+
Object.defineProperty(schema, COMPILED_REF, {
|
|
87
|
+
enumerable: false,
|
|
88
|
+
value: joinScope(scope, schema.$ref)
|
|
89
|
+
});
|
|
44
90
|
// @todo currently not used:
|
|
45
91
|
Object.defineProperty(schema, GET_ROOT, { enumerable: false, value: getRoot });
|
|
46
92
|
// console.log("compiled ref", scope, schema.$ref, "=>", joinScope(scope, schema.$ref));
|
|
@@ -48,5 +94,5 @@ export default function compile(rootSchema: JSONSchema, force = false): JSONSche
|
|
|
48
94
|
});
|
|
49
95
|
|
|
50
96
|
// console.log(JSON.stringify(context.ids, null, 2));
|
|
51
|
-
return
|
|
97
|
+
return compiledSchema;
|
|
52
98
|
}
|
package/lib/config/strings.ts
CHANGED
|
@@ -5,15 +5,28 @@ export default {
|
|
|
5
5
|
AdditionalPropertiesError: "Additional property `{{property}}` on `{{pointer}}` does not match schema `{{schema}}`",
|
|
6
6
|
AnyOfError: "Value `{{value}}` at `{{pointer}}` does not match any schema of `{{anyOf}}`",
|
|
7
7
|
AllOfError: "Value `{{value}}` at `{{pointer}}` does not match schema of `{{allOf}}`",
|
|
8
|
+
ConstError: "Expected value at `{{pointer}}` to be `{{expected}}`, but value given is `{{value}}`",
|
|
9
|
+
ContainsError: "The array at `{{pointer}}` must contain an element that matches `{{schema}}`",
|
|
10
|
+
ContainsArrayError: "The property at `{{pointer}}` must not be an array",
|
|
11
|
+
containsAnyError: "The array at `{{pointer}}` must contain at least one item",
|
|
8
12
|
EnumError: "Expected given value `{{value}}` in `{{pointer}}` to be one of `{{values}}`",
|
|
13
|
+
FormatDateError: "Value `{{value}}` at `{{pointer}}` is not a valid date",
|
|
9
14
|
FormatDateTimeError: "Value `{{value}}` at `{{pointer}}` is not a valid date-time",
|
|
10
15
|
FormatEmailError: "Value `{{value}}` at `{{pointer}}` is not a valid email",
|
|
11
|
-
|
|
12
|
-
|
|
16
|
+
FormatURLError: "Value `{{value}}` at `{{pointer}}` is not a valid url",
|
|
17
|
+
FormatURIError: "Value `{{value}}` at `{{pointer}}` is not a valid uri",
|
|
18
|
+
FormatURIReferenceError: "Value `{{value}}` at `{{pointer}}` is not a valid uri-reference",
|
|
19
|
+
FormatURITemplateError: "Value `{{value}}` at `{{pointer}}` is not a valid uri-template",
|
|
13
20
|
FormatHostnameError: "Value `{{value}}` at `{{pointer}}` is not a valid hostname",
|
|
14
21
|
FormatIPV4Error: "Value `{{value}}` at `{{pointer}}` is not a valid IPv4 address",
|
|
22
|
+
FormatIPV4LeadingZeroError: "IPv4 addresses starting with zero are invalid, since they are interpreted as octals",
|
|
15
23
|
FormatIPV6Error: "Value `{{value}}` at `{{pointer}}` is not a valid IPv6 address",
|
|
24
|
+
FormatIPV6LeadingZeroError: "IPv6 addresses starting with zero are invalid, since they are interpreted as octals",
|
|
25
|
+
FormatJSONPointerError: "Value `{{value}}` at `{{pointer}}` is not a valid json-pointer",
|
|
16
26
|
FormatRegExError: "Value `{{value}}` at `{{pointer}}` is not a valid regular expression",
|
|
27
|
+
FormatTimeError: "Value `{{value}}` at `{{pointer}}` is not a valid time",
|
|
28
|
+
InvalidDataError: "No value may be specified in `{{pointer}}`",
|
|
29
|
+
InvalidPropertyNameError: "Invalid property name `{{property}}` at `{{pointer}}`",
|
|
17
30
|
MaximumError: "Value in `{{pointer}}` is `{{length}}`, but should be `{{maximum}}` at maximum",
|
|
18
31
|
MaxItemsError: "Too many items in `{{pointer}}`, should be `{{maximum}}` at most, but got `{{length}}`",
|
|
19
32
|
MaxLengthError: "Value `{{pointer}}` should have a maximum length of `{{maxLength}}`, but got `{{length}}`.",
|
|
@@ -38,4 +51,5 @@ export default {
|
|
|
38
51
|
UnknownPropertyError: "Could not find a valid schema for property `{{pointer}}` within object",
|
|
39
52
|
UniqueItemsError: "Expected unique items in {{pointer}}: duplicate value `{{value}}` found at {{itemPointer}} and {{duplicatePointer}}",
|
|
40
53
|
ValueNotEmptyError: "A value for `{{property}}` is required at `{{pointer}}`"
|
|
41
|
-
}
|
|
54
|
+
} as Record<string, string>;
|
|
55
|
+
|
package/lib/createSchemaOf.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import getTypeOf from "./getTypeOf";
|
|
2
2
|
import { JSONSchema } from "./types";
|
|
3
3
|
|
|
4
|
-
|
|
5
4
|
/**
|
|
6
5
|
* Create a simple json schema for the given input data
|
|
7
6
|
* @param data - data to get json schema for
|
|
@@ -14,12 +13,11 @@ export default function createSchemaOf(data: any): JSONSchema {
|
|
|
14
13
|
|
|
15
14
|
if (schema.type === "object") {
|
|
16
15
|
schema.properties = {};
|
|
17
|
-
Object.keys(data).forEach(key => (schema.properties[key] = createSchemaOf(data[key])));
|
|
16
|
+
Object.keys(data).forEach((key) => (schema.properties[key] = createSchemaOf(data[key])));
|
|
18
17
|
}
|
|
19
18
|
|
|
20
19
|
if (schema.type === "array" && data.length === 1) {
|
|
21
20
|
schema.items = createSchemaOf(data[0]);
|
|
22
|
-
|
|
23
21
|
} else if (schema.type === "array") {
|
|
24
22
|
schema.items = data.map(createSchemaOf);
|
|
25
23
|
}
|