json-schema-library 6.1.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/CHANGELOG.md +106 -0
- package/README.md +811 -200
- package/TASKS.md +3 -81
- package/dist/index.d.ts +23 -34
- 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 +1 -1
- package/dist/lib/compile/getRef.d.ts +2 -1
- package/dist/lib/compile/index.d.ts +8 -5
- package/dist/lib/compile/types.d.ts +5 -0
- 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 +2 -1
- package/dist/lib/draft06/index.d.ts +7 -0
- package/dist/lib/draft06/validation/type.d.ts +3 -10
- 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 +3 -3
- package/dist/lib/getSchema.d.ts +1 -1
- package/dist/lib/getTemplate.d.ts +1 -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/types.d.ts +7 -57
- package/dist/lib/step.d.ts +1 -1
- package/dist/lib/types.d.ts +3 -3
- package/dist/lib/utils/createCustomError.d.ts +8 -11
- package/dist/lib/utils/filter.d.ts +4 -3
- package/dist/lib/utils/flattenArray.d.ts +1 -1
- package/dist/lib/utils/punycode.ucs2decode.d.ts +1 -1
- package/dist/lib/validate.d.ts +1 -1
- 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 -17
- package/dist/module/index.js +17 -33
- package/dist/module/lib/SchemaService.js +7 -4
- package/dist/module/lib/{draft06/addSchema.js → addRemoteSchema.js} +2 -4
- package/dist/module/lib/compile/getRef.js +1 -1
- package/dist/module/lib/compile/index.js +36 -22
- package/dist/module/lib/compile/types.js +1 -0
- 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 +32 -20
- package/dist/module/lib/draft06/index.js +96 -0
- package/dist/module/lib/draft06/validation/keyword.js +19 -7
- package/dist/module/lib/draft06/validation/type.js +18 -17
- package/dist/module/lib/draft07/index.js +96 -0
- package/dist/module/lib/each.js +1 -1
- package/dist/module/lib/eachSchema.js +28 -19
- package/dist/module/lib/getChildSchemaSelection.js +1 -1
- package/dist/module/lib/getSchema.js +2 -1
- package/dist/module/lib/getTemplate.js +2 -2
- package/dist/module/lib/jsoneditor/index.js +16 -0
- package/dist/module/lib/resolveOneOf.strict.js +40 -2
- package/dist/module/lib/{resolveRef.withOverwrite.js → resolveRef.merge.js} +0 -0
- package/dist/module/lib/schema/getTypeDefs.js +2 -2
- package/dist/module/lib/schema/getTypeId.js +9 -5
- package/dist/module/lib/schema/types.js +33 -9
- package/dist/module/lib/step.js +6 -3
- package/dist/module/lib/utils/createCustomError.js +4 -4
- package/dist/module/lib/utils/flattenArray.js +4 -3
- package/dist/module/lib/utils/merge.js +1 -0
- package/dist/module/lib/utils/punycode.ucs2decode.js +4 -3
- package/dist/module/lib/validate.js +1 -0
- package/dist/module/lib/validation/errors.js +1 -1
- package/dist/module/lib/validation/format.js +12 -6
- package/dist/module/lib/validation/keyword.js +2 -2
- package/index.ts +38 -33
- package/lib/SchemaService.ts +18 -11
- package/lib/{addSchema.ts → addRemoteSchema.ts} +3 -5
- package/lib/addValidator.ts +10 -7
- package/lib/compile/getRef.ts +3 -4
- package/lib/compile/index.ts +59 -23
- package/lib/compile/types.ts +6 -0
- package/lib/draft/index.ts +201 -0
- package/lib/draft04/index.ts +95 -0
- package/lib/draft06/compile/index.ts +57 -21
- package/lib/draft06/index.ts +101 -0
- package/lib/draft06/validation/keyword.ts +47 -25
- package/lib/draft06/validation/type.ts +23 -19
- package/lib/draft07/index.ts +101 -0
- package/lib/each.ts +6 -4
- package/lib/eachSchema.ts +45 -32
- package/lib/getChildSchemaSelection.ts +4 -4
- package/lib/getSchema.ts +3 -2
- package/lib/getTemplate.ts +33 -16
- package/lib/isValid.ts +7 -3
- package/lib/jsoneditor/index.ts +20 -0
- package/lib/resolveAllOf.ts +1 -1
- package/lib/resolveAnyOf.ts +7 -3
- package/lib/resolveOneOf.fuzzy.ts +1 -1
- package/lib/resolveOneOf.strict.ts +49 -4
- package/lib/{resolveRef.withOverwrite.ts → resolveRef.merge.ts} +0 -0
- package/lib/schema/getTypeDefs.ts +6 -7
- package/lib/schema/getTypeId.ts +10 -7
- package/lib/schema/types.ts +41 -9
- package/lib/step.ts +9 -6
- package/lib/types.ts +7 -6
- package/lib/utils/createCustomError.ts +8 -13
- package/lib/utils/filter.ts +4 -4
- package/lib/utils/flattenArray.ts +5 -4
- package/lib/utils/merge.ts +1 -0
- package/lib/utils/punycode.ucs2decode.ts +6 -5
- package/lib/validate.ts +2 -1
- package/lib/validateAsync.ts +2 -2
- package/lib/validation/errors.ts +2 -4
- package/lib/validation/format.ts +42 -17
- package/lib/validation/keyword.ts +30 -30
- package/package.json +3 -1
- package/tsconfig.json +1 -1
- package/dist/lib/cores/CoreInterface.d.ts +0 -42
- package/dist/lib/cores/Draft04.d.ts +0 -13
- package/dist/lib/cores/Draft06.d.ts +0 -15
- package/dist/lib/cores/Draft07.d.ts +0 -15
- package/dist/lib/cores/JsonEditor.d.ts +0 -13
- package/dist/lib/draft06/addSchema.d.ts +0 -7
- package/dist/module/lib/addSchema.js +0 -11
- package/dist/module/lib/cores/CoreInterface.js +0 -73
- package/dist/module/lib/cores/Draft04.js +0 -52
- package/dist/module/lib/cores/Draft06.js +0 -61
- package/dist/module/lib/cores/Draft07.js +0 -61
- package/dist/module/lib/cores/JsonEditor.js +0 -51
- package/dist/module/remotes/draft04.json +0 -150
- package/dist/module/remotes/draft06.json +0 -155
- package/dist/module/remotes/draft07.json +0 -172
- package/dist/module/remotes/index.js +0 -9
- package/dist/remotes/index.d.ts +0 -4
- package/lib/cores/CoreInterface.ts +0 -103
- package/lib/cores/Draft04.ts +0 -64
- package/lib/cores/Draft06.ts +0 -76
- package/lib/cores/Draft07.ts +0 -75
- package/lib/cores/JsonEditor.ts +0 -62
- package/lib/draft06/addSchema.ts +0 -14
- package/remotes/index.ts +0 -9
package/dist/module/index.js
CHANGED
|
@@ -1,45 +1,29 @@
|
|
|
1
|
-
import
|
|
2
|
-
import addValidator from "./lib/addValidator";
|
|
3
|
-
import compileSchema from "./lib/compileSchema";
|
|
4
|
-
import createCustomError from "./lib/utils/createCustomError";
|
|
5
|
-
import createSchemaOf from "./lib/createSchemaOf";
|
|
6
|
-
import Draft04 from "./lib/cores/Draft04";
|
|
7
|
-
import Draft06 from "./lib/cores/Draft06";
|
|
8
|
-
import Draft07 from "./lib/cores/Draft07";
|
|
9
|
-
import each from "./lib/each";
|
|
10
|
-
import eachSchema from "./lib/eachSchema";
|
|
11
|
-
import getChildSchemaSelection from "./lib/getChildSchemaSelection";
|
|
12
|
-
import getSchema from "./lib/getSchema";
|
|
13
|
-
import getTemplate from "./lib/getTemplate";
|
|
1
|
+
import { createError, createCustomError } from "./lib/utils/createCustomError";
|
|
14
2
|
import getTypeOf from "./lib/getTypeOf";
|
|
15
|
-
import Interface from "./lib/cores/CoreInterface";
|
|
16
|
-
import isValid from "./lib/isValid";
|
|
17
|
-
import JsonEditor from "./lib/cores/JsonEditor";
|
|
18
3
|
import resolveOneOf from "./lib/resolveOneOf.strict";
|
|
19
4
|
import resolveOneOfFuzzy from "./lib/resolveOneOf.fuzzy";
|
|
5
|
+
import resolveRef from "./lib/resolveRef.strict";
|
|
6
|
+
import resolveRefMerge from "./lib/resolveRef.merge";
|
|
20
7
|
import SchemaService from "./lib/SchemaService";
|
|
21
8
|
import settings from "./lib/config/settings";
|
|
22
|
-
import step from "./lib/step";
|
|
23
9
|
import strings from "./lib/config/strings";
|
|
24
|
-
import validate from "./lib/validate";
|
|
25
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";
|
|
26
18
|
const config = { strings };
|
|
27
|
-
export { config,
|
|
19
|
+
export { config, Draft, Draft04, // core implementing draft04 specs
|
|
20
|
+
draft04Config, // config implementing draft04 specs
|
|
28
21
|
Draft06, // core implementing draft06 specs
|
|
22
|
+
draft06Config, // config implementing draft06 specs
|
|
29
23
|
Draft07, // core implementing draft07 specs
|
|
24
|
+
draft07Config, // config implementing draft07 specs
|
|
30
25
|
JsonEditor, // adjusted core of draft04 to better support the json-editor
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
each, // iterate over data, receiving each data-entry with its schema
|
|
35
|
-
eachSchema, // iterates over a json-schemas type definitions
|
|
36
|
-
getChildSchemaSelection, // get available child schemas
|
|
37
|
-
getSchema, // get schema of datapointer
|
|
38
|
-
getTemplate, // create data which is valid to the given schema
|
|
39
|
-
getTypeOf, // returns the javascript datatype
|
|
40
|
-
isValid, // returns a boolean if the schema is valid
|
|
41
|
-
resolveOneOf, resolveOneOfFuzzy, settings, SchemaService, step, // steps into a json-schema, returning the matching child-schema
|
|
42
|
-
validate, // validates data by a schema
|
|
43
|
-
validateAsync // async validation of data by a schema
|
|
26
|
+
draftJsonEditorConfig, // adjusted config of draft04 to better support the json-editor
|
|
27
|
+
createError, createCustomError, getTypeOf, // returns the javascript datatype
|
|
28
|
+
isJSONError, render, resolveRef, resolveRefMerge, resolveOneOf, resolveOneOfFuzzy, settings, SchemaService, validateAsync // async validation of data by a schema
|
|
44
29
|
};
|
|
45
|
-
export { isJSONError } from "./lib/types";
|
|
@@ -1,5 +1,5 @@
|
|
|
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
|
export default class SchemaService {
|
|
@@ -19,14 +19,17 @@ export default class SchemaService {
|
|
|
19
19
|
this.cache = {};
|
|
20
20
|
}
|
|
21
21
|
get(pointer, data) {
|
|
22
|
-
if (data) {
|
|
22
|
+
if (data) {
|
|
23
|
+
// possibly separate entry point
|
|
23
24
|
const schema = getSchema(this.core, pointer, data, this.schema);
|
|
24
25
|
return copy(schema);
|
|
25
26
|
}
|
|
26
|
-
if (pointer === "#") {
|
|
27
|
+
if (pointer === "#") {
|
|
28
|
+
// root
|
|
27
29
|
return this.schema;
|
|
28
30
|
}
|
|
29
|
-
if (this.cache[pointer]) {
|
|
31
|
+
if (this.cache[pointer]) {
|
|
32
|
+
// return cached result
|
|
30
33
|
return this.cache[pointer];
|
|
31
34
|
}
|
|
32
35
|
const parentPointer = gp.join(pointer, "..");
|
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import remotes from "../../remotes";
|
|
2
|
-
import compileSchema from "./compile";
|
|
3
1
|
/**
|
|
4
2
|
* register a json-schema to be referenced from another json-schema
|
|
5
3
|
* @param url base-url of json-schema (aka id)
|
|
6
4
|
* @param schema
|
|
7
5
|
*/
|
|
8
|
-
export default function
|
|
6
|
+
export default function addRemoteSchema(draft, url, schema) {
|
|
9
7
|
schema.id = schema.id || url;
|
|
10
|
-
remotes[url] = compileSchema(schema);
|
|
8
|
+
draft.remotes[url] = draft.compileSchema(schema);
|
|
11
9
|
}
|
|
@@ -2,7 +2,7 @@ import { get } from "gson-pointer";
|
|
|
2
2
|
import splitRef from "./splitRef";
|
|
3
3
|
import getTypeOf from "../getTypeOf";
|
|
4
4
|
const suffixes = /(#|\/)+$/g;
|
|
5
|
-
const isObject = (val) =>
|
|
5
|
+
const isObject = (val) => getTypeOf(val) === "object";
|
|
6
6
|
// 1. combined is known
|
|
7
7
|
// 2. base or pointer is known
|
|
8
8
|
// 3. base + pointer is known
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
/* eslint max-statements-per-line: ["error", { "max": 2 }] */
|
|
2
|
-
import eachSchema from "../eachSchema";
|
|
3
|
-
import remotes from "../../remotes";
|
|
2
|
+
import { eachSchema } from "../eachSchema";
|
|
4
3
|
import joinScope from "./joinScope";
|
|
5
4
|
import getRef from "./getRef";
|
|
6
5
|
const COMPILED = "__compiled";
|
|
@@ -9,32 +8,44 @@ const GET_REF = "getRef";
|
|
|
9
8
|
const GET_ROOT = "getRoot";
|
|
10
9
|
const suffixes = /(#|\/)+$/g;
|
|
11
10
|
/**
|
|
12
|
-
* compiles the input root schema for
|
|
11
|
+
* compiles the input root schema for `$ref` resolution and returns it again
|
|
13
12
|
* @attention this modifies input schema but maintains object-structure
|
|
14
13
|
*
|
|
15
14
|
* for a compiled json-schema you can call getRef on any contained schema (location of type).
|
|
16
15
|
* this resolves a $ref target to a valid schema (for a valid $ref)
|
|
17
16
|
*
|
|
18
|
-
* @param
|
|
19
|
-
* @param
|
|
20
|
-
* @
|
|
17
|
+
* @param draft
|
|
18
|
+
* @param schemaToCompile - json-schema to compile
|
|
19
|
+
* @param [rootSchema] - compiled root json-schema to use for definitions resolution
|
|
20
|
+
* @param [force] = false - force compile json-schema
|
|
21
|
+
* @return compiled copy of input json-schema
|
|
21
22
|
*/
|
|
22
|
-
export default function
|
|
23
|
-
if (
|
|
24
|
-
return
|
|
25
|
-
}
|
|
26
|
-
const context = { ids: {}, remotes:
|
|
27
|
-
const rootSchemaAsString = JSON.stringify(
|
|
28
|
-
|
|
29
|
-
Object.defineProperty(
|
|
30
|
-
Object.defineProperty(
|
|
23
|
+
export default function compileSchema(draft, schemaToCompile, rootSchema = schemaToCompile, force = false) {
|
|
24
|
+
if (!schemaToCompile || schemaToCompile[COMPILED] !== undefined) {
|
|
25
|
+
return schemaToCompile;
|
|
26
|
+
}
|
|
27
|
+
const context = { ids: {}, remotes: draft.remotes };
|
|
28
|
+
const rootSchemaAsString = JSON.stringify(schemaToCompile);
|
|
29
|
+
const compiledSchema = JSON.parse(rootSchemaAsString);
|
|
30
|
+
Object.defineProperty(compiledSchema, COMPILED, { enumerable: false, value: true });
|
|
31
|
+
Object.defineProperty(compiledSchema, GET_REF, {
|
|
32
|
+
enumerable: false,
|
|
33
|
+
value: getRef.bind(null, context, compiledSchema)
|
|
34
|
+
});
|
|
35
|
+
// bail early, when no $refs are defined
|
|
31
36
|
if (force === false && rootSchemaAsString.includes("$ref") === false) {
|
|
32
|
-
|
|
33
|
-
|
|
37
|
+
return compiledSchema;
|
|
38
|
+
}
|
|
39
|
+
// compile this schema under rootSchema, making definitions available to $ref-resolution
|
|
40
|
+
if (schemaToCompile !== rootSchema) {
|
|
41
|
+
Object.defineProperty(compiledSchema, "definitions", {
|
|
42
|
+
enumerable: false,
|
|
43
|
+
value: Object.assign({}, rootSchema.definitions, rootSchema.$defs, schemaToCompile.definitions, schemaToCompile.$defs)
|
|
44
|
+
});
|
|
34
45
|
}
|
|
35
46
|
const scopes = {};
|
|
36
|
-
const getRoot = () =>
|
|
37
|
-
eachSchema(
|
|
47
|
+
const getRoot = () => compiledSchema;
|
|
48
|
+
eachSchema(compiledSchema, (schema, pointer) => {
|
|
38
49
|
if (schema.id) {
|
|
39
50
|
context.ids[schema.id.replace(suffixes, "")] = pointer;
|
|
40
51
|
}
|
|
@@ -48,13 +59,16 @@ export default function compile(rootSchema, force = false) {
|
|
|
48
59
|
if (context.ids[scope] == null) {
|
|
49
60
|
context.ids[scope] = pointer;
|
|
50
61
|
}
|
|
51
|
-
if (schema.$ref) {
|
|
52
|
-
Object.defineProperty(schema, COMPILED_REF, {
|
|
62
|
+
if (schema.$ref && !schema[COMPILED_REF]) {
|
|
63
|
+
Object.defineProperty(schema, COMPILED_REF, {
|
|
64
|
+
enumerable: false,
|
|
65
|
+
value: joinScope(scope, schema.$ref)
|
|
66
|
+
});
|
|
53
67
|
// @todo currently not used:
|
|
54
68
|
Object.defineProperty(schema, GET_ROOT, { enumerable: false, value: getRoot });
|
|
55
69
|
// console.log("compiled ref", scope, schema.$ref, "=>", joinScope(scope, schema.$ref));
|
|
56
70
|
}
|
|
57
71
|
});
|
|
58
72
|
// console.log(JSON.stringify(context.ids, null, 2));
|
|
59
|
-
return
|
|
73
|
+
return compiledSchema;
|
|
60
74
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
export class Draft {
|
|
2
|
+
constructor(config, schema) {
|
|
3
|
+
/** cache for remote schemas */
|
|
4
|
+
this.remotes = {};
|
|
5
|
+
/** error creators by id */
|
|
6
|
+
this.errors = {};
|
|
7
|
+
/** map for valid keywords of a type */
|
|
8
|
+
this.typeKeywords = {};
|
|
9
|
+
/** keyword validators */
|
|
10
|
+
this.validateKeyword = {};
|
|
11
|
+
/** type validators */
|
|
12
|
+
this.validateType = {};
|
|
13
|
+
/** format validators */
|
|
14
|
+
this.validateFormat = {};
|
|
15
|
+
this.config = config;
|
|
16
|
+
this.typeKeywords = JSON.parse(JSON.stringify(config.typeKeywords));
|
|
17
|
+
this.validateKeyword = Object.assign({}, config.validateKeyword);
|
|
18
|
+
this.validateType = Object.assign({}, config.validateType);
|
|
19
|
+
this.validateFormat = Object.assign({}, config.validateFormat);
|
|
20
|
+
this.errors = Object.assign({}, config.errors);
|
|
21
|
+
this.setSchema(schema);
|
|
22
|
+
}
|
|
23
|
+
get rootSchema() {
|
|
24
|
+
return this.__rootSchema;
|
|
25
|
+
}
|
|
26
|
+
set rootSchema(rootSchema) {
|
|
27
|
+
if (rootSchema == null) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
this.__rootSchema = this.config.compileSchema(this, rootSchema);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* register a json-schema to be referenced from another json-schema
|
|
34
|
+
* @param url - base-url of json-schema (aka id)
|
|
35
|
+
* @param schema - json-schema root
|
|
36
|
+
*/
|
|
37
|
+
addRemoteSchema(url, schema) {
|
|
38
|
+
this.config.addRemoteSchema(this, url, schema);
|
|
39
|
+
}
|
|
40
|
+
compileSchema(schema) {
|
|
41
|
+
var _a;
|
|
42
|
+
return this.config.compileSchema(this, schema, (_a = this.rootSchema) !== null && _a !== void 0 ? _a : schema);
|
|
43
|
+
}
|
|
44
|
+
createSchemaOf(data) {
|
|
45
|
+
return this.config.createSchemaOf(data);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Iterates over data, retrieving its schema
|
|
49
|
+
*
|
|
50
|
+
* @param data - the data to iterate
|
|
51
|
+
* @param callback - will be called with (schema, data, pointer) on each item
|
|
52
|
+
* @param [schema] - the schema matching the data. Defaults to rootSchema
|
|
53
|
+
* @param [pointer] - pointer to current data. Default to rootPointer
|
|
54
|
+
*/
|
|
55
|
+
each(data, callback, schema, pointer) {
|
|
56
|
+
return this.config.each(this, data, callback, schema, pointer);
|
|
57
|
+
}
|
|
58
|
+
eachSchema(callback, schema = this.rootSchema) {
|
|
59
|
+
return this.config.eachSchema(schema, callback);
|
|
60
|
+
}
|
|
61
|
+
getChildSchemaSelection(property, schema) {
|
|
62
|
+
return this.config.getChildSchemaSelection(this, property, schema);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Returns the json-schema of a data-json-pointer.
|
|
66
|
+
* Notes
|
|
67
|
+
* - Uses core.step to walk through data and schema
|
|
68
|
+
*
|
|
69
|
+
* @param pointer - json pointer in data to get the json schema for
|
|
70
|
+
* @param [data] - the data object, which includes the json pointers value. This is optional, as
|
|
71
|
+
* long as no oneOf, anyOf, etc statement is part of the pointers schema
|
|
72
|
+
* @param [schema] - the json schema to iterate. Defaults to core.rootSchema
|
|
73
|
+
* @return json schema object of the json-pointer or an error
|
|
74
|
+
*/
|
|
75
|
+
getSchema(pointer = "#", data, schema) {
|
|
76
|
+
return this.config.getSchema(this, pointer, data, schema);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Create data object matching the given schema
|
|
80
|
+
*
|
|
81
|
+
* @param [data] - optional template data
|
|
82
|
+
* @param [schema] - json schema, defaults to rootSchema
|
|
83
|
+
* @return created template data
|
|
84
|
+
*/
|
|
85
|
+
getTemplate(data, schema) {
|
|
86
|
+
return this.config.getTemplate(this, data, schema);
|
|
87
|
+
}
|
|
88
|
+
isValid(data, schema, pointer) {
|
|
89
|
+
return this.config.isValid(this, data, schema, pointer);
|
|
90
|
+
}
|
|
91
|
+
resolveAnyOf(data, schema, pointer) {
|
|
92
|
+
return this.config.resolveAnyOf(this, data, schema, pointer);
|
|
93
|
+
}
|
|
94
|
+
resolveAllOf(data, schema, pointer) {
|
|
95
|
+
return this.config.resolveAllOf(this, data, schema, pointer);
|
|
96
|
+
}
|
|
97
|
+
resolveRef(schema) {
|
|
98
|
+
return this.config.resolveRef(schema, this.rootSchema);
|
|
99
|
+
}
|
|
100
|
+
resolveOneOf(data, schema, pointer) {
|
|
101
|
+
return this.config.resolveOneOf(this, data, schema, pointer);
|
|
102
|
+
}
|
|
103
|
+
setSchema(schema) {
|
|
104
|
+
this.rootSchema = schema;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Returns the json-schema of the given object property or array item.
|
|
108
|
+
* e.g. it steps by one key into the data
|
|
109
|
+
*
|
|
110
|
+
* This helper determines the location of the property within the schema (additional properties, oneOf, ...) and
|
|
111
|
+
* returns the correct schema.
|
|
112
|
+
*
|
|
113
|
+
* @param key - property-name or array-index
|
|
114
|
+
* @param schema - json schema of current data
|
|
115
|
+
* @param data - parent of key
|
|
116
|
+
* @param [pointer] - pointer to schema and data (parent of key)
|
|
117
|
+
* @return Schema or Error if failed resolving key
|
|
118
|
+
*/
|
|
119
|
+
step(key, schema, data, pointer) {
|
|
120
|
+
return this.config.step(this, key, schema, data, pointer);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Validate data by a json schema
|
|
124
|
+
*
|
|
125
|
+
* @param value - value to validate
|
|
126
|
+
* @param [schema] - json schema, defaults to rootSchema
|
|
127
|
+
* @param [pointer] - json pointer pointing to value (used for error-messages only)
|
|
128
|
+
* @return list of errors or empty
|
|
129
|
+
*/
|
|
130
|
+
validate(data, schema, pointer) {
|
|
131
|
+
return this.config.validate(this, data, schema, pointer);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import addRemoteSchema from "../addRemoteSchema";
|
|
2
|
+
import compileSchema from "../compileSchema";
|
|
3
|
+
import { each } from "../each";
|
|
4
|
+
import { eachSchema } from "../eachSchema";
|
|
5
|
+
import ERRORS from "../validation/errors";
|
|
6
|
+
import FORMATS from "../validation/format";
|
|
7
|
+
import getSchema from "../getSchema";
|
|
8
|
+
import getTemplate from "../getTemplate";
|
|
9
|
+
import isValid from "../isValid";
|
|
10
|
+
import KEYWORDS from "../validation/keyword";
|
|
11
|
+
import merge from "../utils/merge";
|
|
12
|
+
import resolveAllOf from "../resolveAllOf";
|
|
13
|
+
import resolveAnyOf from "../resolveAnyOf";
|
|
14
|
+
import resolveOneOf from "../resolveOneOf.strict";
|
|
15
|
+
import resolveRef from "../resolveRef.strict";
|
|
16
|
+
import step from "../step";
|
|
17
|
+
import createSchemaOf from "../createSchemaOf";
|
|
18
|
+
import getChildSchemaSelection from "../getChildSchemaSelection";
|
|
19
|
+
import TYPES from "../validation/type";
|
|
20
|
+
import validate from "../validate";
|
|
21
|
+
import { Draft } from "../draft";
|
|
22
|
+
const draft04Config = {
|
|
23
|
+
typeKeywords: {
|
|
24
|
+
array: ["enum", "items", "minItems", "maxItems", "uniqueItems", "not"],
|
|
25
|
+
boolean: ["enum", "not"],
|
|
26
|
+
object: [
|
|
27
|
+
"additionalProperties",
|
|
28
|
+
"dependencies",
|
|
29
|
+
"enum",
|
|
30
|
+
"format",
|
|
31
|
+
"minProperties",
|
|
32
|
+
"maxProperties",
|
|
33
|
+
"patternProperties",
|
|
34
|
+
"properties",
|
|
35
|
+
"required",
|
|
36
|
+
"not",
|
|
37
|
+
"oneOf",
|
|
38
|
+
"allOf",
|
|
39
|
+
"anyOf"
|
|
40
|
+
],
|
|
41
|
+
string: [
|
|
42
|
+
"enum",
|
|
43
|
+
"format",
|
|
44
|
+
"maxLength",
|
|
45
|
+
"minLength",
|
|
46
|
+
"pattern",
|
|
47
|
+
"not",
|
|
48
|
+
"oneOf",
|
|
49
|
+
"allOf",
|
|
50
|
+
"anyOf"
|
|
51
|
+
],
|
|
52
|
+
number: [
|
|
53
|
+
"enum",
|
|
54
|
+
"format",
|
|
55
|
+
"maximum",
|
|
56
|
+
"minimum",
|
|
57
|
+
"multipleOf",
|
|
58
|
+
"not",
|
|
59
|
+
"oneOf",
|
|
60
|
+
"allOf",
|
|
61
|
+
"anyOf"
|
|
62
|
+
],
|
|
63
|
+
null: ["enum", "format", "not", "oneOf", "allOf", "anyOf"]
|
|
64
|
+
},
|
|
65
|
+
validateKeyword: KEYWORDS,
|
|
66
|
+
validateType: TYPES,
|
|
67
|
+
validateFormat: FORMATS,
|
|
68
|
+
errors: ERRORS,
|
|
69
|
+
addRemoteSchema,
|
|
70
|
+
compileSchema,
|
|
71
|
+
createSchemaOf,
|
|
72
|
+
each,
|
|
73
|
+
eachSchema,
|
|
74
|
+
getChildSchemaSelection,
|
|
75
|
+
getSchema,
|
|
76
|
+
getTemplate,
|
|
77
|
+
isValid,
|
|
78
|
+
resolveAllOf,
|
|
79
|
+
resolveAnyOf,
|
|
80
|
+
resolveOneOf,
|
|
81
|
+
resolveRef,
|
|
82
|
+
step,
|
|
83
|
+
validate
|
|
84
|
+
};
|
|
85
|
+
class Draft04 extends Draft {
|
|
86
|
+
constructor(schema, config = {}) {
|
|
87
|
+
super(merge(draft04Config, config), schema);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
export { Draft04, draft04Config };
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
import remotes from "../../../remotes";
|
|
1
|
+
import { eachSchema } from "../../eachSchema";
|
|
2
|
+
// import remotes from "../../../remotes";
|
|
4
3
|
import joinScope from "../../compile/joinScope";
|
|
5
4
|
import getRef from "../../compile/getRef";
|
|
6
5
|
const COMPILED = "__compiled";
|
|
@@ -21,26 +20,36 @@ const suffixes = /(#|\/)+$/g;
|
|
|
21
20
|
* @param [force] = false force compile json-schema
|
|
22
21
|
* @return compiled json-schema
|
|
23
22
|
*/
|
|
24
|
-
export default function
|
|
23
|
+
export default function compileSchema(draft, schemaToCompile, rootSchema = schemaToCompile, force = false) {
|
|
25
24
|
// @ts-ignore
|
|
26
|
-
if (
|
|
27
|
-
return
|
|
25
|
+
if (schemaToCompile === true || schemaToCompile === false || schemaToCompile === undefined) {
|
|
26
|
+
return schemaToCompile;
|
|
28
27
|
}
|
|
29
|
-
if (
|
|
30
|
-
return
|
|
28
|
+
if (schemaToCompile[COMPILED] !== undefined) {
|
|
29
|
+
return schemaToCompile;
|
|
31
30
|
} // eslint-disable-line
|
|
32
|
-
const context = { ids: {}, remotes:
|
|
33
|
-
const rootSchemaAsString = JSON.stringify(
|
|
34
|
-
|
|
35
|
-
Object.defineProperty(
|
|
36
|
-
Object.defineProperty(
|
|
31
|
+
const context = { ids: {}, remotes: draft.remotes };
|
|
32
|
+
const rootSchemaAsString = JSON.stringify(schemaToCompile);
|
|
33
|
+
const compiledSchema = JSON.parse(rootSchemaAsString);
|
|
34
|
+
Object.defineProperty(compiledSchema, COMPILED, { enumerable: false, value: true });
|
|
35
|
+
Object.defineProperty(compiledSchema, GET_REF, {
|
|
36
|
+
enumerable: false,
|
|
37
|
+
value: getRef.bind(null, context, compiledSchema)
|
|
38
|
+
});
|
|
39
|
+
// bail early, when no $refs are defined
|
|
37
40
|
if (force === false && rootSchemaAsString.includes("$ref") === false) {
|
|
38
|
-
|
|
39
|
-
|
|
41
|
+
return compiledSchema;
|
|
42
|
+
}
|
|
43
|
+
// compile this schema under rootSchema, making definitions available to $ref-resolution
|
|
44
|
+
if (compiledSchema !== rootSchema) {
|
|
45
|
+
Object.defineProperty(compiledSchema, "$defs", {
|
|
46
|
+
enumerable: true,
|
|
47
|
+
value: Object.assign({}, rootSchema.definitions, rootSchema.$defs, compiledSchema.definitions, compiledSchema.$defs)
|
|
48
|
+
});
|
|
40
49
|
}
|
|
41
50
|
const scopes = {};
|
|
42
|
-
const getRoot = () =>
|
|
43
|
-
eachSchema(
|
|
51
|
+
const getRoot = () => compiledSchema;
|
|
52
|
+
eachSchema(compiledSchema, (schema, pointer) => {
|
|
44
53
|
if (schema.$id) {
|
|
45
54
|
context.ids[schema.$id.replace(suffixes, "")] = pointer;
|
|
46
55
|
}
|
|
@@ -54,12 +63,15 @@ export default function compile(rootSchema, force = false) {
|
|
|
54
63
|
if (context.ids[scope] == null) {
|
|
55
64
|
context.ids[scope] = pointer;
|
|
56
65
|
}
|
|
57
|
-
if (schema.$ref) {
|
|
58
|
-
Object.defineProperty(schema, COMPILED_REF, {
|
|
66
|
+
if (schema.$ref && !schema[COMPILED_REF]) {
|
|
67
|
+
Object.defineProperty(schema, COMPILED_REF, {
|
|
68
|
+
enumerable: false,
|
|
69
|
+
value: joinScope(scope, schema.$ref)
|
|
70
|
+
});
|
|
59
71
|
// @todo currently not used:
|
|
60
72
|
Object.defineProperty(schema, GET_ROOT, { enumerable: false, value: getRoot });
|
|
61
73
|
// console.log("compiled ref", scope, schema.$ref, "=>", joinScope(scope, schema.$ref));
|
|
62
74
|
}
|
|
63
75
|
});
|
|
64
|
-
return
|
|
76
|
+
return compiledSchema;
|
|
65
77
|
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import addRemoteSchema from "../addRemoteSchema";
|
|
2
|
+
import compileSchema from "../draft06/compile";
|
|
3
|
+
import { each } from "../each";
|
|
4
|
+
import { eachSchema } from "../eachSchema";
|
|
5
|
+
import ERRORS from "../validation/errors";
|
|
6
|
+
import FORMATS from "../validation/format";
|
|
7
|
+
import getSchema from "../getSchema";
|
|
8
|
+
import getTemplate from "../getTemplate";
|
|
9
|
+
import isValid from "../isValid";
|
|
10
|
+
import KEYWORDS from "../draft06/validation/keyword";
|
|
11
|
+
import merge from "../utils/merge";
|
|
12
|
+
import resolveAllOf from "../resolveAllOf";
|
|
13
|
+
import resolveAnyOf from "../resolveAnyOf";
|
|
14
|
+
import resolveOneOf from "../resolveOneOf.strict";
|
|
15
|
+
import resolveRef from "../resolveRef.strict";
|
|
16
|
+
import createSchemaOf from "../createSchemaOf";
|
|
17
|
+
import getChildSchemaSelection from "../getChildSchemaSelection";
|
|
18
|
+
import step from "../step";
|
|
19
|
+
import TYPES from "../draft06/validation/type";
|
|
20
|
+
import validate from "../validate";
|
|
21
|
+
import { Draft } from "../draft";
|
|
22
|
+
const draft06Config = {
|
|
23
|
+
typeKeywords: {
|
|
24
|
+
array: ["enum", "contains", "items", "minItems", "maxItems", "uniqueItems", "not", "if"],
|
|
25
|
+
boolean: ["enum", "not"],
|
|
26
|
+
object: [
|
|
27
|
+
"additionalProperties",
|
|
28
|
+
"dependencies",
|
|
29
|
+
"enum",
|
|
30
|
+
"format",
|
|
31
|
+
"minProperties",
|
|
32
|
+
"maxProperties",
|
|
33
|
+
"patternProperties",
|
|
34
|
+
"properties",
|
|
35
|
+
"propertyNames",
|
|
36
|
+
"required",
|
|
37
|
+
"not",
|
|
38
|
+
"oneOf",
|
|
39
|
+
"allOf",
|
|
40
|
+
"anyOf",
|
|
41
|
+
"if"
|
|
42
|
+
],
|
|
43
|
+
string: [
|
|
44
|
+
"enum",
|
|
45
|
+
"format",
|
|
46
|
+
"maxLength",
|
|
47
|
+
"minLength",
|
|
48
|
+
"pattern",
|
|
49
|
+
"not",
|
|
50
|
+
"oneOf",
|
|
51
|
+
"allOf",
|
|
52
|
+
"anyOf",
|
|
53
|
+
"if"
|
|
54
|
+
],
|
|
55
|
+
number: [
|
|
56
|
+
"enum",
|
|
57
|
+
"exclusiveMaximum",
|
|
58
|
+
"exclusiveMinimum",
|
|
59
|
+
"format",
|
|
60
|
+
"maximum",
|
|
61
|
+
"minimum",
|
|
62
|
+
"multipleOf",
|
|
63
|
+
"not",
|
|
64
|
+
"oneOf",
|
|
65
|
+
"allOf",
|
|
66
|
+
"anyOf",
|
|
67
|
+
"if"
|
|
68
|
+
],
|
|
69
|
+
null: ["enum", "format", "not", "oneOf", "allOf", "anyOf"]
|
|
70
|
+
},
|
|
71
|
+
validateKeyword: KEYWORDS,
|
|
72
|
+
validateType: TYPES,
|
|
73
|
+
validateFormat: FORMATS,
|
|
74
|
+
errors: ERRORS,
|
|
75
|
+
addRemoteSchema,
|
|
76
|
+
compileSchema,
|
|
77
|
+
createSchemaOf,
|
|
78
|
+
each,
|
|
79
|
+
eachSchema,
|
|
80
|
+
getChildSchemaSelection,
|
|
81
|
+
getSchema,
|
|
82
|
+
getTemplate,
|
|
83
|
+
isValid,
|
|
84
|
+
resolveAllOf,
|
|
85
|
+
resolveAnyOf,
|
|
86
|
+
resolveOneOf,
|
|
87
|
+
resolveRef,
|
|
88
|
+
step,
|
|
89
|
+
validate
|
|
90
|
+
};
|
|
91
|
+
class Draft06 extends Draft {
|
|
92
|
+
constructor(schema, config = {}) {
|
|
93
|
+
super(merge(draft06Config, config), schema);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
export { Draft06, draft06Config };
|