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.
Files changed (161) hide show
  1. package/.editorconfig +1 -0
  2. package/.prettierignore +1 -0
  3. package/.prettierrc +7 -0
  4. package/CHANGELOG.md +106 -0
  5. package/README.md +811 -199
  6. package/TASKS.md +3 -81
  7. package/dist/index.d.ts +29 -71
  8. package/dist/jsonSchemaLibrary.js +1 -1
  9. package/dist/lib/SchemaService.d.ts +6 -8
  10. package/dist/lib/{addSchema.d.ts → addRemoteSchema.d.ts} +2 -1
  11. package/dist/lib/addValidator.d.ts +3 -2
  12. package/dist/lib/compile/getRef.d.ts +2 -1
  13. package/dist/lib/compile/index.d.ts +15 -1
  14. package/dist/lib/compile/types.d.ts +5 -0
  15. package/dist/lib/config/strings.d.ts +1 -39
  16. package/dist/lib/draft/index.d.ts +132 -0
  17. package/dist/lib/draft04/index.d.ts +7 -0
  18. package/dist/lib/draft06/compile/index.d.ts +16 -0
  19. package/dist/lib/draft06/index.d.ts +7 -0
  20. package/dist/lib/draft06/validation/keyword.d.ts +3 -0
  21. package/dist/lib/draft06/validation/type.d.ts +10 -0
  22. package/dist/lib/draft06/validation/typeKeywordMapping.d.ts +13 -0
  23. package/dist/lib/draft07/index.d.ts +7 -0
  24. package/dist/lib/each.d.ts +3 -2
  25. package/dist/lib/eachSchema.d.ts +2 -4
  26. package/dist/lib/getChildSchemaSelection.d.ts +7 -5
  27. package/dist/lib/getSchema.d.ts +2 -2
  28. package/dist/lib/getTemplate.d.ts +1 -1
  29. package/dist/lib/getTypeOf.d.ts +2 -1
  30. package/dist/lib/isValid.d.ts +1 -1
  31. package/dist/lib/jsoneditor/index.d.ts +7 -0
  32. package/dist/lib/resolveAllOf.d.ts +1 -1
  33. package/dist/lib/resolveAnyOf.d.ts +1 -1
  34. package/dist/lib/resolveOneOf.fuzzy.d.ts +1 -1
  35. package/dist/lib/resolveOneOf.strict.d.ts +2 -2
  36. package/dist/lib/{resolveRef.withOverwrite.d.ts → resolveRef.merge.d.ts} +0 -0
  37. package/dist/lib/schema/getTypeDefs.d.ts +6 -4
  38. package/dist/lib/schema/getTypeId.d.ts +1 -1
  39. package/dist/lib/schema/types.d.ts +7 -57
  40. package/dist/lib/step.d.ts +5 -5
  41. package/dist/lib/types.d.ts +11 -3
  42. package/dist/lib/utils/createCustomError.d.ts +8 -11
  43. package/dist/lib/utils/filter.d.ts +4 -4
  44. package/dist/lib/utils/flattenArray.d.ts +1 -1
  45. package/dist/lib/utils/merge.d.ts +3 -0
  46. package/dist/lib/utils/punycode.ucs2decode.d.ts +1 -1
  47. package/dist/lib/validate.d.ts +2 -2
  48. package/dist/lib/validateAsync.d.ts +1 -1
  49. package/dist/lib/validation/errors.d.ts +1 -3
  50. package/dist/lib/validation/format.d.ts +4 -11
  51. package/dist/lib/validation/keyword.d.ts +2 -27
  52. package/dist/lib/validation/type.d.ts +3 -10
  53. package/dist/lib/validation/typeKeywordMapping.d.ts +4 -4
  54. package/dist/module/index.js +23 -32
  55. package/dist/module/lib/SchemaService.js +7 -4
  56. package/dist/module/lib/{addSchema.js → addRemoteSchema.js} +2 -4
  57. package/dist/module/lib/addValidator.js +3 -4
  58. package/dist/module/lib/compile/getRef.js +1 -1
  59. package/dist/module/lib/compile/index.js +43 -18
  60. package/dist/module/lib/compile/types.js +1 -0
  61. package/dist/module/lib/config/strings.js +15 -2
  62. package/dist/module/lib/createSchemaOf.js +1 -1
  63. package/dist/module/lib/draft/index.js +133 -0
  64. package/dist/module/lib/draft04/index.js +90 -0
  65. package/dist/module/lib/draft06/compile/index.js +77 -0
  66. package/dist/module/lib/draft06/index.js +96 -0
  67. package/dist/module/lib/draft06/validation/keyword.js +168 -0
  68. package/dist/module/lib/draft06/validation/type.js +31 -0
  69. package/dist/module/lib/draft06/validation/typeKeywordMapping.js +15 -0
  70. package/dist/module/lib/draft07/index.js +96 -0
  71. package/dist/module/lib/each.js +2 -2
  72. package/dist/module/lib/eachSchema.js +28 -19
  73. package/dist/module/lib/getChildSchemaSelection.js +7 -6
  74. package/dist/module/lib/getSchema.js +4 -2
  75. package/dist/module/lib/getTemplate.js +42 -15
  76. package/dist/module/lib/jsoneditor/index.js +16 -0
  77. package/dist/module/lib/resolveAllOf.js +3 -4
  78. package/dist/module/lib/resolveOneOf.fuzzy.js +13 -3
  79. package/dist/module/lib/resolveOneOf.strict.js +49 -2
  80. package/dist/module/lib/{resolveRef.withOverwrite.js → resolveRef.merge.js} +0 -0
  81. package/dist/module/lib/resolveRef.strict.js +8 -0
  82. package/dist/module/lib/schema/getTypeDefs.js +14 -3
  83. package/dist/module/lib/schema/getTypeId.js +10 -6
  84. package/dist/module/lib/schema/types.js +33 -9
  85. package/dist/module/lib/step.js +67 -13
  86. package/dist/module/lib/types.js +7 -1
  87. package/dist/module/lib/utils/createCustomError.js +4 -4
  88. package/dist/module/lib/utils/filter.js +3 -5
  89. package/dist/module/lib/utils/flattenArray.js +4 -3
  90. package/dist/module/lib/utils/merge.js +4 -0
  91. package/dist/module/lib/utils/punycode.ucs2decode.js +4 -3
  92. package/dist/module/lib/validate.js +34 -8
  93. package/dist/module/lib/validateAsync.js +7 -7
  94. package/dist/module/lib/validation/errors.js +16 -3
  95. package/dist/module/lib/validation/format.js +115 -8
  96. package/dist/module/lib/validation/keyword.js +79 -32
  97. package/dist/module/lib/validation/type.js +2 -1
  98. package/index.ts +46 -32
  99. package/lib/SchemaService.ts +18 -11
  100. package/lib/{addSchema.ts → addRemoteSchema.ts} +3 -5
  101. package/lib/addValidator.ts +15 -12
  102. package/lib/compile/getRef.ts +3 -4
  103. package/lib/compile/index.ts +65 -19
  104. package/lib/compile/types.ts +6 -0
  105. package/lib/config/strings.ts +17 -3
  106. package/lib/createSchemaOf.ts +1 -3
  107. package/lib/draft/index.ts +201 -0
  108. package/lib/draft04/index.ts +95 -0
  109. package/lib/draft06/compile/index.ts +104 -0
  110. package/lib/draft06/index.ts +101 -0
  111. package/lib/draft06/validation/keyword.ts +199 -0
  112. package/lib/draft06/validation/type.ts +47 -0
  113. package/lib/draft06/validation/typeKeywordMapping.ts +15 -0
  114. package/lib/draft07/index.ts +101 -0
  115. package/lib/each.ts +11 -4
  116. package/lib/eachSchema.ts +45 -32
  117. package/lib/getChildSchemaSelection.ts +14 -7
  118. package/lib/getSchema.ts +18 -9
  119. package/lib/getTemplate.ts +155 -42
  120. package/lib/getTypeOf.ts +2 -1
  121. package/lib/isValid.ts +7 -3
  122. package/lib/jsoneditor/index.ts +20 -0
  123. package/lib/resolveAllOf.ts +10 -6
  124. package/lib/resolveAnyOf.ts +7 -3
  125. package/lib/resolveOneOf.fuzzy.ts +26 -9
  126. package/lib/resolveOneOf.strict.ts +63 -5
  127. package/lib/{resolveRef.withOverwrite.ts → resolveRef.merge.ts} +0 -0
  128. package/lib/resolveRef.strict.ts +9 -0
  129. package/lib/schema/getTypeDefs.ts +19 -7
  130. package/lib/schema/getTypeId.ts +11 -8
  131. package/lib/schema/types.ts +41 -9
  132. package/lib/step.ts +109 -25
  133. package/lib/types.ts +23 -5
  134. package/lib/utils/createCustomError.ts +8 -13
  135. package/lib/utils/filter.ts +7 -9
  136. package/lib/utils/flattenArray.ts +5 -4
  137. package/lib/utils/merge.ts +5 -0
  138. package/lib/utils/punycode.ucs2decode.ts +6 -5
  139. package/lib/validate.ts +47 -16
  140. package/lib/validateAsync.ts +15 -14
  141. package/lib/validation/errors.ts +17 -6
  142. package/lib/validation/format.ts +147 -13
  143. package/lib/validation/keyword.ts +172 -103
  144. package/lib/validation/type.ts +5 -1
  145. package/package.json +75 -63
  146. package/{dist/module/remotes/draft04.json → remotes/draft06.json} +47 -42
  147. package/remotes/draft07.json +172 -0
  148. package/remotes/draft2019-09.json +86 -0
  149. package/tsconfig.json +3 -10
  150. package/dist/lib/cores/CoreInterface.d.ts +0 -23
  151. package/dist/lib/cores/Draft04.d.ts +0 -13
  152. package/dist/lib/cores/JsonEditor.d.ts +0 -13
  153. package/dist/module/lib/cores/CoreInterface.js +0 -51
  154. package/dist/module/lib/cores/Draft04.js +0 -52
  155. package/dist/module/lib/cores/JsonEditor.js +0 -51
  156. package/dist/module/remotes/index.js +0 -10
  157. package/dist/remotes/index.d.ts +0 -5
  158. package/lib/cores/CoreInterface.ts +0 -76
  159. package/lib/cores/Draft04.ts +0 -66
  160. package/lib/cores/JsonEditor.ts +0 -64
  161. package/remotes/index.ts +0 -11
@@ -1,5 +1,3 @@
1
1
  import { CreateError } from "../utils/createCustomError";
2
- declare const errors: {
3
- [p: string]: CreateError;
4
- };
2
+ declare const errors: Record<string, CreateError>;
5
3
  export default errors;
@@ -1,11 +1,4 @@
1
- declare const _default: {
2
- "date-time": (core: any, schema: any, value: any, pointer: any) => import("../types").JSONError;
3
- email: (core: any, schema: any, value: any, pointer: any) => import("../types").JSONError;
4
- hostname: (core: any, schema: any, value: any, pointer: any) => import("../types").JSONError;
5
- ipv4: (core: any, schema: any, value: any, pointer: any) => import("../types").JSONError;
6
- ipv6: (core: any, schema: any, value: any, pointer: any) => import("../types").JSONError;
7
- regex: (core: any, schema: any, value: any, pointer: any) => import("../types").JSONError;
8
- uri: (core: any, schema: any, value: any, pointer: any) => import("../types").JSONError;
9
- url: (core: any, schema: any, value: any, pointer: any) => import("../types").JSONError;
10
- };
11
- export default _default;
1
+ import { JSONError, JSONSchema } from "../types";
2
+ import { Draft } from "../draft";
3
+ declare const formatValidators: Record<string, (draft: Draft, schema: JSONSchema, value: unknown, pointer: string) => undefined | JSONError | JSONError[]>;
4
+ export default formatValidators;
@@ -1,28 +1,3 @@
1
- declare const KeywordValidation: {
2
- additionalProperties: (core: any, schema: any, value: any, pointer: any) => any[];
3
- allOf: (core: any, schema: any, value: any, pointer: any) => any[];
4
- anyOf: (core: any, schema: any, value: any, pointer: any) => any;
5
- dependencies: (core: any, schema: any, value: any, pointer: any) => any[];
6
- enum: (core: any, schema: any, value: any, pointer: any) => any;
7
- format: (core: any, schema: any, value: any, pointer: any) => any;
8
- items: (core: any, schema: any, value: any, pointer: any) => any[];
9
- maximum: (core: any, schema: any, value: any, pointer: any) => any;
10
- maxItems: (core: any, schema: any, value: any, pointer: any) => any;
11
- maxLength: (core: any, schema: any, value: any, pointer: any) => any;
12
- maxProperties: (core: any, schema: any, value: any, pointer: any) => any;
13
- minLength: (core: any, schema: any, value: any, pointer: any) => any;
14
- minimum: (core: any, schema: any, value: any, pointer: any) => any;
15
- minItems: (core: any, schema: any, value: any, pointer: any) => any;
16
- minProperties: (core: any, schema: any, value: any, pointer: any) => any;
17
- multipleOf: (core: any, schema: any, value: any, pointer: any) => any;
18
- not: (core: any, schema: any, value: any, pointer: any) => any[];
19
- oneOf: (core: any, schema: any, value: any, pointer: any) => any;
20
- pattern: (core: any, schema: any, value: any, pointer: any) => any;
21
- patternProperties: (core: any, schema: any, value: any, pointer: any) => any[];
22
- properties: (core: any, schema: any, value: any, pointer: any) => any[];
23
- propertiesRequired: (core: any, schema: any, value: any, pointer: any) => any[];
24
- required: (core: any, schema: any, value: any, pointer: any) => any;
25
- requiredNotEmpty: (core: any, schema: any, value: any, pointer: any) => any;
26
- uniqueItems: (core: any, schema: any, value: any, pointer: any) => any[];
27
- };
1
+ import { JSONValidator } from "../types";
2
+ declare const KeywordValidation: Record<string, JSONValidator>;
28
3
  export default KeywordValidation;
@@ -1,12 +1,4 @@
1
- declare const _default: {
2
- array: (core: any, schema: any, value: any, pointer: any) => any;
3
- object: (core: any, schema: any, value: any, pointer: any) => any;
4
- string: (core: any, schema: any, value: any, pointer: any) => any;
5
- integer: (core: any, schema: any, value: any, pointer: any) => any;
6
- number: (core: any, schema: any, value: any, pointer: any) => any;
7
- boolean: (core: any, schema: any, value: any, pointer: any) => any;
8
- null: (core: any, schema: any, value: any, pointer: any) => any;
9
- };
1
+ import { JSONTypeValidator } from "../types";
10
2
  /**
11
3
  * @todo: type is also a keyword, as is properties, items, etc
12
4
  *
@@ -14,4 +6,5 @@ declare const _default: {
14
6
  * or seven in case of ajv https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md#type
15
7
  * 1 null, 2 boolean, 3 object, 4 array, 5 number, 6 string (7 integer)
16
8
  */
17
- export default _default;
9
+ declare const typeValidators: Record<string, JSONTypeValidator>;
10
+ export default typeValidators;
@@ -1,3 +1,7 @@
1
+ /**
2
+ * Mapping, used in type validation to iterate over type-specific keywords to validate
3
+ * - overview https://epoberezkin.github.io/ajv/keywords.html
4
+ */
1
5
  declare const _default: {
2
6
  array: string[];
3
7
  boolean: string[];
@@ -6,8 +10,4 @@ declare const _default: {
6
10
  number: string[];
7
11
  null: string[];
8
12
  };
9
- /**
10
- * Mapping, used in type validation to iterate over type-specific keywords to validate
11
- * - overview https://epoberezkin.github.io/ajv/keywords.html
12
- */
13
13
  export default _default;
@@ -1,38 +1,29 @@
1
- import strings from "./lib/config/strings";
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 isValid from "./lib/isValid";
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
  const config = { strings };
23
- export { config, Interface, Draft04, // core implementing draft04 specs
19
+ export { config, Draft, Draft04, // core implementing draft04 specs
20
+ draft04Config, // config implementing draft04 specs
21
+ Draft06, // core implementing draft06 specs
22
+ draft06Config, // config implementing draft06 specs
23
+ Draft07, // core implementing draft07 specs
24
+ draft07Config, // config implementing draft07 specs
24
25
  JsonEditor, // adjusted core of draft04 to better support the json-editor
25
- addSchema, // add a schema to be references via $ref
26
- addValidator, // add validation for keyword, format, datatype and customize errors
27
- compileSchema, createCustomError, createSchemaOf, // creates a simple schema based on the given data
28
- each, // iterate over data, receiving each data-entry with its schema
29
- eachSchema, // iterates over a json-schemas type definitions
30
- getChildSchemaSelection, // get available child schemas
31
- getSchema, // get schema of datapointer
32
- getTemplate, // create data which is valid to the given schema
33
- getTypeOf, // returns the javascript datatype
34
- isValid, // returns a boolean if the schema is valid
35
- settings, SchemaService, step, // steps into a json-schema, returning the matching child-schema
36
- validate, // validates data by a schema
37
- 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
38
29
  };
@@ -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 "./compileSchema";
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
  }
@@ -22,11 +22,10 @@ function addFormat(core, formatType, validationFunction) {
22
22
  if (typeof validationFunction !== "function") {
23
23
  throw new Error(`Validation function expected. Received ${typeof validationFunction}`);
24
24
  }
25
- if (core.validateFormat[formatType] == null) {
26
- core.validateFormat[formatType] = validationFunction;
27
- return;
25
+ if (core.validateFormat[formatType]) {
26
+ throw new Error(`A format '${formatType}' is already registered to validation`);
28
27
  }
29
- throw new Error(`A format '${formatType}' is already registered to validation`);
28
+ core.validateFormat[formatType] = validationFunction;
30
29
  }
31
30
  /**
32
31
  * Adds a custom keyword validation to a specific type. May not override existing keywords.
@@ -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";
@@ -8,22 +7,45 @@ const COMPILED_REF = "__ref";
8
7
  const GET_REF = "getRef";
9
8
  const GET_ROOT = "getRoot";
10
9
  const suffixes = /(#|\/)+$/g;
11
- export default function compile(rootSchema, force = false) {
12
- if (rootSchema[COMPILED] !== undefined) {
13
- return rootSchema;
14
- } // eslint-disable-line
15
- const context = { ids: {}, remotes: Object.assign({}, remotes) };
16
- const rootSchemaAsString = JSON.stringify(rootSchema);
17
- rootSchema = JSON.parse(rootSchemaAsString);
18
- Object.defineProperty(rootSchema, COMPILED, { enumerable: false, value: true });
19
- Object.defineProperty(rootSchema, GET_REF, { enumerable: false, value: getRef.bind(null, context, rootSchema) });
10
+ /**
11
+ * compiles the input root schema for `$ref` resolution and returns it again
12
+ * @attention this modifies input schema but maintains object-structure
13
+ *
14
+ * for a compiled json-schema you can call getRef on any contained schema (location of type).
15
+ * this resolves a $ref target to a valid schema (for a valid $ref)
16
+ *
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
22
+ */
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
20
36
  if (force === false && rootSchemaAsString.includes("$ref") === false) {
21
- // bail early, when no $refs are defined
22
- 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
+ });
23
45
  }
24
46
  const scopes = {};
25
- const getRoot = () => rootSchema;
26
- eachSchema(rootSchema, (schema, pointer) => {
47
+ const getRoot = () => compiledSchema;
48
+ eachSchema(compiledSchema, (schema, pointer) => {
27
49
  if (schema.id) {
28
50
  context.ids[schema.id.replace(suffixes, "")] = pointer;
29
51
  }
@@ -37,13 +59,16 @@ export default function compile(rootSchema, force = false) {
37
59
  if (context.ids[scope] == null) {
38
60
  context.ids[scope] = pointer;
39
61
  }
40
- if (schema.$ref) {
41
- 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
+ });
42
67
  // @todo currently not used:
43
68
  Object.defineProperty(schema, GET_ROOT, { enumerable: false, value: getRoot });
44
69
  // console.log("compiled ref", scope, schema.$ref, "=>", joinScope(scope, schema.$ref));
45
70
  }
46
71
  });
47
72
  // console.log(JSON.stringify(context.ids, null, 2));
48
- return rootSchema;
73
+ return compiledSchema;
49
74
  }
@@ -0,0 +1 @@
1
+ export {};
@@ -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
- FormatUrlError: "Value `{{value}}` at `{{pointer}}` is not a valid url",
12
- FormatUriError: "Value `{{value}}` at `{{pointer}}` is not a valid uri",
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}}`.",
@@ -10,7 +10,7 @@ export default function createSchemaOf(data) {
10
10
  };
11
11
  if (schema.type === "object") {
12
12
  schema.properties = {};
13
- Object.keys(data).forEach(key => (schema.properties[key] = createSchemaOf(data[key])));
13
+ Object.keys(data).forEach((key) => (schema.properties[key] = createSchemaOf(data[key])));
14
14
  }
15
15
  if (schema.type === "array" && data.length === 1) {
16
16
  schema.items = createSchemaOf(data[0]);
@@ -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 };