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.
Files changed (142) hide show
  1. package/.editorconfig +1 -0
  2. package/CHANGELOG.md +106 -0
  3. package/README.md +811 -200
  4. package/TASKS.md +3 -81
  5. package/dist/index.d.ts +23 -34
  6. package/dist/jsonSchemaLibrary.js +1 -1
  7. package/dist/lib/SchemaService.d.ts +6 -8
  8. package/dist/lib/{addSchema.d.ts → addRemoteSchema.d.ts} +2 -1
  9. package/dist/lib/addValidator.d.ts +1 -1
  10. package/dist/lib/compile/getRef.d.ts +2 -1
  11. package/dist/lib/compile/index.d.ts +8 -5
  12. package/dist/lib/compile/types.d.ts +5 -0
  13. package/dist/lib/draft/index.d.ts +132 -0
  14. package/dist/lib/draft04/index.d.ts +7 -0
  15. package/dist/lib/draft06/compile/index.d.ts +2 -1
  16. package/dist/lib/draft06/index.d.ts +7 -0
  17. package/dist/lib/draft06/validation/type.d.ts +3 -10
  18. package/dist/lib/draft07/index.d.ts +7 -0
  19. package/dist/lib/each.d.ts +3 -2
  20. package/dist/lib/eachSchema.d.ts +2 -4
  21. package/dist/lib/getChildSchemaSelection.d.ts +3 -3
  22. package/dist/lib/getSchema.d.ts +1 -1
  23. package/dist/lib/getTemplate.d.ts +1 -1
  24. package/dist/lib/isValid.d.ts +1 -1
  25. package/dist/lib/jsoneditor/index.d.ts +7 -0
  26. package/dist/lib/resolveAllOf.d.ts +1 -1
  27. package/dist/lib/resolveAnyOf.d.ts +1 -1
  28. package/dist/lib/resolveOneOf.fuzzy.d.ts +1 -1
  29. package/dist/lib/resolveOneOf.strict.d.ts +2 -2
  30. package/dist/lib/{resolveRef.withOverwrite.d.ts → resolveRef.merge.d.ts} +0 -0
  31. package/dist/lib/schema/getTypeDefs.d.ts +6 -4
  32. package/dist/lib/schema/types.d.ts +7 -57
  33. package/dist/lib/step.d.ts +1 -1
  34. package/dist/lib/types.d.ts +3 -3
  35. package/dist/lib/utils/createCustomError.d.ts +8 -11
  36. package/dist/lib/utils/filter.d.ts +4 -3
  37. package/dist/lib/utils/flattenArray.d.ts +1 -1
  38. package/dist/lib/utils/punycode.ucs2decode.d.ts +1 -1
  39. package/dist/lib/validate.d.ts +1 -1
  40. package/dist/lib/validateAsync.d.ts +1 -1
  41. package/dist/lib/validation/errors.d.ts +1 -3
  42. package/dist/lib/validation/format.d.ts +4 -17
  43. package/dist/module/index.js +17 -33
  44. package/dist/module/lib/SchemaService.js +7 -4
  45. package/dist/module/lib/{draft06/addSchema.js → addRemoteSchema.js} +2 -4
  46. package/dist/module/lib/compile/getRef.js +1 -1
  47. package/dist/module/lib/compile/index.js +36 -22
  48. package/dist/module/lib/compile/types.js +1 -0
  49. package/dist/module/lib/draft/index.js +133 -0
  50. package/dist/module/lib/draft04/index.js +90 -0
  51. package/dist/module/lib/draft06/compile/index.js +32 -20
  52. package/dist/module/lib/draft06/index.js +96 -0
  53. package/dist/module/lib/draft06/validation/keyword.js +19 -7
  54. package/dist/module/lib/draft06/validation/type.js +18 -17
  55. package/dist/module/lib/draft07/index.js +96 -0
  56. package/dist/module/lib/each.js +1 -1
  57. package/dist/module/lib/eachSchema.js +28 -19
  58. package/dist/module/lib/getChildSchemaSelection.js +1 -1
  59. package/dist/module/lib/getSchema.js +2 -1
  60. package/dist/module/lib/getTemplate.js +2 -2
  61. package/dist/module/lib/jsoneditor/index.js +16 -0
  62. package/dist/module/lib/resolveOneOf.strict.js +40 -2
  63. package/dist/module/lib/{resolveRef.withOverwrite.js → resolveRef.merge.js} +0 -0
  64. package/dist/module/lib/schema/getTypeDefs.js +2 -2
  65. package/dist/module/lib/schema/getTypeId.js +9 -5
  66. package/dist/module/lib/schema/types.js +33 -9
  67. package/dist/module/lib/step.js +6 -3
  68. package/dist/module/lib/utils/createCustomError.js +4 -4
  69. package/dist/module/lib/utils/flattenArray.js +4 -3
  70. package/dist/module/lib/utils/merge.js +1 -0
  71. package/dist/module/lib/utils/punycode.ucs2decode.js +4 -3
  72. package/dist/module/lib/validate.js +1 -0
  73. package/dist/module/lib/validation/errors.js +1 -1
  74. package/dist/module/lib/validation/format.js +12 -6
  75. package/dist/module/lib/validation/keyword.js +2 -2
  76. package/index.ts +38 -33
  77. package/lib/SchemaService.ts +18 -11
  78. package/lib/{addSchema.ts → addRemoteSchema.ts} +3 -5
  79. package/lib/addValidator.ts +10 -7
  80. package/lib/compile/getRef.ts +3 -4
  81. package/lib/compile/index.ts +59 -23
  82. package/lib/compile/types.ts +6 -0
  83. package/lib/draft/index.ts +201 -0
  84. package/lib/draft04/index.ts +95 -0
  85. package/lib/draft06/compile/index.ts +57 -21
  86. package/lib/draft06/index.ts +101 -0
  87. package/lib/draft06/validation/keyword.ts +47 -25
  88. package/lib/draft06/validation/type.ts +23 -19
  89. package/lib/draft07/index.ts +101 -0
  90. package/lib/each.ts +6 -4
  91. package/lib/eachSchema.ts +45 -32
  92. package/lib/getChildSchemaSelection.ts +4 -4
  93. package/lib/getSchema.ts +3 -2
  94. package/lib/getTemplate.ts +33 -16
  95. package/lib/isValid.ts +7 -3
  96. package/lib/jsoneditor/index.ts +20 -0
  97. package/lib/resolveAllOf.ts +1 -1
  98. package/lib/resolveAnyOf.ts +7 -3
  99. package/lib/resolveOneOf.fuzzy.ts +1 -1
  100. package/lib/resolveOneOf.strict.ts +49 -4
  101. package/lib/{resolveRef.withOverwrite.ts → resolveRef.merge.ts} +0 -0
  102. package/lib/schema/getTypeDefs.ts +6 -7
  103. package/lib/schema/getTypeId.ts +10 -7
  104. package/lib/schema/types.ts +41 -9
  105. package/lib/step.ts +9 -6
  106. package/lib/types.ts +7 -6
  107. package/lib/utils/createCustomError.ts +8 -13
  108. package/lib/utils/filter.ts +4 -4
  109. package/lib/utils/flattenArray.ts +5 -4
  110. package/lib/utils/merge.ts +1 -0
  111. package/lib/utils/punycode.ucs2decode.ts +6 -5
  112. package/lib/validate.ts +2 -1
  113. package/lib/validateAsync.ts +2 -2
  114. package/lib/validation/errors.ts +2 -4
  115. package/lib/validation/format.ts +42 -17
  116. package/lib/validation/keyword.ts +30 -30
  117. package/package.json +3 -1
  118. package/tsconfig.json +1 -1
  119. package/dist/lib/cores/CoreInterface.d.ts +0 -42
  120. package/dist/lib/cores/Draft04.d.ts +0 -13
  121. package/dist/lib/cores/Draft06.d.ts +0 -15
  122. package/dist/lib/cores/Draft07.d.ts +0 -15
  123. package/dist/lib/cores/JsonEditor.d.ts +0 -13
  124. package/dist/lib/draft06/addSchema.d.ts +0 -7
  125. package/dist/module/lib/addSchema.js +0 -11
  126. package/dist/module/lib/cores/CoreInterface.js +0 -73
  127. package/dist/module/lib/cores/Draft04.js +0 -52
  128. package/dist/module/lib/cores/Draft06.js +0 -61
  129. package/dist/module/lib/cores/Draft07.js +0 -61
  130. package/dist/module/lib/cores/JsonEditor.js +0 -51
  131. package/dist/module/remotes/draft04.json +0 -150
  132. package/dist/module/remotes/draft06.json +0 -155
  133. package/dist/module/remotes/draft07.json +0 -172
  134. package/dist/module/remotes/index.js +0 -9
  135. package/dist/remotes/index.d.ts +0 -4
  136. package/lib/cores/CoreInterface.ts +0 -103
  137. package/lib/cores/Draft04.ts +0 -64
  138. package/lib/cores/Draft06.ts +0 -76
  139. package/lib/cores/Draft07.ts +0 -75
  140. package/lib/cores/JsonEditor.ts +0 -62
  141. package/lib/draft06/addSchema.ts +0 -14
  142. package/remotes/index.ts +0 -9
@@ -1,45 +1,29 @@
1
- import addSchema from "./lib/addSchema";
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, Interface, Draft04, // core implementing draft04 specs
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
- addSchema, // add a schema to be references via $ref
32
- addValidator, // add validation for keyword, format, datatype and customize errors
33
- compileSchema, createCustomError, createSchemaOf, // creates a simple schema based on the given data
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 "./cores/JsonEditor";
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) { // possibly separate entry point
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 === "#") { // root
27
+ if (pointer === "#") {
28
+ // root
27
29
  return this.schema;
28
30
  }
29
- if (this.cache[pointer]) { // return cached result
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 addSchema(url, schema) {
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) => (getTypeOf(val) === "object");
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 $ref resolution and returns it again
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 rootSchema root json-schema ($id, defs, ... ) to compile
19
- * @param [force] = false force compile json-schema
20
- * @return compiled json-schema
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 compile(rootSchema, force = false) {
23
- if (rootSchema[COMPILED] !== undefined) {
24
- return rootSchema;
25
- } // eslint-disable-line
26
- const context = { ids: {}, remotes: Object.assign({}, remotes) };
27
- const rootSchemaAsString = JSON.stringify(rootSchema);
28
- rootSchema = JSON.parse(rootSchemaAsString);
29
- Object.defineProperty(rootSchema, COMPILED, { enumerable: false, value: true });
30
- Object.defineProperty(rootSchema, GET_REF, { enumerable: false, value: getRef.bind(null, context, rootSchema) });
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
- // bail early, when no $refs are defined
33
- return rootSchema;
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 = () => rootSchema;
37
- eachSchema(rootSchema, (schema, pointer) => {
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, { enumerable: false, value: joinScope(scope, schema.$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 rootSchema;
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
- /* eslint max-statements-per-line: ["error", { "max": 2 }] */
2
- import eachSchema from "../../eachSchema";
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 compile(rootSchema, force = false) {
23
+ export default function compileSchema(draft, schemaToCompile, rootSchema = schemaToCompile, force = false) {
25
24
  // @ts-ignore
26
- if (rootSchema === true || rootSchema === false) {
27
- return rootSchema;
25
+ if (schemaToCompile === true || schemaToCompile === false || schemaToCompile === undefined) {
26
+ return schemaToCompile;
28
27
  }
29
- if (rootSchema[COMPILED] !== undefined) {
30
- return rootSchema;
28
+ if (schemaToCompile[COMPILED] !== undefined) {
29
+ return schemaToCompile;
31
30
  } // eslint-disable-line
32
- const context = { ids: {}, remotes: Object.assign({}, remotes) };
33
- const rootSchemaAsString = JSON.stringify(rootSchema);
34
- rootSchema = JSON.parse(rootSchemaAsString);
35
- Object.defineProperty(rootSchema, COMPILED, { enumerable: false, value: true });
36
- Object.defineProperty(rootSchema, GET_REF, { enumerable: false, value: getRef.bind(null, context, rootSchema) });
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
- // bail early, when no $refs are defined
39
- return rootSchema;
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 = () => rootSchema;
43
- eachSchema(rootSchema, (schema, pointer) => {
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, { enumerable: false, value: joinScope(scope, schema.$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 rootSchema;
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 };