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
@@ -0,0 +1,47 @@
1
+ import { JSONTypeValidator } from "../../types";
2
+
3
+ /**
4
+ * @todo: type is also a keyword, as is properties, items, etc
5
+ *
6
+ * An instance has one of six primitive types (http://json-schema.org/latest/json-schema-core.html#rfc.section.4.2)
7
+ * or seven in case of ajv https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md#type
8
+ * 1 null, 2 boolean, 3 object, 4 array, 5 number, 6 string (7 integer)
9
+ */
10
+ const validationType: Record<string, JSONTypeValidator> = {
11
+ array: (core, schema, value, pointer) =>
12
+ core.typeKeywords.array
13
+ .filter((key) => schema && schema[key] != null)
14
+ .map((key) => core.validateKeyword[key](core, schema, value, pointer)),
15
+
16
+ object: (core, schema, value, pointer) =>
17
+ core.typeKeywords.object
18
+ .filter((key) => schema && schema[key] != null)
19
+ .map((key) => core.validateKeyword[key](core, schema, value, pointer)),
20
+
21
+ string: (core, schema, value, pointer) =>
22
+ core.typeKeywords.string
23
+ .filter((key) => schema && schema[key] != null)
24
+ .map((key) => core.validateKeyword[key](core, schema, value, pointer)),
25
+
26
+ integer: (core, schema, value, pointer) =>
27
+ core.typeKeywords.number
28
+ .filter((key) => schema && schema[key] != null)
29
+ .map((key) => core.validateKeyword[key](core, schema, value, pointer)),
30
+
31
+ number: (core, schema, value, pointer) =>
32
+ core.typeKeywords.number
33
+ .filter((key) => schema && schema[key] != null)
34
+ .map((key) => core.validateKeyword[key](core, schema, value, pointer)),
35
+
36
+ boolean: (core, schema, value, pointer) =>
37
+ core.typeKeywords.boolean
38
+ .filter((key) => schema && schema[key] != null)
39
+ .map((key) => core.validateKeyword[key](core, schema, value, pointer)),
40
+
41
+ null: (core, schema, value, pointer) =>
42
+ core.typeKeywords.null
43
+ .filter((key) => schema && schema[key] != null)
44
+ .map((key) => core.validateKeyword[key](core, schema, value, pointer))
45
+ };
46
+
47
+ export default validationType;
@@ -0,0 +1,15 @@
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
+ */
5
+ export default {
6
+ array: ["enum", "contains", "items", "minItems", "maxItems", "uniqueItems", "not", "if"],
7
+ boolean: ["enum", "not"],
8
+ object: [
9
+ "additionalProperties", "dependencies", "enum", "format", "minProperties", "maxProperties",
10
+ "patternProperties", "properties", "propertyNames", "required", "not", "oneOf", "allOf", "anyOf", "if"
11
+ ],
12
+ string: ["enum", "format", "maxLength", "minLength", "pattern", "not", "oneOf", "allOf", "anyOf", "if"],
13
+ number: ["enum", "exclusiveMaximum", "exclusiveMinimum", "format", "maximum", "minimum", "multipleOf", "not", "oneOf", "allOf", "anyOf", "if"],
14
+ null: ["enum", "format", "not", "oneOf", "allOf", "anyOf"]
15
+ };
@@ -0,0 +1,101 @@
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 { DraftConfig, Draft } from "../draft";
22
+ import { JSONSchema } from "../types";
23
+
24
+ const draft07Config: DraftConfig = {
25
+ typeKeywords: {
26
+ array: ["enum", "contains", "items", "minItems", "maxItems", "uniqueItems", "not", "if"],
27
+ boolean: ["enum", "not"],
28
+ object: [
29
+ "additionalProperties",
30
+ "dependencies",
31
+ "enum",
32
+ "format",
33
+ "minProperties",
34
+ "maxProperties",
35
+ "patternProperties",
36
+ "properties",
37
+ "propertyNames",
38
+ "required",
39
+ "not",
40
+ "oneOf",
41
+ "allOf",
42
+ "anyOf",
43
+ "if"
44
+ ],
45
+ string: [
46
+ "enum",
47
+ "format",
48
+ "maxLength",
49
+ "minLength",
50
+ "pattern",
51
+ "not",
52
+ "oneOf",
53
+ "allOf",
54
+ "anyOf",
55
+ "if"
56
+ ],
57
+ number: [
58
+ "enum",
59
+ "exclusiveMaximum",
60
+ "exclusiveMinimum",
61
+ "format",
62
+ "maximum",
63
+ "minimum",
64
+ "multipleOf",
65
+ "not",
66
+ "oneOf",
67
+ "allOf",
68
+ "anyOf",
69
+ "if"
70
+ ],
71
+ null: ["enum", "format", "not", "oneOf", "allOf", "anyOf"]
72
+ },
73
+ validateKeyword: KEYWORDS,
74
+ validateType: TYPES,
75
+ validateFormat: FORMATS,
76
+ errors: ERRORS,
77
+
78
+ addRemoteSchema,
79
+ compileSchema,
80
+ createSchemaOf,
81
+ each,
82
+ eachSchema,
83
+ getChildSchemaSelection,
84
+ getSchema,
85
+ getTemplate,
86
+ isValid,
87
+ resolveAllOf,
88
+ resolveAnyOf,
89
+ resolveOneOf,
90
+ resolveRef,
91
+ step,
92
+ validate
93
+ };
94
+
95
+ class Draft07 extends Draft {
96
+ constructor(schema?: JSONSchema, config: Partial<DraftConfig> = {}) {
97
+ super(merge(draft07Config, config), schema);
98
+ }
99
+ }
100
+
101
+ export { Draft07, draft07Config };
package/lib/each.ts CHANGED
@@ -1,7 +1,8 @@
1
- import Core from "./cores/CoreInterface";
1
+ import { Draft as Core } from "./draft";
2
2
  import getTypeOf from "./getTypeOf";
3
3
  import { JSONSchema, JSONPointer } from "./types";
4
4
 
5
+ export type EachCallback = (schema: JSONSchema, data: unknown, pointer: JSONPointer) => void;
5
6
 
6
7
  /**
7
8
  * Iterates over data, retrieving its schema
@@ -12,18 +13,24 @@ import { JSONSchema, JSONPointer } from "./types";
12
13
  * @param [schema] - the schema matching the data. Defaults to rootSchema
13
14
  * @param [pointer] - pointer to current data. Default to rootPointer
14
15
  */
15
- export default function each(core: Core, data: any, callback, schema: JSONSchema = core.rootSchema, pointer: JSONPointer = "#") {
16
+ export function each(
17
+ core: Core,
18
+ data: any,
19
+ callback: EachCallback,
20
+ schema: JSONSchema = core.rootSchema,
21
+ pointer: JSONPointer = "#"
22
+ ) {
16
23
  callback(schema, data, pointer);
17
24
  const dataType = getTypeOf(data);
18
25
 
19
26
  if (dataType === "object") {
20
- Object.keys(data).forEach(key => {
27
+ Object.keys(data).forEach((key) => {
21
28
  const nextSchema = core.step(key, schema, data, pointer); // not save
22
29
  const next = data[key]; // save
23
30
  core.each(next, callback, nextSchema, `${pointer}/${key}`);
24
31
  });
25
32
  } else if (dataType === "array") {
26
- data.forEach((next, key) => {
33
+ data.forEach((next: unknown, key: number) => {
27
34
  const nextSchema = core.step(key, schema, data, pointer);
28
35
  core.each(next, callback, nextSchema, `${pointer}/${key}`);
29
36
  });
package/lib/eachSchema.ts CHANGED
@@ -2,57 +2,70 @@ import gp from "gson-pointer";
2
2
  import getTypeDefs from "./schema/getTypeDefs";
3
3
  import { JSONSchema, JSONPointer } from "./types";
4
4
 
5
-
6
- export interface OnSchema {
7
- (schema: JSONSchema, pointer: JSONPointer): void;
8
- }
5
+ export type EachSchemaCallback = (schema: JSONSchema, pointer: JSONPointer) => void;
9
6
 
10
7
  type Walker = {
11
8
  nextTypeDefs: typeof nextTypeDefs;
12
- callback: OnSchema;
13
- }
14
-
15
-
16
- const isObject = value => Object.prototype.toString.call(value) === "[object Object]";
9
+ callback: EachSchemaCallback;
10
+ };
17
11
 
12
+ const isObject = (value: unknown): value is Record<string, unknown> =>
13
+ Object.prototype.toString.call(value) === "[object Object]";
18
14
 
19
15
  function nextTypeDefs(schema: JSONSchema, pointer: JSONPointer) {
20
- if (this.callback(schema, pointer) === true) { // eslint-disable-line no-invalid-this
16
+ if (this.callback(schema, pointer) === true) {
17
+ // eslint-disable-line no-invalid-this
21
18
  return; // stop iteration
22
19
  }
23
20
 
24
21
  const defs = getTypeDefs(schema);
25
22
  // eslint-disable-next-line no-invalid-this
26
- defs.forEach(next => this.nextTypeDefs(next.def, gp.join(pointer, next.pointer, false)));
23
+ defs.forEach((next) => this.nextTypeDefs(next.def, gp.join(pointer, next.pointer, false)));
27
24
  }
28
25
 
29
-
30
- function eachDefinition(walk: Walker, schema: JSONSchema, pointer: JSONPointer) {
31
- Object.keys(schema.definitions)
32
- .forEach(defId => {
33
- if (!isObject(schema.definitions[defId])) {
34
- console.log(`Invalid schema in ${pointer}/definitions/${defId}`);
35
- return;
36
- }
37
- walk.nextTypeDefs(schema.definitions[defId], gp.join(pointer, "definitions", defId, false));
38
- });
26
+ function eachDefinition(
27
+ walk: Walker,
28
+ schema: JSONSchema,
29
+ pointer: JSONPointer,
30
+ key = "definitions"
31
+ ) {
32
+ const defs = schema[key];
33
+ Object.keys(defs).forEach((defId) => {
34
+ if (defs[defId] === false || isObject(defs[defId])) {
35
+ walk.nextTypeDefs(defs[defId], gp.join(pointer, key, defId, false));
36
+ return;
37
+ }
38
+ console.log(`Invalid schema in ${pointer}/${key}/${defId}`);
39
+ });
39
40
  }
40
41
 
41
-
42
- export default function eachSchema(schema: JSONSchema, callback: OnSchema, pointer: JSONPointer = "#") {
42
+ export function eachSchema(
43
+ schema: JSONSchema,
44
+ callback: EachSchemaCallback,
45
+ pointer: JSONPointer = "#"
46
+ ) {
43
47
  const walk = { callback, nextTypeDefs };
44
48
  walk.nextTypeDefs(schema, pointer);
45
49
 
46
- if (schema.definitions == null) {
47
- return;
50
+ if (schema.definitions != null) {
51
+ walk.callback = (defschema, schemaPointer) => {
52
+ callback(defschema, schemaPointer);
53
+ if (defschema.definitions != null) {
54
+ eachDefinition(walk, defschema, schemaPointer);
55
+ }
56
+ };
57
+
58
+ eachDefinition(walk, schema, pointer);
48
59
  }
49
60
 
50
- walk.callback = (defschema, schemaPointer) => {
51
- callback(defschema, schemaPointer);
52
- if (defschema.definitions != null) {
53
- eachDefinition(walk, defschema, schemaPointer);
54
- }
55
- };
61
+ if (schema.$defs != null) {
62
+ walk.callback = (defschema, schemaPointer) => {
63
+ callback(defschema, schemaPointer);
64
+ if (defschema.definitions != null) {
65
+ eachDefinition(walk, defschema, schemaPointer);
66
+ }
67
+ };
56
68
 
57
- eachDefinition(walk, schema, pointer);
69
+ eachDefinition(walk, schema, pointer, "$defs");
70
+ }
58
71
  }
@@ -1,19 +1,26 @@
1
+ import { Draft } from "./draft";
2
+ import { isJSONError, JSONError, JSONSchema } from "./types";
3
+
1
4
  /**
2
5
  * Returns a list of possible child-schemas for the given property key. In case of a oneOf selection, multiple schemas
3
6
  * could be added at the given property (e.g. item-index), thus an array of options is returned. In all other cases
4
7
  * a list with a single item will be returned
5
8
  *
6
- * @param {Core} core - core to use
7
- * @param {String} property - parent schema of following property
8
- * @param {Object} [schema] - parent schema of following property
9
- * @return {Object}
9
+ * @param core - core to use
10
+ * @param property - parent schema of following property
11
+ * @param [schema] - parent schema of following property
12
+ * @return
10
13
  */
11
- export default function getChildSchemaSelection(core, property, schema = core.rootSchema) {
14
+ export default function getChildSchemaSelection(
15
+ core: Draft,
16
+ property: string | number,
17
+ schema: JSONSchema = core.rootSchema
18
+ ): JSONSchema[] | JSONError {
12
19
  const result = core.step(property, schema, {}, "#");
13
20
 
14
- if (result.type === "error") {
21
+ if (isJSONError(result)) {
15
22
  if (result.code === "one-of-error") {
16
- return result.data.oneOf.map(item => core.resolveRef(item));
23
+ return result.data.oneOf.map((item: JSONSchema) => core.resolveRef(item));
17
24
  }
18
25
  return result;
19
26
  }
package/lib/getSchema.ts CHANGED
@@ -1,11 +1,9 @@
1
1
  import gp from "gson-pointer";
2
- import { JSONSchema, JSONPointer } from "./types";
3
- import Core from "./cores/CoreInterface";
4
-
2
+ import { JSONSchema, JSONPointer, isJSONError } from "./types";
3
+ import { Draft as Core } from "./draft";
5
4
 
6
5
  const emptyObject = {};
7
6
 
8
-
9
7
  /**
10
8
  * Returns the json-schema of a data-json-pointer.
11
9
  *
@@ -19,22 +17,33 @@ const emptyObject = {};
19
17
  * @param [schema] - the json schema to iterate. Defaults to core.rootSchema
20
18
  * @return json schema object of the json-pointer or an error
21
19
  */
22
- export default function getSchema(core: Core, pointer: JSONPointer, data?: any, schema: JSONSchema = core.rootSchema): JSONSchema {
20
+ export default function getSchema(
21
+ core: Core,
22
+ pointer: JSONPointer,
23
+ data?: unknown,
24
+ schema: JSONSchema = core.rootSchema
25
+ ): JSONSchema {
23
26
  const frags = gp.split(pointer);
24
27
  return _get(core, schema, frags, pointer, data);
25
28
  }
26
29
 
27
-
28
- function _get(core: Core, schema: JSONSchema, frags: Array<string>, pointer: JSONPointer, data: any = emptyObject): JSONSchema {
30
+ function _get(
31
+ core: Core,
32
+ schema: JSONSchema,
33
+ frags: Array<string>,
34
+ pointer: JSONPointer,
35
+ data: unknown = emptyObject
36
+ ): JSONSchema {
29
37
  if (frags.length === 0) {
30
- return schema;
38
+ return core.resolveRef(schema);
31
39
  }
32
40
 
33
41
  const key = frags.shift(); // step key
34
42
  schema = core.step(key, schema, data, pointer); // step schema
35
- if (schema && schema.type === "error") {
43
+ if (isJSONError(schema)) {
36
44
  return schema;
37
45
  }
46
+ // @ts-ignore
38
47
  data = data[key]; // step data
39
48
  return _get(core, schema, frags, `${pointer}/${key}`, data);
40
49
  }