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
@@ -29,7 +29,11 @@ const KeywordValidation = {
29
29
  return undefined;
30
30
  }
31
31
  if (schema.exclusiveMaximum <= value) {
32
- return core.errors.maximumError({ maximum: schema.exclusiveMaximum, length: value, pointer });
32
+ return core.errors.maximumError({
33
+ maximum: schema.exclusiveMaximum,
34
+ length: value,
35
+ pointer
36
+ });
33
37
  }
34
38
  return undefined;
35
39
  },
@@ -38,7 +42,11 @@ const KeywordValidation = {
38
42
  return undefined;
39
43
  }
40
44
  if (schema.exclusiveMinimum >= value) {
41
- return core.errors.minimumError({ minimum: schema.exclusiveMinimum, length: value, pointer });
45
+ return core.errors.minimumError({
46
+ minimum: schema.exclusiveMinimum,
47
+ length: value,
48
+ pointer
49
+ });
42
50
  }
43
51
  return undefined;
44
52
  },
@@ -81,11 +89,11 @@ const KeywordValidation = {
81
89
  }
82
90
  const errors = [];
83
91
  const keys = Object.keys(value);
84
- const patterns = Object.keys(pp).map(expr => ({
92
+ const patterns = Object.keys(pp).map((expr) => ({
85
93
  regex: new RegExp(expr),
86
94
  patternSchema: pp[expr]
87
95
  }));
88
- keys.forEach(key => {
96
+ keys.forEach((key) => {
89
97
  let patternFound = false;
90
98
  for (let i = 0, l = patterns.length; i < l; i += 1) {
91
99
  if (patterns[i].regex.test(key)) {
@@ -93,7 +101,9 @@ const KeywordValidation = {
93
101
  // for a boolean schema `false`, always invalidate
94
102
  if (patterns[i].patternSchema === false) {
95
103
  errors.push(core.errors.patternPropertiesError({
96
- key, pointer, patterns: Object.keys(pp).join(",")
104
+ key,
105
+ pointer,
106
+ patterns: Object.keys(pp).join(",")
97
107
  }));
98
108
  return;
99
109
  }
@@ -109,7 +119,9 @@ const KeywordValidation = {
109
119
  if (patternFound === false && schema.additionalProperties === false) {
110
120
  // this is an arrangement with additionalProperties
111
121
  errors.push(core.errors.patternPropertiesError({
112
- key, pointer, patterns: Object.keys(pp).join(",")
122
+ key,
123
+ pointer,
124
+ patterns: Object.keys(pp).join(",")
113
125
  }));
114
126
  }
115
127
  });
@@ -139,7 +151,7 @@ const KeywordValidation = {
139
151
  const errors = [];
140
152
  const properties = Object.keys(value);
141
153
  const propertySchema = { ...schema.propertyNames, type: "string" };
142
- properties.forEach(prop => {
154
+ properties.forEach((prop) => {
143
155
  const validationResult = core.validate(prop, propertySchema, `${pointer}/${prop}`);
144
156
  if (validationResult.length > 0) {
145
157
  errors.push(core.errors.invalidPropertyNameError({
@@ -5,26 +5,27 @@
5
5
  * or seven in case of ajv https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md#type
6
6
  * 1 null, 2 boolean, 3 object, 4 array, 5 number, 6 string (7 integer)
7
7
  */
8
- export default {
8
+ const validationType = {
9
9
  array: (core, schema, value, pointer) => core.typeKeywords.array
10
- .filter(key => schema && schema[key] != null)
11
- .map(key => core.validateKeyword[key](core, schema, value, pointer)),
10
+ .filter((key) => schema && schema[key] != null)
11
+ .map((key) => core.validateKeyword[key](core, schema, value, pointer)),
12
12
  object: (core, schema, value, pointer) => core.typeKeywords.object
13
- .filter(key => schema && schema[key] != null)
14
- .map(key => core.validateKeyword[key](core, schema, value, pointer)),
13
+ .filter((key) => schema && schema[key] != null)
14
+ .map((key) => core.validateKeyword[key](core, schema, value, pointer)),
15
15
  string: (core, schema, value, pointer) => core.typeKeywords.string
16
- .filter(key => schema && schema[key] != null)
17
- .map(key => core.validateKeyword[key](core, schema, value, pointer)),
16
+ .filter((key) => schema && schema[key] != null)
17
+ .map((key) => core.validateKeyword[key](core, schema, value, pointer)),
18
18
  integer: (core, schema, value, pointer) => core.typeKeywords.number
19
- .filter(key => schema && schema[key] != null)
20
- .map(key => core.validateKeyword[key](core, schema, value, pointer)),
19
+ .filter((key) => schema && schema[key] != null)
20
+ .map((key) => core.validateKeyword[key](core, schema, value, pointer)),
21
21
  number: (core, schema, value, pointer) => core.typeKeywords.number
22
- .filter(key => schema && schema[key] != null)
23
- .map(key => core.validateKeyword[key](core, schema, value, pointer)),
24
- "boolean": (core, schema, value, pointer) => core.typeKeywords.boolean
25
- .filter(key => schema && schema[key] != null)
26
- .map(key => core.validateKeyword[key](core, schema, value, pointer)),
27
- "null": (core, schema, value, pointer) => core.typeKeywords.null
28
- .filter(key => schema && schema[key] != null)
29
- .map(key => core.validateKeyword[key](core, schema, value, pointer))
22
+ .filter((key) => schema && schema[key] != null)
23
+ .map((key) => core.validateKeyword[key](core, schema, value, pointer)),
24
+ boolean: (core, schema, value, pointer) => core.typeKeywords.boolean
25
+ .filter((key) => schema && schema[key] != null)
26
+ .map((key) => core.validateKeyword[key](core, schema, value, pointer)),
27
+ null: (core, schema, value, pointer) => core.typeKeywords.null
28
+ .filter((key) => schema && schema[key] != null)
29
+ .map((key) => core.validateKeyword[key](core, schema, value, pointer))
30
30
  };
31
+ export default validationType;
@@ -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 draft07Config = {
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 Draft07 extends Draft {
92
+ constructor(schema, config = {}) {
93
+ super(merge(draft07Config, config), schema);
94
+ }
95
+ }
96
+ export { Draft07, draft07Config };
@@ -8,7 +8,7 @@ import getTypeOf from "./getTypeOf";
8
8
  * @param [schema] - the schema matching the data. Defaults to rootSchema
9
9
  * @param [pointer] - pointer to current data. Default to rootPointer
10
10
  */
11
- export default function each(core, data, callback, schema = core.rootSchema, pointer = "#") {
11
+ export function each(core, data, callback, schema = core.rootSchema, pointer = "#") {
12
12
  callback(schema, data, pointer);
13
13
  const dataType = getTypeOf(data);
14
14
  if (dataType === "object") {
@@ -1,35 +1,44 @@
1
1
  import gp from "gson-pointer";
2
2
  import getTypeDefs from "./schema/getTypeDefs";
3
- const isObject = value => Object.prototype.toString.call(value) === "[object Object]";
3
+ const isObject = (value) => Object.prototype.toString.call(value) === "[object Object]";
4
4
  function nextTypeDefs(schema, pointer) {
5
- if (this.callback(schema, pointer) === true) { // eslint-disable-line no-invalid-this
5
+ if (this.callback(schema, pointer) === true) {
6
+ // eslint-disable-line no-invalid-this
6
7
  return; // stop iteration
7
8
  }
8
9
  const defs = getTypeDefs(schema);
9
10
  // eslint-disable-next-line no-invalid-this
10
- defs.forEach(next => this.nextTypeDefs(next.def, gp.join(pointer, next.pointer, false)));
11
+ defs.forEach((next) => this.nextTypeDefs(next.def, gp.join(pointer, next.pointer, false)));
11
12
  }
12
- function eachDefinition(walk, schema, pointer) {
13
- Object.keys(schema.definitions)
14
- .forEach(defId => {
15
- if (schema.definitions[defId] === false || isObject(schema.definitions[defId])) {
16
- walk.nextTypeDefs(schema.definitions[defId], gp.join(pointer, "definitions", defId, false));
13
+ function eachDefinition(walk, schema, pointer, key = "definitions") {
14
+ const defs = schema[key];
15
+ Object.keys(defs).forEach((defId) => {
16
+ if (defs[defId] === false || isObject(defs[defId])) {
17
+ walk.nextTypeDefs(defs[defId], gp.join(pointer, key, defId, false));
17
18
  return;
18
19
  }
19
- console.log(`Invalid schema in ${pointer}/definitions/${defId}`);
20
+ console.log(`Invalid schema in ${pointer}/${key}/${defId}`);
20
21
  });
21
22
  }
22
- export default function eachSchema(schema, callback, pointer = "#") {
23
+ export function eachSchema(schema, callback, pointer = "#") {
23
24
  const walk = { callback, nextTypeDefs };
24
25
  walk.nextTypeDefs(schema, pointer);
25
- if (schema.definitions == null) {
26
- return;
26
+ if (schema.definitions != null) {
27
+ walk.callback = (defschema, schemaPointer) => {
28
+ callback(defschema, schemaPointer);
29
+ if (defschema.definitions != null) {
30
+ eachDefinition(walk, defschema, schemaPointer);
31
+ }
32
+ };
33
+ eachDefinition(walk, schema, pointer);
34
+ }
35
+ if (schema.$defs != null) {
36
+ walk.callback = (defschema, schemaPointer) => {
37
+ callback(defschema, schemaPointer);
38
+ if (defschema.definitions != null) {
39
+ eachDefinition(walk, defschema, schemaPointer);
40
+ }
41
+ };
42
+ eachDefinition(walk, schema, pointer, "$defs");
27
43
  }
28
- walk.callback = (defschema, schemaPointer) => {
29
- callback(defschema, schemaPointer);
30
- if (defschema.definitions != null) {
31
- eachDefinition(walk, defschema, schemaPointer);
32
- }
33
- };
34
- eachDefinition(walk, schema, pointer);
35
44
  }
@@ -4,7 +4,7 @@ import { isJSONError } from "./types";
4
4
  * could be added at the given property (e.g. item-index), thus an array of options is returned. In all other cases
5
5
  * a list with a single item will be returned
6
6
  *
7
- * @param core - core to use
7
+ * @param core - core to use
8
8
  * @param property - parent schema of following property
9
9
  * @param [schema] - parent schema of following property
10
10
  * @return
@@ -20,13 +20,14 @@ export default function getSchema(core, pointer, data, schema = core.rootSchema)
20
20
  }
21
21
  function _get(core, schema, frags, pointer, data = emptyObject) {
22
22
  if (frags.length === 0) {
23
- return schema;
23
+ return core.resolveRef(schema);
24
24
  }
25
25
  const key = frags.shift(); // step key
26
26
  schema = core.step(key, schema, data, pointer); // step schema
27
27
  if (isJSONError(schema)) {
28
28
  return schema;
29
29
  }
30
+ // @ts-ignore
30
31
  data = data[key]; // step data
31
32
  return _get(core, schema, frags, `${pointer}/${key}`, data);
32
33
  }
@@ -197,8 +197,8 @@ const TYPE = {
197
197
  // build array type of items, ignores additionalItems
198
198
  array: (core, schema, data, pointer, opts) => {
199
199
  const template = schema.default === undefined ? [] : schema.default;
200
- const d = data || [];
201
200
  schema.minItems = schema.minItems || 0;
201
+ const d = data || [];
202
202
  // items are undefined
203
203
  if (schema.items == null) {
204
204
  return d;
@@ -268,6 +268,6 @@ function getDefault(schema, templateValue, initValue) {
268
268
  return schema.default;
269
269
  }
270
270
  export default (core, data, schema = core.rootSchema, opts = defaultOptions) => {
271
- cache = { mi: ".." };
271
+ cache = { mi: {} };
272
272
  return getTemplate(core, data, schema, "#", opts);
273
273
  };
@@ -0,0 +1,16 @@
1
+ import merge from "../utils/merge";
2
+ import resolveOneOf from "../resolveOneOf.fuzzy";
3
+ import resolveRef from "../resolveRef.merge";
4
+ import { Draft } from "../draft";
5
+ import { draft07Config } from "../draft07";
6
+ const draftJsonEditorConfig = {
7
+ ...draft07Config,
8
+ resolveOneOf,
9
+ resolveRef
10
+ };
11
+ class JsonEditor extends Draft {
12
+ constructor(schema, config = {}) {
13
+ super(merge(draftJsonEditorConfig, config), schema);
14
+ }
15
+ }
16
+ export { JsonEditor, draftJsonEditorConfig };
@@ -1,5 +1,8 @@
1
1
  import { errorOrPromise } from "./utils/filter";
2
2
  import flattenArray from "./utils/flattenArray";
3
+ import settings from "./config/settings";
4
+ import { isJSONError } from "./types";
5
+ const { DECLARATOR_ONEOF } = settings;
3
6
  /**
4
7
  * Selects and returns a oneOf schema for the given data
5
8
  *
@@ -10,6 +13,41 @@ import flattenArray from "./utils/flattenArray";
10
13
  * @return oneOf schema or an error
11
14
  */
12
15
  export default function resolveOneOf(core, data, schema = core.rootSchema, pointer = "#") {
16
+ // !keyword: oneOfProperty
17
+ // an additional <DECLARATOR_ONEOF> (default `oneOfProperty`) on the schema will exactly determine the
18
+ // oneOf value (if set in data)
19
+ // @fixme
20
+ // abort if no data is given an DECLARATOR_ONEOF is set (used by getChildSchemaSelection)
21
+ // this case (data != null) should not be necessary
22
+ if (data != null && schema[DECLARATOR_ONEOF]) {
23
+ const errors = [];
24
+ const oneOfProperty = schema[DECLARATOR_ONEOF];
25
+ const oneOfValue = data[schema[DECLARATOR_ONEOF]];
26
+ if (oneOfValue === undefined) {
27
+ return core.errors.missingOneOfPropertyError({ property: oneOfProperty, pointer });
28
+ }
29
+ for (let i = 0; i < schema.oneOf.length; i += 1) {
30
+ const one = core.resolveRef(schema.oneOf[i]);
31
+ const oneOfPropertySchema = core.step(oneOfProperty, one, data, pointer);
32
+ if (isJSONError(oneOfPropertySchema)) {
33
+ return oneOfPropertySchema;
34
+ }
35
+ let result = flattenArray(core.validate(oneOfValue, oneOfPropertySchema, pointer));
36
+ result = result.filter(errorOrPromise);
37
+ if (result.length > 0) {
38
+ errors.push(...result);
39
+ }
40
+ else {
41
+ return one; // return resolved schema
42
+ }
43
+ }
44
+ return core.errors.oneOfPropertyError({
45
+ property: oneOfProperty,
46
+ value: oneOfValue,
47
+ pointer,
48
+ errors
49
+ });
50
+ }
13
51
  const matches = [];
14
52
  const errors = [];
15
53
  for (let i = 0; i < schema.oneOf.length; i += 1) {
@@ -30,13 +68,13 @@ export default function resolveOneOf(core, data, schema = core.rootSchema, point
30
68
  return core.errors.multipleOneOfError({
31
69
  value: data,
32
70
  pointer,
33
- matches,
71
+ matches
34
72
  });
35
73
  }
36
74
  return core.errors.oneOfError({
37
75
  value: JSON.stringify(data),
38
76
  pointer,
39
77
  oneOf: schema.oneOf,
40
- errors,
78
+ errors
41
79
  });
42
80
  }
@@ -2,7 +2,7 @@ import gp from "gson-pointer";
2
2
  import { get } from "gson-query";
3
3
  import getTypeId from "./getTypeId";
4
4
  import types from "./types";
5
- const isObject = value => Object.prototype.toString.call(value) === "[object Object]";
5
+ const isObject = (value) => Object.prototype.toString.call(value) === "[object Object]";
6
6
  /**
7
7
  * Returns a list of all (direct) type definitions from the given schema
8
8
  * @param schema
@@ -29,7 +29,7 @@ export default function getTypeDefs(schema) {
29
29
  if (type.definitions == null) {
30
30
  return defs;
31
31
  }
32
- type.definitions.forEach(query => {
32
+ type.definitions.forEach((query) => {
33
33
  get(schema, query, (value, key, parent, pointer) => {
34
34
  if (isObject(value) && getTypeId(value)) {
35
35
  defs.push({ pointer: gp.join(gp.split(pointer), false), def: value });
@@ -1,6 +1,7 @@
1
1
  import types from "./types";
2
- const isObject = value => Object.prototype.toString.call(value) === "[object Object]";
3
- const typeKeywords = Object.keys(types).filter(id => types[id].type === false);
2
+ const isObject = (value) => Object.prototype.toString.call(value) === "[object Object]";
3
+ const typeKeywords = Object.keys(types).filter((id) => types[id].type === false);
4
+ const hasOwnProperty = Object.prototype.hasOwnProperty;
4
5
  /**
5
6
  * @throws Error on multiple matches (invalid schema)
6
7
  *
@@ -15,10 +16,11 @@ export default function getTypeId(schema) {
15
16
  if (schema.enum) {
16
17
  return "enum";
17
18
  }
19
+ // @ts-ignore
18
20
  if (types[schema.type] || Array.isArray(schema.type)) {
19
21
  return schema.type;
20
22
  }
21
- const ids = typeKeywords.filter(type => schema[type]);
23
+ const ids = typeKeywords.filter((type) => schema[type]);
22
24
  if (ids.length === 1) {
23
25
  return ids[0];
24
26
  }
@@ -26,14 +28,16 @@ export default function getTypeId(schema) {
26
28
  // @expensive, guess type object
27
29
  for (let i = 0, l = types.object.keywords.length; i < l; i += 1) {
28
30
  const keyword = types.object.keywords[i];
29
- if (schema.hasOwnProperty(keyword)) { // eslint-disable-line
31
+ if (hasOwnProperty.call(schema, keyword)) {
32
+ // eslint-disable-line
30
33
  return "object";
31
34
  }
32
35
  }
33
36
  // @expensive, guess type array
34
37
  for (let i = 0, l = types.array.keywords.length; i < l; i += 1) {
35
38
  const keyword = types.array.keywords[i];
36
- if (schema.hasOwnProperty(keyword)) { // eslint-disable-line
39
+ if (hasOwnProperty.call(schema, keyword)) {
40
+ // eslint-disable-line
37
41
  return "array";
38
42
  }
39
43
  }
@@ -1,4 +1,4 @@
1
- export default {
1
+ const Types = {
2
2
  $ref: {
3
3
  type: false
4
4
  },
@@ -13,14 +13,22 @@ export default {
13
13
  array: {
14
14
  type: true,
15
15
  // ignore additionalItems:TypeDef, when items:TypeDef
16
- definitions: ["allOf/*", "anyOf/*", "oneOf/*", "not", "items", "items/*", "additionalItems"],
16
+ definitions: [
17
+ "allOf/*",
18
+ "anyOf/*",
19
+ "oneOf/*",
20
+ "not",
21
+ "items",
22
+ "items/*",
23
+ "additionalItems"
24
+ ],
17
25
  validationKeywords: ["minItems", "maxItems", "uniqueItems"],
18
26
  keywords: ["items", "additionalItems", "minItems", "maxItems", "uniqueItems"]
19
27
  },
20
- "boolean": {
28
+ boolean: {
21
29
  type: true
22
30
  },
23
- "enum": {
31
+ enum: {
24
32
  type: false
25
33
  },
26
34
  integer: {
@@ -37,7 +45,7 @@ export default {
37
45
  definitions: ["allOf/*", "anyOf/*", "oneOf/*", "not"],
38
46
  validationKeywords: ["minimum", "maximum", "multipleOf"]
39
47
  },
40
- "null": {
48
+ null: {
41
49
  type: true
42
50
  },
43
51
  object: {
@@ -45,11 +53,26 @@ export default {
45
53
  // patternProperties also validate properties
46
54
  // dependencies:(string, TypeDef) extend current TypeDef
47
55
  // additional Properties validate only remaining properties (after properties & pattern)
48
- definitions: ["allOf/*", "anyOf/*", "oneOf/*", "not",
49
- "properties/*", "additionalProperties", "patternProperties/*", "dependencies/*"],
56
+ definitions: [
57
+ "allOf/*",
58
+ "anyOf/*",
59
+ "oneOf/*",
60
+ "not",
61
+ "properties/*",
62
+ "additionalProperties",
63
+ "patternProperties/*",
64
+ "dependencies/*"
65
+ ],
50
66
  validationKeywords: ["minProperties", "maxProperties", "required"],
51
- keywords: ["properties", "additionalProperties", "patternProperties", "dependencies", "minProperties",
52
- "maxProperties", "required"]
67
+ keywords: [
68
+ "properties",
69
+ "additionalProperties",
70
+ "patternProperties",
71
+ "dependencies",
72
+ "minProperties",
73
+ "maxProperties",
74
+ "required"
75
+ ]
53
76
  },
54
77
  oneOf: {
55
78
  type: false,
@@ -61,3 +84,4 @@ export default {
61
84
  validationKeywords: ["minLength", "maxLength", "pattern"]
62
85
  }
63
86
  };
87
+ export default Types;
@@ -168,7 +168,8 @@ export default function step(core, key, schema, data, pointer = "#") {
168
168
  if (Array.isArray(schema.type)) {
169
169
  const dataType = getTypeOf(data);
170
170
  if (schema.type.includes(dataType)) {
171
- return stepType[dataType](core, key, schema, data, pointer);
171
+ // @ts-ignore
172
+ return stepType[dataType](core, `${key}`, schema, data, pointer);
172
173
  }
173
174
  return core.errors.typeError({
174
175
  value: data,
@@ -178,8 +179,10 @@ export default function step(core, key, schema, data, pointer = "#") {
178
179
  });
179
180
  }
180
181
  const expectedType = schema.type || getTypeOf(data);
181
- if (stepType[expectedType]) {
182
- return stepType[expectedType](core, key, schema, data, pointer);
182
+ // @ts-ignore
183
+ const stepFunction = stepType[expectedType];
184
+ if (stepFunction) {
185
+ return stepFunction(core, `${key}`, schema, data, pointer);
183
186
  }
184
187
  return new Error(`Unsupported schema type ${schema.type} for key ${key}`);
185
188
  }
@@ -1,6 +1,6 @@
1
1
  import __ from "./__";
2
- function dashCase(str) {
3
- return str.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
2
+ function dashCase(text) {
3
+ return text.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
4
4
  }
5
5
  export function createError(name, data) {
6
6
  return {
@@ -13,11 +13,11 @@ export function createError(name, data) {
13
13
  }
14
14
  /**
15
15
  * Creates a custom Error-Constructor which instances may be identified by `customError instanceof Error`. Its messages
16
- * are defined by strings-object __@see config/strings.ts
16
+ * are defined by strings-object @see config/strings.ts
17
17
  *
18
18
  * @param name - id of error (camelcased)
19
19
  * @return error constructor function
20
20
  */
21
- export default function createCustomError(name) {
21
+ export function createCustomError(name) {
22
22
  return createError.bind(null, name);
23
23
  }
@@ -1,10 +1,11 @@
1
1
  export default function flattenArray(list, result = []) {
2
2
  for (let i = 0; i < list.length; i += 1) {
3
- if (Array.isArray(list[i])) {
4
- flattenArray(list[i], result);
3
+ const item = list[i];
4
+ if (Array.isArray(item)) {
5
+ flattenArray(item, result);
5
6
  }
6
7
  else {
7
- result.push(list[i]);
8
+ result.push(item);
8
9
  }
9
10
  }
10
11
  return result;
@@ -1,4 +1,5 @@
1
1
  import deepmerge from "deepmerge";
2
+ // @ts-ignore
2
3
  const overwriteMerge = (destinationArray, sourceArray) => sourceArray;
3
4
  /**
4
5
  * returns a new json-schema, where properties are combined and arrays are replaced
@@ -20,12 +20,13 @@ export default function ucs2decode(string) {
20
20
  const length = string.length;
21
21
  while (counter < length) {
22
22
  const value = string.charCodeAt(counter++);
23
- if (value >= 0xD800 && value <= 0xDBFF && counter < length) {
23
+ if (value >= 0xd800 && value <= 0xdbff && counter < length) {
24
24
  // It's a high surrogate, and there is a next character.
25
25
  const extra = string.charCodeAt(counter++);
26
26
  // eslint-disable-next-line eqeqeq
27
- if ((extra & 0xFC00) == 0xDC00) { // Low surrogate.
28
- output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
27
+ if ((extra & 0xfc00) == 0xdc00) {
28
+ // Low surrogate.
29
+ output.push(((value & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000);
29
30
  }
30
31
  else {
31
32
  // It's an unmatched surrogate; only append this code unit, in case the
@@ -58,5 +58,6 @@ export default function validate(core, value, schema = core.rootSchema, pointer
58
58
  }
59
59
  const errors = flattenArray(core.validateType[receivedType](core, schema, value, pointer));
60
60
  // also promises may be passed along (validateAsync)
61
+ // @ts-ignore
61
62
  return errors.filter(errorOrPromise);
62
63
  }
@@ -1,5 +1,5 @@
1
1
  /* eslint no-invalid-this: 0 */
2
- import createCustomError from "../utils/createCustomError";
2
+ import { createCustomError } from "../utils/createCustomError";
3
3
  const errors = {
4
4
  additionalItemsError: createCustomError("AdditionalItemsError"),
5
5
  additionalPropertiesError: createCustomError("AdditionalPropertiesError"),