json-schema-library 10.5.4 → 11.0.1

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 (39) hide show
  1. package/README.md +128 -14
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.d.cts +42 -19
  4. package/dist/index.d.mts +42 -19
  5. package/dist/index.mjs +1 -1
  6. package/dist/jlib.js +2 -2
  7. package/index.ts +8 -4
  8. package/package.json +6 -6
  9. package/src/Keyword.ts +8 -3
  10. package/src/SchemaNode.ts +46 -10
  11. package/src/compileSchema.validate.test.ts +89 -54
  12. package/src/draft04.ts +10 -8
  13. package/src/draft06.ts +10 -8
  14. package/src/draft07.ts +10 -8
  15. package/src/draft2019-09/keywords/additionalItems.ts +2 -2
  16. package/src/draft2019-09/keywords/items.ts +2 -2
  17. package/src/draft2019-09/keywords/unevaluatedItems.ts +12 -6
  18. package/src/draft2019.ts +10 -8
  19. package/src/draft2020.ts +8 -6
  20. package/src/errors/errors.ts +3 -1
  21. package/src/formats/formats.ts +2 -6
  22. package/src/keywords/additionalProperties.ts +2 -2
  23. package/src/keywords/allOf.ts +2 -2
  24. package/src/keywords/dependencies.ts +5 -6
  25. package/src/keywords/dependentRequired.ts +2 -2
  26. package/src/keywords/dependentSchemas.ts +4 -3
  27. package/src/keywords/deprecated.ts +18 -0
  28. package/src/keywords/items.ts +2 -2
  29. package/src/keywords/oneOf.test.ts +150 -15
  30. package/src/keywords/oneOf.ts +64 -4
  31. package/src/keywords/patternProperties.ts +2 -2
  32. package/src/keywords/prefixItems.ts +2 -11
  33. package/src/keywords/properties.ts +2 -2
  34. package/src/keywords/unevaluatedItems.ts +2 -2
  35. package/src/keywords/unevaluatedProperties.ts +2 -2
  36. package/src/methods/getData.test.ts +1779 -1781
  37. package/src/types.ts +32 -18
  38. package/src/utils/sanitizeErrors.ts +9 -8
  39. package/src/validateNode.ts +2 -2
package/src/types.ts CHANGED
@@ -1,23 +1,18 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
1
2
  import { Draft } from "./Draft";
2
3
  import { errors } from "./errors/errors";
3
4
  import { SchemaNode, isSchemaNode, GetNodeOptions } from "./SchemaNode";
5
+ import { isObject } from "./utils/isObject";
4
6
 
5
7
  export type BooleanSchema = boolean;
6
8
  // eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style
7
9
  export interface JsonSchema {
8
- [keyword: string]: any; // eslint-disable-line @typescript-eslint/no-explicit-any
10
+ [keyword: string]: any;
9
11
  }
10
- export type JsonPointer = string;
11
-
12
- export type DefaultErrors = keyof typeof errors;
13
- export type ErrorConfig = Record<DefaultErrors | string, string | ((error: ErrorData) => void)>;
14
- export type OptionalNodeOrError = { node?: SchemaNode; error: undefined } | { node: undefined; error?: JsonError };
15
- export type NodeOrError = { node: SchemaNode; error: undefined } | { node: undefined; error: JsonError };
16
12
 
17
- export type { SchemaNode, GetNodeOptions, Draft };
18
- export { isSchemaNode };
13
+ export type JsonPointer = string;
19
14
 
20
- export type ErrorData<T extends Record<string, unknown> = Record<string, unknown>> = T & {
15
+ export type AnnotationData<D extends Record<string, unknown> = Record<string, unknown>> = D & {
21
16
  /* json-pointer to location of error */
22
17
  pointer: string;
23
18
  /* json-schema of error location */
@@ -26,23 +21,42 @@ export type ErrorData<T extends Record<string, unknown> = Record<string, unknown
26
21
  value: unknown;
27
22
  };
28
23
 
29
- export type JsonError<T extends ErrorData = ErrorData> = {
30
- type: "error";
31
- code: ErrorConfig | string;
24
+ export type Annotation<T = string, D extends AnnotationData = AnnotationData, S = string> = {
25
+ type: T;
26
+ code: S;
32
27
  message: string;
33
- data: T;
28
+ data: D;
34
29
  [p: string]: unknown;
35
30
  };
36
31
 
32
+ export type DefaultErrors = keyof typeof errors;
33
+ export type ErrorConfig = Record<DefaultErrors | string, string | ((error: AnnotationData) => void)>;
34
+ export type OptionalNodeOrError = { node?: SchemaNode; error: undefined } | { node: undefined; error?: JsonError };
35
+ export type NodeOrError = { node: SchemaNode; error: undefined } | { node: undefined; error: JsonError };
36
+ export type JsonError<D extends AnnotationData = AnnotationData> = Annotation<"error", D, ErrorConfig | string>;
37
+ export type JsonAnnotation<D extends AnnotationData = AnnotationData> = Annotation<"annotation", D>;
38
+
39
+ export type { SchemaNode, GetNodeOptions, Draft };
40
+ export { isSchemaNode };
41
+
42
+ export function isAnnotation(value: any): value is Annotation {
43
+ return isObject(value) && (value?.type && value?.code && value?.data) != null;
44
+ }
45
+
46
+ /**
47
+ * ts type guard for json error
48
+ * @returns true if passed type is a JsonError
49
+ */
50
+ export function isJsonAnnotation(error: unknown): error is JsonAnnotation {
51
+ return isObject(error) && error.type === "annotation";
52
+ }
53
+
37
54
  /**
38
55
  * ts type guard for json error
39
56
  * @returns true if passed type is a JsonError
40
57
  */
41
58
  export function isJsonError(error: unknown): error is JsonError {
42
- if (error && typeof error == "object") {
43
- return (error as Record<string, unknown>).type === "error";
44
- }
45
- return false;
59
+ return isObject(error) && error.type === "error";
46
60
  }
47
61
 
48
62
  export function isNumber(value: unknown): value is number {
@@ -1,12 +1,13 @@
1
- import { isJsonError, JsonError } from "../types";
2
- import { ValidationResult, JsonSchemaValidator } from "../Keyword";
3
-
4
- type MaybeNestedErrors = ReturnType<JsonSchemaValidator>;
1
+ import { isAnnotation } from "../types";
2
+ import { ValidationAnnotation, ValidationReturnType } from "../Keyword";
5
3
 
4
+ /**
5
+ * Flattens nested validation array results and filters items to only include errors, annotations and promises
6
+ */
6
7
  export default function sanitizeErrors(
7
- list: MaybeNestedErrors | MaybeNestedErrors[],
8
- result: (JsonError | Promise<JsonError | undefined> | ValidationResult)[] = []
9
- ): ValidationResult[] {
8
+ list: ValidationReturnType | ValidationReturnType[] | ValidationAnnotation[],
9
+ result: ValidationAnnotation[] = []
10
+ ) {
10
11
  if (!Array.isArray(list)) {
11
12
  if (list !== undefined) {
12
13
  return [list];
@@ -16,7 +17,7 @@ export default function sanitizeErrors(
16
17
  for (const item of list) {
17
18
  if (Array.isArray(item)) {
18
19
  sanitizeErrors(item, result);
19
- } else if (isJsonError(item) || item instanceof Promise) {
20
+ } else if (isAnnotation(item) || item instanceof Promise) {
20
21
  result.push(item);
21
22
  }
22
23
  }
@@ -1,5 +1,5 @@
1
1
  import { BooleanSchema, JsonSchema, SchemaNode } from "./types";
2
- import { ValidationPath, ValidationResult } from "./Keyword";
2
+ import { ValidationPath, ValidationReturnType } from "./Keyword";
3
3
  import sanitizeErrors from "./utils/sanitizeErrors";
4
4
 
5
5
  export function validateNode(node: SchemaNode, data: unknown, pointer: string, path: ValidationPath) {
@@ -17,7 +17,7 @@ export function validateNode(node: SchemaNode, data: unknown, pointer: string, p
17
17
  })
18
18
  ];
19
19
  }
20
- const errors: (undefined | ValidationResult | ValidationResult | Promise<undefined>)[] = [];
20
+ const errors: ValidationReturnType = [];
21
21
  for (const validate of node.validators) {
22
22
  const result = validate({ node, data, pointer, path });
23
23
  if (Array.isArray(result)) {