json-schema-library 11.0.5 → 11.2.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 (113) hide show
  1. package/.mocharc.js +1 -0
  2. package/CHANGELOG.md +17 -0
  3. package/README.md +120 -0
  4. package/bowtie/.editorconfig +21 -0
  5. package/bowtie/.prettierrc +6 -0
  6. package/bowtie/BOWTIE.md +54 -0
  7. package/bowtie/Dockerfile +6 -0
  8. package/bowtie/bowtie-api.ts +101 -0
  9. package/bowtie/bowtie.test.ts +76 -0
  10. package/bowtie/bowtie.ts +156 -0
  11. package/bowtie/package.json +11 -0
  12. package/bowtie/tsconfig.json +12 -0
  13. package/dist/index.cjs +1 -1
  14. package/dist/index.d.cts +39 -470
  15. package/dist/index.d.mts +39 -470
  16. package/dist/index.mjs +1 -1
  17. package/dist/jlib.js +2 -13
  18. package/dist/remotes/index.cjs +1 -0
  19. package/dist/remotes/index.d.cts +7 -0
  20. package/dist/remotes/index.d.mts +7 -0
  21. package/dist/remotes/index.mjs +1 -0
  22. package/dist/types-B2wwNWyo.d.cts +513 -0
  23. package/dist/types-BhTU1l2h.d.mts +513 -0
  24. package/index.ts +10 -4
  25. package/package.json +14 -8
  26. package/src/Keyword.ts +37 -12
  27. package/src/SchemaNode.ts +84 -16
  28. package/src/compileSchema.ts +56 -4
  29. package/src/draft04/keywords/$ref.ts +22 -14
  30. package/src/draft04/keywords/exclusiveMaximum.ts +14 -0
  31. package/src/draft04/keywords/exclusiveMinimum.ts +14 -0
  32. package/src/draft04/validateSchema.test.ts +20 -0
  33. package/src/draft06/keywords/$ref.ts +15 -5
  34. package/src/draft2019-09/keywords/$ref.test.ts +3 -1
  35. package/src/draft2019-09/keywords/$ref.ts +40 -16
  36. package/src/draft2019-09/keywords/additionalItems.ts +33 -10
  37. package/src/draft2019-09/keywords/items.ts +32 -10
  38. package/src/draft2019-09/keywords/unevaluatedItems.ts +19 -6
  39. package/src/draft2019-09/methods/getData.ts +1 -1
  40. package/src/draft2019-09/validateSchema.test.ts +28 -0
  41. package/src/errors/errors.ts +8 -1
  42. package/src/formats/formats.ts +35 -28
  43. package/src/formats/hyperjump.d.ts +172 -0
  44. package/src/keywords/$defs.ts +34 -8
  45. package/src/keywords/$ref.ts +59 -13
  46. package/src/keywords/additionalProperties.ts +19 -8
  47. package/src/keywords/allOf.ts +44 -18
  48. package/src/keywords/anyOf.ts +38 -19
  49. package/src/keywords/contains.ts +21 -9
  50. package/src/keywords/dependencies.ts +37 -17
  51. package/src/keywords/dependentRequired.ts +56 -38
  52. package/src/keywords/dependentSchemas.ts +37 -13
  53. package/src/keywords/deprecated.ts +32 -8
  54. package/src/keywords/enum.ts +30 -8
  55. package/src/keywords/exclusiveMaximum.ts +21 -2
  56. package/src/keywords/exclusiveMinimum.ts +22 -3
  57. package/src/keywords/format.ts +21 -2
  58. package/src/keywords/ifthenelse.ts +49 -5
  59. package/src/keywords/items.ts +27 -13
  60. package/src/keywords/maxItems.ts +22 -2
  61. package/src/keywords/maxLength.ts +30 -9
  62. package/src/keywords/maxProperties.ts +30 -9
  63. package/src/keywords/maximum.ts +28 -8
  64. package/src/keywords/minItems.ts +30 -9
  65. package/src/keywords/minLength.ts +30 -9
  66. package/src/keywords/minProperties.ts +26 -5
  67. package/src/keywords/minimum.ts +32 -13
  68. package/src/keywords/multipleOf.ts +33 -12
  69. package/src/keywords/not.ts +23 -10
  70. package/src/keywords/oneOf.ts +29 -9
  71. package/src/keywords/pattern.ts +35 -9
  72. package/src/keywords/properties.ts +34 -11
  73. package/src/keywords/propertyDependencies.test.ts +180 -0
  74. package/src/keywords/propertyDependencies.ts +173 -0
  75. package/src/keywords/propertyNames.ts +26 -14
  76. package/src/keywords/required.ts +31 -8
  77. package/src/keywords/type.ts +53 -16
  78. package/src/keywords/unevaluatedItems.ts +24 -8
  79. package/src/keywords/unevaluatedProperties.ts +24 -7
  80. package/src/keywords/uniqueItems.ts +23 -4
  81. package/src/mergeNode.ts +9 -4
  82. package/src/methods/getData.ts +1 -1
  83. package/src/settings.ts +2 -1
  84. package/src/types.ts +1 -1
  85. package/src/utils/isListOfStrings.ts +3 -0
  86. package/src/validate.test.ts +0 -2
  87. package/src/validateNode.ts +6 -3
  88. package/src/validateSchema.test.ts +312 -0
  89. package/tsconfig.json +11 -4
  90. package/tsconfig.test.json +9 -2
  91. package/tsdown.config.ts +1 -1
  92. package/Dockerfile +0 -6
  93. package/bowtie_jlib.js +0 -140
  94. package/remotes/draft04.json +0 -150
  95. package/remotes/draft06.json +0 -155
  96. package/remotes/draft07.json +0 -155
  97. package/remotes/draft2019-09.json +0 -42
  98. package/remotes/draft2019-09_meta_applicator.json +0 -53
  99. package/remotes/draft2019-09_meta_content.json +0 -14
  100. package/remotes/draft2019-09_meta_core.json +0 -54
  101. package/remotes/draft2019-09_meta_format.json +0 -11
  102. package/remotes/draft2019-09_meta_meta-data.json +0 -34
  103. package/remotes/draft2019-09_meta_validation.json +0 -95
  104. package/remotes/draft2020-12.json +0 -55
  105. package/remotes/draft2020-12_meta_applicator.json +0 -45
  106. package/remotes/draft2020-12_meta_content.json +0 -14
  107. package/remotes/draft2020-12_meta_core.json +0 -48
  108. package/remotes/draft2020-12_meta_format_annotation.json +0 -11
  109. package/remotes/draft2020-12_meta_format_assertion.json +0 -11
  110. package/remotes/draft2020-12_meta_meta_data.json +0 -34
  111. package/remotes/draft2020-12_meta_unevaluated.json +0 -12
  112. package/remotes/draft2020-12_meta_validation.json +0 -87
  113. package/remotes/index.ts +0 -48
@@ -4,28 +4,51 @@ import { SchemaNode } from "../../types";
4
4
  import { getValue } from "../../utils/getValue";
5
5
  import { validateNode } from "../../validateNode";
6
6
 
7
+ const KEYWORD = "additionalItems";
8
+
7
9
  export const additionalItemsKeyword: Keyword = {
8
- id: "additionalItems",
9
- keyword: "additionalItems",
10
+ id: KEYWORD,
11
+ keyword: KEYWORD,
10
12
  order: -10,
11
13
  parse: parseAdditionalItems,
12
14
  addResolve: (node: SchemaNode) => node.items != null,
13
15
  resolve: additionalItemsResolver,
14
- addValidate: ({ schema }) =>
15
- schema.additionalItems != null && schema.additionalItems !== true && Array.isArray(schema.items),
16
+ addValidate: ({ schema }) => schema[KEYWORD] != null && schema[KEYWORD] !== true && Array.isArray(schema.items),
16
17
  validate: validateAdditionalItems
17
18
  };
18
19
 
19
20
  // must come as last resolver
20
21
  export function parseAdditionalItems(node: SchemaNode) {
21
22
  const { schema, evaluationPath, schemaLocation } = node;
22
- if ((isObject(schema.additionalItems) || schema.additionalItems === true) && Array.isArray(schema.items)) {
23
- node.items = node.compileSchema(
24
- schema.additionalItems,
25
- `${evaluationPath}/additionalItems`,
26
- `${schemaLocation}/additionalItems`
27
- );
23
+ if (schema.additionalItems == null || schema.additionalItems === false) {
24
+ return;
25
+ }
26
+
27
+ const additionalItems = schema[KEYWORD];
28
+
29
+ if (!(isObject(additionalItems) || additionalItems === true)) {
30
+ return node.createError("schema-error", {
31
+ pointer: evaluationPath,
32
+ schema,
33
+ value: undefined,
34
+ message: `Keyword '${KEYWORD}' must be an object or a boolean - received '${typeof additionalItems}'`
35
+ });
36
+ }
37
+
38
+ if (!Array.isArray(schema.items)) {
39
+ return node.createAnnotation("schema-error", {
40
+ pointer: evaluationPath,
41
+ schema,
42
+ value: undefined,
43
+ message: `Keyword '${KEYWORD}' is only evaluated with a given items-array`
44
+ });
28
45
  }
46
+
47
+ node.items = node.compileSchema(
48
+ schema.additionalItems,
49
+ `${evaluationPath}/additionalItems`,
50
+ `${schemaLocation}/additionalItems`
51
+ );
29
52
  }
30
53
 
31
54
  function additionalItemsResolver({ node, key, data }: JsonSchemaResolverParams) {
@@ -3,11 +3,13 @@ import { SchemaNode } from "../../types";
3
3
  import { isObject } from "../../utils/isObject";
4
4
  import { validateNode } from "../../validateNode";
5
5
 
6
+ const KEYWORD = "items";
7
+
6
8
  export const itemsKeyword: Keyword = {
7
- id: "items",
8
- keyword: "items",
9
+ id: KEYWORD,
10
+ keyword: KEYWORD,
9
11
  parse: parseItems,
10
- addResolve: (node) => (node.prefixItems || node.items) != null,
12
+ addResolve: (node) => (node.prefixItems || node[KEYWORD]) != null,
11
13
  resolve: itemsResolver,
12
14
  addValidate: ({ schema }) => schema.items != null,
13
15
  validate: validateItems
@@ -24,18 +26,38 @@ function itemsResolver({ node, key }: JsonSchemaResolverParams) {
24
26
 
25
27
  export function parseItems(node: SchemaNode) {
26
28
  const { schema, evaluationPath } = node;
27
- if (isObject(schema.items)) {
29
+ const items = schema[KEYWORD];
30
+ if (items == null || typeof items === "boolean") {
31
+ return;
32
+ }
33
+
34
+ if (isObject(items)) {
28
35
  const propertyNode = node.compileSchema(
29
- schema.items,
30
- `${evaluationPath}/items`,
31
- `${node.schemaLocation}/items`
36
+ items,
37
+ `${evaluationPath}/${KEYWORD}`,
38
+ `${node.schemaLocation}/${KEYWORD}`
32
39
  );
33
40
  node.items = propertyNode;
34
- } else if (Array.isArray(schema.items)) {
35
- node.prefixItems = schema.items.map((itemSchema, index) =>
36
- node.compileSchema(itemSchema, `${evaluationPath}/items/${index}`, `${node.schemaLocation}/items/${index}`)
41
+ return;
42
+ }
43
+
44
+ if (Array.isArray(items)) {
45
+ node.prefixItems = items.map((itemSchema, index) =>
46
+ node.compileSchema(
47
+ itemSchema,
48
+ `${evaluationPath}/${KEYWORD}/${index}`,
49
+ `${node.schemaLocation}/${KEYWORD}/${index}`
50
+ )
37
51
  );
52
+ return;
38
53
  }
54
+
55
+ return node.createError("schema-error", {
56
+ pointer: evaluationPath,
57
+ schema,
58
+ value: undefined,
59
+ message: `Keyword '${KEYWORD}' must be an object or array - received '${typeof items}'`
60
+ });
39
61
  }
40
62
 
41
63
  function validateItems({ node, data, pointer = "#", path }: JsonSchemaValidatorParams) {
@@ -3,27 +3,40 @@ import { SchemaNode } from "../../types";
3
3
  import { Keyword, JsonSchemaValidatorParams, ValidationReturnType } from "../../Keyword";
4
4
  import { validateNode } from "../../validateNode";
5
5
 
6
+ const KEYWORD = "unevaluatedItems";
7
+
6
8
  /**
7
9
  * @draft >= 2019-09
8
10
  * Similar to additionalItems, but can "see" into subschemas and across references
9
11
  * https://json-schema.org/draft/2019-09/json-schema-core#rfc.section.9.3.1.3
10
12
  */
11
13
  export const unevaluatedItemsKeyword: Keyword = {
12
- id: "unevaluatedItems",
13
- keyword: "unevaluatedItems",
14
+ id: KEYWORD,
15
+ keyword: KEYWORD,
14
16
  parse: parseUnevaluatedItems,
15
- addValidate: ({ schema }) => schema.unevaluatedItems != null,
17
+ addValidate: ({ schema }) => schema[KEYWORD] != null,
16
18
  validate: validateUnevaluatedItems
17
19
  };
18
20
 
19
21
  export function parseUnevaluatedItems(node: SchemaNode) {
20
- if (!isObject(node.schema.unevaluatedItems)) {
22
+ const { unevaluatedItems } = node.schema;
23
+ if (unevaluatedItems == null || typeof unevaluatedItems === "boolean") {
21
24
  return;
22
25
  }
26
+
27
+ if (!isObject(unevaluatedItems)) {
28
+ return node.createError("schema-error", {
29
+ pointer: node.evaluationPath,
30
+ schema: node.schema,
31
+ value: undefined,
32
+ message: `Keyword '${KEYWORD}' must be an object - received '${typeof unevaluatedItems}'`
33
+ });
34
+ }
35
+
23
36
  node.unevaluatedItems = node.compileSchema(
24
37
  node.schema.unevaluatedItems,
25
- `${node.evaluationPath}/unevaluatedItems`,
26
- `${node.schemaLocation}/unevaluatedItems`
38
+ `${node.evaluationPath}/${KEYWORD}`,
39
+ `${node.schemaLocation}/${KEYWORD}`
27
40
  );
28
41
  }
29
42
 
@@ -160,7 +160,7 @@ export function getData(node: SchemaNode, data?: unknown, opts?: TemplateOptions
160
160
  return defaultData;
161
161
  }
162
162
 
163
- if (resolvedNode && resolvedNode !== currentNode) {
163
+ if (isSchemaNode(resolvedNode)) {
164
164
  defaultData = resolvedNode.getData(defaultData, opts) ?? defaultData;
165
165
  currentNode = resolvedNode;
166
166
  }
@@ -0,0 +1,28 @@
1
+ import { compileSchema as _compileSchema, type CompileOptions } from "../compileSchema";
2
+ import { strict as assert } from "assert";
3
+ import { draft2019 } from "../draft2019";
4
+ import { JsonSchema } from "../types";
5
+
6
+ const drafts = [draft2019];
7
+ function compileSchema(schema: JsonSchema, options: CompileOptions = {}) {
8
+ return _compileSchema(schema, { ...options, drafts });
9
+ }
10
+
11
+ describe("validateSchema (2019-09)", () => {
12
+ it("should error if `additionalItems` is of an invalid type", () => {
13
+ const { schemaErrors } = compileSchema({ additionalItems: [] });
14
+ assert.equal(schemaErrors?.length, 1);
15
+ });
16
+ it("should create annotation for `additionalItems` where items-array is missing", () => {
17
+ const { schemaAnnotations } = compileSchema({ additionalItems: true });
18
+ assert.equal(schemaAnnotations?.length, 1);
19
+ });
20
+ it("should error if `items` is of an invalid type", () => {
21
+ const { schemaErrors } = compileSchema({ items: 999 });
22
+ assert.equal(schemaErrors?.length, 1);
23
+ });
24
+ it("should error if `unevaluatedItems` is not an object or boolean", () => {
25
+ const { schemaErrors } = compileSchema({ unevaluatedItems: [] });
26
+ assert.equal(schemaErrors?.length, 1);
27
+ });
28
+ });
@@ -19,12 +19,15 @@ export const errors = {
19
19
  "format-duration-error": "Value `{{value}}` at `{{pointer}}` is not a valid duration",
20
20
  "format-email-error": "Value `{{value}}` at `{{pointer}}` is not a valid email",
21
21
  "format-hostname-error": "Value `{{value}}` at `{{pointer}}` is not a valid hostname",
22
+ "format-idn-hostname-error": "Value `{{value}}` at `{{pointer}}` is not a valid idn hostname",
22
23
  "format-ipv4-error": "Value `{{value}}` at `{{pointer}}` is not a valid IPv4 address",
23
24
  "format-ipv4-leading-zero-error":
24
25
  "IPv4 addresses starting with zero are invalid, since they are interpreted as octals",
25
26
  "format-ipv6-error": "Value `{{value}}` at `{{pointer}}` is not a valid IPv6 address",
26
27
  "format-ipv6-leading-zero-error":
27
28
  "IPv6 addresses starting with zero are invalid, since they are interpreted as octals",
29
+ "format-iri-error": "Value `{{value}}` at `{{pointer}}` is not a valid iri",
30
+ "format-iri-reference-error": "Value `{{value}}` at `{{pointer}}` is not a valid iri-reference",
28
31
  "format-json-pointer-error": "Value `{{value}}` at `{{pointer}}` is not a valid json-pointer",
29
32
  "format-regex-error": "Value `{{value}}` at `{{pointer}}` is not a valid regular expression",
30
33
  "format-time-error": "Value `{{value}}` at `{{pointer}}` is not a valid time",
@@ -62,6 +65,7 @@ export const errors = {
62
65
  "pattern-error": "Value in `{{pointer}}` should match `{{description}}`, but received `{{received}}`",
63
66
  "pattern-properties-error":
64
67
  "Property `{{key}}` does not match any patterns in `{{pointer}}`. Valid patterns are: {{patterns}}",
68
+ "ref-error": "Could not resolve $ref '{{ref}}' from '{{pointer}}'",
65
69
  "required-property-error": "The required property `{{key}}` is missing at `{{pointer}}`",
66
70
  /** return schema-warning with createSchemaWarning:true when a valid, but undefined property was found */
67
71
  "schema-warning": "Failed retrieving a schema from '{{pointer}}' to key '{{key}}'",
@@ -74,5 +78,8 @@ export const errors = {
74
78
  "unknown-property-error": "Could not find a valid schema for property `{{pointer}}` within object",
75
79
  "value-not-empty-error": "A value for `{{property}}` is required at `{{pointer}}`",
76
80
  // annotations
77
- "deprecated-warning": "Value at `{{pointer}}` is deprecated"
81
+ "deprecated-warning": "Value at `{{pointer}}` is deprecated",
82
+ // schema validation
83
+ "schema-error": "Invalid schema found at {{pointer}}: {{message}}",
84
+ "unknown-keyword-warning": "Keyword '{{value}}' is not a valid keyword to draft '{{draft}}'"
78
85
  };
@@ -1,16 +1,14 @@
1
1
  /* eslint-disable no-control-regex */
2
- import { getTypeOf } from "../utils/getTypeOf";
2
+ import { isAsciiIdn, isUri, isIdnEmail, isIri, isIriReference, isIdn } from "@hyperjump/json-schema-formats";
3
3
  import validUrl from "valid-url";
4
+ import { getTypeOf } from "../utils/getTypeOf";
4
5
  import { JsonSchemaValidatorParams, ValidationReturnType } from "../Keyword";
5
- import { parse as parseIdnEmail } from "smtp-address-parser";
6
6
  import settings from "../settings";
7
7
 
8
8
  const { REGEX_FLAGS } = settings;
9
9
  const isValidIPV4 = /^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$/;
10
10
  const isValidIPV6 =
11
11
  /^((([0-9a-f]{1,4}:){7}([0-9a-f]{1,4}|:))|(([0-9a-f]{1,4}:){6}(:[0-9a-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){5}(((:[0-9a-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){4}(((:[0-9a-f]{1,4}){1,3})|((:[0-9a-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){3}(((:[0-9a-f]{1,4}){1,4})|((:[0-9a-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){2}(((:[0-9a-f]{1,4}){1,5})|((:[0-9a-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){1}(((:[0-9a-f]{1,4}){1,6})|((:[0-9a-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9a-f]{1,4}){1,7})|((:[0-9a-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))$/i;
12
- const isValidHostname =
13
- /^(?=.{1,255}$)[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?(?:\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?)*\.?$/;
14
12
  const matchDate = /^(\d\d\d\d)-(\d\d)-(\d\d)$/;
15
13
  const matchTime =
16
14
  /^(?<time>(?:([0-1]\d|2[0-3]):[0-5]\d:(?<second>[0-5]\d|60)))(?:\.\d+)?(?<offset>(?:z|[+-]([0-1]\d|2[0-3])(?::?[0-5]\d)?))$/i;
@@ -129,33 +127,30 @@ export const formats: Record<string, (options: JsonSchemaValidatorParams) => Val
129
127
  return undefined;
130
128
  },
131
129
 
132
- /**
133
- * @draft 7
134
- * [RFC6531] https://json-schema.org/draft-07/json-schema-validation.html#RFC6531
135
- */
136
- "idn-email": ({ node, pointer, data }) => {
130
+ hostname: ({ node, pointer, data }) => {
137
131
  const { schema } = node;
138
- if (typeof data !== "string" || data === "") {
132
+ if (typeof data !== "string" || isAsciiIdn(data)) {
139
133
  return undefined;
140
134
  }
141
- try {
142
- parseIdnEmail(data);
143
- return undefined;
144
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
145
- } catch (e) {
146
- return node.createError("format-email-error", { value: data, pointer, schema });
147
- }
135
+ return node.createError("format-hostname-error", { value: data, pointer, schema });
148
136
  },
149
137
 
150
- hostname: ({ node, pointer, data }) => {
151
- const { schema } = node;
152
- if (typeof data !== "string") {
138
+ "idn-hostname": ({ node, pointer, data }) => {
139
+ if (typeof data !== "string" || isIdn(data)) {
153
140
  return undefined;
154
141
  }
155
- if (isValidHostname.test(data)) {
142
+ return node.createError("format-idn-hostname-error", { value: data, pointer, schema: node.schema });
143
+ },
144
+
145
+ /**
146
+ * @draft 7
147
+ * [RFC6531] https://json-schema.org/draft-07/json-schema-validation.html#RFC6531
148
+ */
149
+ "idn-email": ({ node, pointer, data }) => {
150
+ if (typeof data !== "string" || data === "" || isIdnEmail(data)) {
156
151
  return undefined;
157
152
  }
158
- return node.createError("format-hostname-error", { value: data, pointer, schema });
153
+ return node.createError("format-email-error", { value: data, pointer, schema: node.schema });
159
154
  },
160
155
 
161
156
  ipv4: ({ node, pointer, data }) => {
@@ -188,6 +183,22 @@ export const formats: Record<string, (options: JsonSchemaValidatorParams) => Val
188
183
  return node.createError("format-ipv6-error", { value: data, pointer, schema });
189
184
  },
190
185
 
186
+ iri: ({ node, pointer, data }) => {
187
+ const { schema } = node;
188
+ if (typeof data !== "string" || data === "" || isIri(data)) {
189
+ return undefined;
190
+ }
191
+ return node.createError("format-iri-error", { value: data, pointer, schema });
192
+ },
193
+
194
+ "iri-reference": ({ node, pointer, data }) => {
195
+ const { schema } = node;
196
+ if (typeof data !== "string" || data === "" || isIriReference(data)) {
197
+ return undefined;
198
+ }
199
+ return node.createError("format-iri-reference-error", { value: data, pointer, schema });
200
+ },
201
+
191
202
  "json-pointer": ({ node, pointer, data }) => {
192
203
  const { schema } = node;
193
204
  if (typeof data !== "string" || data === "") {
@@ -281,14 +292,10 @@ export const formats: Record<string, (options: JsonSchemaValidatorParams) => Val
281
292
  },
282
293
 
283
294
  uri: ({ node, pointer, data }) => {
284
- const { schema } = node;
285
- if (typeof data !== "string" || data === "") {
286
- return undefined;
287
- }
288
- if (validUrl.isUri(data)) {
295
+ if (typeof data !== "string" || data === "" || isUri(data)) {
289
296
  return undefined;
290
297
  }
291
- return node.createError("format-uri-error", { value: data, pointer, schema });
298
+ return node.createError("format-uri-error", { value: data, pointer, schema: node.schema });
292
299
  },
293
300
 
294
301
  "uri-reference": ({ node, pointer, data }) => {
@@ -0,0 +1,172 @@
1
+ declare module "@hyperjump/json-schema-formats" {
2
+ /**
3
+ * The 'date' format. Validates that a string represents a date according to
4
+ * [RFC 3339, section 5.6](https://www.rfc-editor.org/rfc/rfc3339.html#section-5.6).
5
+ *
6
+ * @see [JSON Schema Core, section 7.3.1](https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-7.3.1)
7
+ */
8
+ export const isDate: (date: string) => boolean;
9
+
10
+ /**
11
+ * The 'time' format. Validates that a string represents a time according to
12
+ * [RFC 3339, section 5.6](https://www.rfc-editor.org/rfc/rfc3339.html#section-5.6).
13
+ *
14
+ * **NOTE**: Leap seconds are only allowed on specific dates. Since there is no date
15
+ * in this context, leap seconds are never allowed.
16
+ *
17
+ * @see [JSON Schema Core, section 7.3.1](https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-7.3.1)
18
+ */
19
+ export const isTime: (time: string) => boolean;
20
+
21
+ /**
22
+ * The 'date-time' format. Validates that a string represents a date-time
23
+ * according to [RFC 3339, section 5.6](https://www.rfc-editor.org/rfc/rfc3339.html#section-5.6).
24
+ *
25
+ * @see [JSON Schema Core, section 7.3.1](https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-7.3.1)
26
+ */
27
+ export const isDateTime: (dateTime: string) => boolean;
28
+
29
+ /**
30
+ * The 'duration' format. Validates that a string represents a duration
31
+ * according to [RFC 3339, Appendix A](https://www.rfc-editor.org/rfc/rfc3339.html#appendix-A).
32
+ *
33
+ * @see [JSON Schema Core, section 7.3.1](https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-7.3.1)
34
+ */
35
+ export const isDuration: (duration: string) => boolean;
36
+
37
+ /**
38
+ * The 'email' format. Validates that a string represents an email as defined by
39
+ * the "Mailbox" ABNF rule in [RFC 5321, section 4.1.2](https://www.rfc-editor.org/rfc/rfc5321.html#section-4.1.2).
40
+ *
41
+ * @see [JSON Schema Core, section 7.3.2](https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-7.3.2)
42
+ */
43
+ export const isEmail: (email: string) => boolean;
44
+
45
+ /**
46
+ * The 'idn-email' format. Validates that a string represents an email as
47
+ * defined by the "Mailbox" ABNF rule in [RFC 6531, section 3.3](https://www.rfc-editor.org/rfc/rfc6531.html#section-3.3).
48
+ *
49
+ * @see [JSON Schema Core, section 7.3.2](https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-7.3.2)
50
+ */
51
+ export const isIdnEmail: (email: string) => boolean;
52
+
53
+ /**
54
+ * The 'hostname' format in draft-04 - draft-06. Validates that a string
55
+ * represents a hostname as defined by [RFC 1123, section 2.1](https://www.rfc-editor.org/rfc/rfc1123.html#section-2.1).
56
+ *
57
+ * **NOTE**: The 'hostname' format changed in draft-07. Use {@link isAsciiIdn} for
58
+ * draft-07 and later.
59
+ *
60
+ * @see [JSON Schema Core, section 7.3.3](https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-7.3.3)
61
+ */
62
+ export const isHostname: (hostname: string) => boolean;
63
+
64
+ /**
65
+ * The 'hostname' format since draft-07. Validates that a string represents an
66
+ * IDNA2008 internationalized domain name consiting of only A-labels and NR-LDH
67
+ * labels as defined by [RFC 5890, section 2.3.2.1](https://www.rfc-editor.org/rfc/rfc5890.html#section-2.3.2.3).
68
+ *
69
+ * **NOTE**: The 'hostname' format changed in draft-07. Use {@link isHostname}
70
+ * for draft-06 and earlier.
71
+ *
72
+ * @see [JSON Schema Core, section 7.3.3](https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-7.3.3)
73
+ */
74
+ export const isAsciiIdn: (hostname: string) => boolean;
75
+
76
+ /**
77
+ * The 'idn-hostname' format. Validates that a string represents an IDNA2008
78
+ * internationalized domain name as defined by [RFC 5890, section 2.3.2.1](https://www.rfc-editor.org/rfc/rfc5890.html#section-2.3.2.1).
79
+ *
80
+ * @see [JSON Schema Core, section 7.3.3](https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-7.3.3)
81
+ */
82
+ export const isIdn: (hostname: string) => boolean;
83
+
84
+ /**
85
+ * The 'ipv4' format. Validates that a string represents an IPv4 address
86
+ * according to the "dotted-quad" ABNF syntax as defined in
87
+ * [RFC 2673, section 3.2](https://www.rfc-editor.org/rfc/rfc2673.html#section-3.2).
88
+ *
89
+ * @see [JSON Schema Core, section 7.3.4](https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-7.3.4)
90
+ */
91
+ export const isIPv4: (ip: string) => boolean;
92
+
93
+ /**
94
+ * The 'ipv6' format. Validates that a string represents an IPv6 address as
95
+ * defined in [RFC 4291, section 2.2](https://www.rfc-editor.org/rfc/rfc4291.html#section-2.2).
96
+ *
97
+ * @see [JSON Schema Core, section 7.3.4](https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-7.3.4)
98
+ */
99
+ export const isIPv6: (ip: string) => boolean;
100
+
101
+ /**
102
+ * The 'uri' format. Validates that a string represents a URI as defined by [RFC
103
+ * 3986](https://www.rfc-editor.org/rfc/rfc3986.html).
104
+ *
105
+ * @see [JSON Schema Core, section 7.3.5](https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-7.3.5)
106
+ */
107
+ export const isUri: (uri: string) => boolean;
108
+
109
+ /**
110
+ * The 'uri-reference' format. Validates that a string represents a URI
111
+ * Reference as defined by [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986.html).
112
+ *
113
+ * @see [JSON Schema Core, section 7.3.5](https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-7.3.5)
114
+ */
115
+ export const isUriReference: (uri: string) => boolean;
116
+
117
+ /**
118
+ * The 'iri' format. Validates that a string represents an IRI as defined by
119
+ * [RFC 3987](https://www.rfc-editor.org/rfc/rfc3987.html).
120
+ *
121
+ * @see [JSON Schema Core, section 7.3.5](https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-7.3.5)
122
+ */
123
+ export const isIri: (iri: string) => boolean;
124
+
125
+ /**
126
+ * The 'iri-reference' format. Validates that a string represents an IRI
127
+ * Reference as defined by [RFC 3987](https://www.rfc-editor.org/rfc/rfc3987.html).
128
+ *
129
+ * @see [JSON Schema Core, section 7.3.5](https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-7.3.5)
130
+ */
131
+ export const isIriReference: (iri: string) => boolean;
132
+
133
+ /**
134
+ * The 'uuid' format. Validates that a string represents a UUID address as
135
+ * defined by [RFC 4122](https://www.rfc-editor.org/rfc/rfc4122.html).
136
+ *
137
+ * @see [JSON Schema Core, section 7.3.5](https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-7.3.5)
138
+ */
139
+ export const isUuid: (uuid: string) => boolean;
140
+
141
+ /**
142
+ * The 'uri-template' format. Validates that a string represents a URI Template
143
+ * as defined by [RFC 6570](https://www.rfc-editor.org/rfc/rfc6570.html).
144
+ *
145
+ * @see [JSON Schema Core, section 7.3.6](https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-7.3.6)
146
+ */
147
+ export const isUriTemplate: (uriTemplate: string) => boolean;
148
+
149
+ /**
150
+ * The 'json-pointer' format. Validates that a string represents a JSON Pointer
151
+ * as defined by [RFC 6901](https://www.rfc-editor.org/rfc/rfc6901.html).
152
+ *
153
+ * @see [JSON Schema Core, section 7.3.7](https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-7.3.7)
154
+ */
155
+ export const isJsonPointer: (pointer: string) => boolean;
156
+
157
+ /**
158
+ * The 'relative-json-pointer' format. Validates that a string represents an IRI
159
+ * Reference as defined by [draft-bhutton-relative-json-pointer-00](https://datatracker.ietf.org/doc/html/draft-bhutton-relative-json-pointer-00).
160
+ *
161
+ * @see [JSON Schema Core, section 7.3.5](https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-7.3.5)
162
+ */
163
+ export const isRelativeJsonPointer: (pointer: string) => boolean;
164
+
165
+ /**
166
+ * The 'regex' format. Validates that a string represents a regular expression
167
+ * as defined by [ECMA-262](https://262.ecma-international.org/5.1/).
168
+ *
169
+ * @see [JSON Schema Core, section 7.3.8](https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-7.3.8)
170
+ */
171
+ export const isRegex: (regex: string) => boolean;
172
+ }
@@ -1,5 +1,6 @@
1
- import { Keyword } from "../Keyword";
1
+ import { Keyword, ValidationAnnotation } from "../Keyword";
2
2
  import { SchemaNode } from "../types";
3
+ import { isObject } from "../utils/isObject";
3
4
 
4
5
  export const $defsKeyword: Keyword = {
5
6
  id: "$defs",
@@ -8,17 +9,40 @@ export const $defsKeyword: Keyword = {
8
9
  };
9
10
 
10
11
  export function parseDefs(node: SchemaNode) {
12
+ const errors: ValidationAnnotation[] = [];
13
+
11
14
  if (node.schema.$defs) {
12
- node.$defs = node.$defs ?? {};
13
- Object.keys(node.schema.$defs).forEach((property) => {
14
- node.$defs![property] = node.compileSchema(
15
- node.schema.$defs[property],
16
- `${node.evaluationPath}/$defs/${urlEncodeJsonPointerProperty(property)}`,
17
- `${node.schemaLocation}/$defs/${property}`
15
+ if (!isObject(node.schema.$defs)) {
16
+ errors.push(
17
+ node.createError("schema-error", {
18
+ pointer: node.schemaLocation,
19
+ schema: node.schema,
20
+ value: node.schema.$defs,
21
+ message: `$defs must be an object - received: ${typeof node.schema.$defs}`
22
+ })
18
23
  );
19
- });
24
+ } else {
25
+ node.$defs = node.$defs ?? {};
26
+ Object.keys(node.schema.$defs).forEach((property) => {
27
+ node.$defs![property] = node.compileSchema(
28
+ node.schema.$defs[property],
29
+ `${node.evaluationPath}/$defs/${urlEncodeJsonPointerProperty(property)}`,
30
+ `${node.schemaLocation}/$defs/${property}`
31
+ );
32
+ });
33
+ }
20
34
  }
21
35
  if (node.schema.definitions) {
36
+ if (!isObject(node.schema.definitions)) {
37
+ errors.push(
38
+ node.createError("schema-error", {
39
+ pointer: node.schemaLocation,
40
+ schema: node.schema,
41
+ value: node.schema.$defs,
42
+ message: `definitions must be an object - received: ${typeof node.schema.definitions}`
43
+ })
44
+ );
45
+ }
22
46
  node.$defs = node.$defs ?? {};
23
47
  Object.keys(node.schema.definitions).forEach((property) => {
24
48
  node.$defs![property] = node.compileSchema(
@@ -28,6 +52,8 @@ export function parseDefs(node: SchemaNode) {
28
52
  );
29
53
  });
30
54
  }
55
+
56
+ return errors;
31
57
  }
32
58
 
33
59
  function urlEncodeJsonPointerProperty(property: string) {