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/dist/index.d.cts CHANGED
@@ -25,7 +25,10 @@ interface JsonSchemaResolver {
25
25
  order?: number;
26
26
  (options: JsonSchemaResolverParams): SchemaNode | JsonError | undefined;
27
27
  }
28
- type ValidationResult = JsonError | Promise<JsonError | undefined>;
28
+ type Maybe<T> = T | undefined;
29
+ type ValidationAnnotation = JsonError | JsonAnnotation | Promise<Maybe<ValidationAnnotation>[]>;
30
+ type ValidationResult = Maybe<ValidationAnnotation>;
31
+ type ValidationReturnType = ValidationResult | ValidationResult[];
29
32
  type JsonSchemaValidatorParams = {
30
33
  pointer: string;
31
34
  data: unknown;
@@ -35,7 +38,7 @@ type JsonSchemaValidatorParams = {
35
38
  interface JsonSchemaValidator {
36
39
  toJSON?: () => string;
37
40
  order?: number;
38
- (options: JsonSchemaValidatorParams): undefined | Promise<undefined> | ValidationResult | ValidationResult[];
41
+ (options: JsonSchemaValidatorParams): ValidationReturnType;
39
42
  }
40
43
  type Keyword = {
41
44
  id: string; /** unique keyword corresponding to JSON Schema keywords (or custom) */
@@ -192,6 +195,7 @@ declare const errors: {
192
195
  "unique-items-error": string;
193
196
  "unknown-property-error": string;
194
197
  "value-not-empty-error": string;
198
+ "deprecated-warning": string;
195
199
  };
196
200
  //#endregion
197
201
  //#region src/SchemaNode.d.ts
@@ -310,7 +314,8 @@ interface SchemaNode extends SchemaNodeMethodsType {
310
314
  */
311
315
  interface SchemaNodeMethodsType {
312
316
  compileSchema(schema: JsonSchema, evaluationPath?: string, schemaLocation?: string, dynamicId?: string): SchemaNode;
313
- createError<T extends string = DefaultErrors>(code: T, data: ErrorData, message?: string): JsonError;
317
+ createError<T extends string = DefaultErrors>(code: T, data: AnnotationData, message?: string): JsonError;
318
+ createAnnotation<T extends string = DefaultErrors>(code: T, data: AnnotationData, message?: string): JsonAnnotation;
314
319
  createSchema(data?: unknown): JsonSchema;
315
320
  getNode(pointer: string, data: unknown, options: {
316
321
  withSchemaWarning: true;
@@ -330,7 +335,7 @@ interface SchemaNodeMethodsType {
330
335
  getNodeRef($ref: string): SchemaNode | undefined;
331
336
  getNodeRoot(): SchemaNode;
332
337
  getDraftVersion(): string;
333
- getData(data?: unknown, options?: TemplateOptions): unknown;
338
+ getData(data?: unknown, options?: TemplateOptions): any;
334
339
  reduceNode(data: unknown, options?: {
335
340
  key?: string | number;
336
341
  pointer?: string;
@@ -372,10 +377,14 @@ type ValidateReturnType = {
372
377
  * List of validation errors or empty
373
378
  */
374
379
  errors: JsonError[];
380
+ /**
381
+ * List of annotations from validators
382
+ */
383
+ annotations: JsonAnnotation[];
375
384
  /**
376
385
  * List of Promises resolving to `JsonError|undefined` or empty.
377
386
  */
378
- errorsAsync: Promise<JsonError | undefined>[];
387
+ errorsAsync: Promise<Maybe<ValidationAnnotation>[]>[];
379
388
  };
380
389
  //#endregion
381
390
  //#region src/types.d.ts
@@ -383,8 +392,20 @@ interface JsonSchema {
383
392
  [keyword: string]: any;
384
393
  }
385
394
  type JsonPointer = string;
395
+ type AnnotationData<D extends Record<string, unknown> = Record<string, unknown>> = D & {
396
+ pointer: string;
397
+ schema: JsonSchema;
398
+ value: unknown;
399
+ };
400
+ type Annotation<T = string, D extends AnnotationData = AnnotationData, S = string> = {
401
+ type: T;
402
+ code: S;
403
+ message: string;
404
+ data: D;
405
+ [p: string]: unknown;
406
+ };
386
407
  type DefaultErrors = keyof typeof errors;
387
- type ErrorConfig = Record<DefaultErrors | string, string | ((error: ErrorData) => void)>;
408
+ type ErrorConfig = Record<DefaultErrors | string, string | ((error: AnnotationData) => void)>;
388
409
  type OptionalNodeOrError = {
389
410
  node?: SchemaNode;
390
411
  error: undefined;
@@ -399,18 +420,14 @@ type NodeOrError = {
399
420
  node: undefined;
400
421
  error: JsonError;
401
422
  };
402
- type ErrorData<T extends Record<string, unknown> = Record<string, unknown>> = T & {
403
- pointer: string;
404
- schema: JsonSchema;
405
- value: unknown;
406
- };
407
- type JsonError<T extends ErrorData = ErrorData> = {
408
- type: "error";
409
- code: ErrorConfig | string;
410
- message: string;
411
- data: T;
412
- [p: string]: unknown;
413
- };
423
+ type JsonError<D extends AnnotationData = AnnotationData> = Annotation<"error", D, ErrorConfig | string>;
424
+ type JsonAnnotation<D extends AnnotationData = AnnotationData> = Annotation<"annotation", D>;
425
+ declare function isAnnotation(value: any): value is Annotation;
426
+ /**
427
+ * ts type guard for json error
428
+ * @returns true if passed type is a JsonError
429
+ */
430
+ declare function isJsonAnnotation(error: unknown): error is JsonAnnotation;
414
431
  /**
415
432
  * ts type guard for json error
416
433
  * @returns true if passed type is a JsonError
@@ -580,8 +597,14 @@ type SchemaType = (typeof SCHEMA_TYPES)[number];
580
597
  */
581
598
  declare function getSchemaType(node: SchemaNode, data: unknown): SchemaType | undefined;
582
599
  //#endregion
600
+ //#region src/utils/sanitizeErrors.d.ts
601
+ /**
602
+ * Flattens nested validation array results and filters items to only include errors, annotations and promises
603
+ */
604
+ declare function sanitizeErrors(list: ValidationReturnType | ValidationReturnType[] | ValidationAnnotation[], result?: ValidationAnnotation[]): ValidationAnnotation[];
605
+ //#endregion
583
606
  //#region remotes/index.d.ts
584
607
  /** remote meta-schema stored by schema $id */
585
608
  declare const remotes: Record<string, any>;
586
609
  //#endregion
587
- export { type CompileOptions, type Context, type DataNode, type Draft, type DraftVersion, type ErrorConfig, type ErrorData, type GetNodeOptions, type JsonError, type JsonPointer, type JsonSchema, type JsonSchemaReducer, type JsonSchemaReducerParams, type JsonSchemaResolver, type JsonSchemaResolverParams, type JsonSchemaValidator, type JsonSchemaValidatorParams, type Keyword, type NodeOrError, type OptionalNodeOrError, type SchemaNode, type ValidateReturnType, type ValidationPath, addKeywords, compileSchema, draft04, draft06, draft07, draft2019, draft2020, draftEditor, extendDraft, getSchemaType, getTypeOf, isJsonError, isReduceable, isSchemaNode, mergeNode, mergeSchema, oneOfFuzzyKeyword, oneOfKeyword, remotes, render, _default as settings };
610
+ export { type Annotation, type AnnotationData, type CompileOptions, type Context, type DataNode, type Draft, type DraftVersion, type ErrorConfig, type GetNodeOptions, type JsonAnnotation, type JsonError, type JsonPointer, type JsonSchema, type JsonSchemaReducer, type JsonSchemaReducerParams, type JsonSchemaResolver, type JsonSchemaResolverParams, type JsonSchemaValidator, type JsonSchemaValidatorParams, type Keyword, type Maybe, type NodeOrError, type OptionalNodeOrError, type SchemaNode, type ValidateReturnType, type ValidationAnnotation, type ValidationPath, type ValidationReturnType, addKeywords, compileSchema, draft04, draft06, draft07, draft2019, draft2020, draftEditor, extendDraft, getSchemaType, getTypeOf, isAnnotation, isJsonAnnotation, isJsonError, isReduceable, isSchemaNode, mergeNode, mergeSchema, oneOfFuzzyKeyword, oneOfKeyword, remotes, render, sanitizeErrors, _default as settings };
package/dist/index.d.mts CHANGED
@@ -25,7 +25,10 @@ interface JsonSchemaResolver {
25
25
  order?: number;
26
26
  (options: JsonSchemaResolverParams): SchemaNode | JsonError | undefined;
27
27
  }
28
- type ValidationResult = JsonError | Promise<JsonError | undefined>;
28
+ type Maybe<T> = T | undefined;
29
+ type ValidationAnnotation = JsonError | JsonAnnotation | Promise<Maybe<ValidationAnnotation>[]>;
30
+ type ValidationResult = Maybe<ValidationAnnotation>;
31
+ type ValidationReturnType = ValidationResult | ValidationResult[];
29
32
  type JsonSchemaValidatorParams = {
30
33
  pointer: string;
31
34
  data: unknown;
@@ -35,7 +38,7 @@ type JsonSchemaValidatorParams = {
35
38
  interface JsonSchemaValidator {
36
39
  toJSON?: () => string;
37
40
  order?: number;
38
- (options: JsonSchemaValidatorParams): undefined | Promise<undefined> | ValidationResult | ValidationResult[];
41
+ (options: JsonSchemaValidatorParams): ValidationReturnType;
39
42
  }
40
43
  type Keyword = {
41
44
  id: string; /** unique keyword corresponding to JSON Schema keywords (or custom) */
@@ -192,6 +195,7 @@ declare const errors: {
192
195
  "unique-items-error": string;
193
196
  "unknown-property-error": string;
194
197
  "value-not-empty-error": string;
198
+ "deprecated-warning": string;
195
199
  };
196
200
  //#endregion
197
201
  //#region src/SchemaNode.d.ts
@@ -310,7 +314,8 @@ interface SchemaNode extends SchemaNodeMethodsType {
310
314
  */
311
315
  interface SchemaNodeMethodsType {
312
316
  compileSchema(schema: JsonSchema, evaluationPath?: string, schemaLocation?: string, dynamicId?: string): SchemaNode;
313
- createError<T extends string = DefaultErrors>(code: T, data: ErrorData, message?: string): JsonError;
317
+ createError<T extends string = DefaultErrors>(code: T, data: AnnotationData, message?: string): JsonError;
318
+ createAnnotation<T extends string = DefaultErrors>(code: T, data: AnnotationData, message?: string): JsonAnnotation;
314
319
  createSchema(data?: unknown): JsonSchema;
315
320
  getNode(pointer: string, data: unknown, options: {
316
321
  withSchemaWarning: true;
@@ -330,7 +335,7 @@ interface SchemaNodeMethodsType {
330
335
  getNodeRef($ref: string): SchemaNode | undefined;
331
336
  getNodeRoot(): SchemaNode;
332
337
  getDraftVersion(): string;
333
- getData(data?: unknown, options?: TemplateOptions): unknown;
338
+ getData(data?: unknown, options?: TemplateOptions): any;
334
339
  reduceNode(data: unknown, options?: {
335
340
  key?: string | number;
336
341
  pointer?: string;
@@ -372,10 +377,14 @@ type ValidateReturnType = {
372
377
  * List of validation errors or empty
373
378
  */
374
379
  errors: JsonError[];
380
+ /**
381
+ * List of annotations from validators
382
+ */
383
+ annotations: JsonAnnotation[];
375
384
  /**
376
385
  * List of Promises resolving to `JsonError|undefined` or empty.
377
386
  */
378
- errorsAsync: Promise<JsonError | undefined>[];
387
+ errorsAsync: Promise<Maybe<ValidationAnnotation>[]>[];
379
388
  };
380
389
  //#endregion
381
390
  //#region src/types.d.ts
@@ -383,8 +392,20 @@ interface JsonSchema {
383
392
  [keyword: string]: any;
384
393
  }
385
394
  type JsonPointer = string;
395
+ type AnnotationData<D extends Record<string, unknown> = Record<string, unknown>> = D & {
396
+ pointer: string;
397
+ schema: JsonSchema;
398
+ value: unknown;
399
+ };
400
+ type Annotation<T = string, D extends AnnotationData = AnnotationData, S = string> = {
401
+ type: T;
402
+ code: S;
403
+ message: string;
404
+ data: D;
405
+ [p: string]: unknown;
406
+ };
386
407
  type DefaultErrors = keyof typeof errors;
387
- type ErrorConfig = Record<DefaultErrors | string, string | ((error: ErrorData) => void)>;
408
+ type ErrorConfig = Record<DefaultErrors | string, string | ((error: AnnotationData) => void)>;
388
409
  type OptionalNodeOrError = {
389
410
  node?: SchemaNode;
390
411
  error: undefined;
@@ -399,18 +420,14 @@ type NodeOrError = {
399
420
  node: undefined;
400
421
  error: JsonError;
401
422
  };
402
- type ErrorData<T extends Record<string, unknown> = Record<string, unknown>> = T & {
403
- pointer: string;
404
- schema: JsonSchema;
405
- value: unknown;
406
- };
407
- type JsonError<T extends ErrorData = ErrorData> = {
408
- type: "error";
409
- code: ErrorConfig | string;
410
- message: string;
411
- data: T;
412
- [p: string]: unknown;
413
- };
423
+ type JsonError<D extends AnnotationData = AnnotationData> = Annotation<"error", D, ErrorConfig | string>;
424
+ type JsonAnnotation<D extends AnnotationData = AnnotationData> = Annotation<"annotation", D>;
425
+ declare function isAnnotation(value: any): value is Annotation;
426
+ /**
427
+ * ts type guard for json error
428
+ * @returns true if passed type is a JsonError
429
+ */
430
+ declare function isJsonAnnotation(error: unknown): error is JsonAnnotation;
414
431
  /**
415
432
  * ts type guard for json error
416
433
  * @returns true if passed type is a JsonError
@@ -580,8 +597,14 @@ type SchemaType = (typeof SCHEMA_TYPES)[number];
580
597
  */
581
598
  declare function getSchemaType(node: SchemaNode, data: unknown): SchemaType | undefined;
582
599
  //#endregion
600
+ //#region src/utils/sanitizeErrors.d.ts
601
+ /**
602
+ * Flattens nested validation array results and filters items to only include errors, annotations and promises
603
+ */
604
+ declare function sanitizeErrors(list: ValidationReturnType | ValidationReturnType[] | ValidationAnnotation[], result?: ValidationAnnotation[]): ValidationAnnotation[];
605
+ //#endregion
583
606
  //#region remotes/index.d.ts
584
607
  /** remote meta-schema stored by schema $id */
585
608
  declare const remotes: Record<string, any>;
586
609
  //#endregion
587
- export { type CompileOptions, type Context, type DataNode, type Draft, type DraftVersion, type ErrorConfig, type ErrorData, type GetNodeOptions, type JsonError, type JsonPointer, type JsonSchema, type JsonSchemaReducer, type JsonSchemaReducerParams, type JsonSchemaResolver, type JsonSchemaResolverParams, type JsonSchemaValidator, type JsonSchemaValidatorParams, type Keyword, type NodeOrError, type OptionalNodeOrError, type SchemaNode, type ValidateReturnType, type ValidationPath, addKeywords, compileSchema, draft04, draft06, draft07, draft2019, draft2020, draftEditor, extendDraft, getSchemaType, getTypeOf, isJsonError, isReduceable, isSchemaNode, mergeNode, mergeSchema, oneOfFuzzyKeyword, oneOfKeyword, remotes, render, _default as settings };
610
+ export { type Annotation, type AnnotationData, type CompileOptions, type Context, type DataNode, type Draft, type DraftVersion, type ErrorConfig, type GetNodeOptions, type JsonAnnotation, type JsonError, type JsonPointer, type JsonSchema, type JsonSchemaReducer, type JsonSchemaReducerParams, type JsonSchemaResolver, type JsonSchemaResolverParams, type JsonSchemaValidator, type JsonSchemaValidatorParams, type Keyword, type Maybe, type NodeOrError, type OptionalNodeOrError, type SchemaNode, type ValidateReturnType, type ValidationAnnotation, type ValidationPath, type ValidationReturnType, addKeywords, compileSchema, draft04, draft06, draft07, draft2019, draft2020, draftEditor, extendDraft, getSchemaType, getTypeOf, isAnnotation, isJsonAnnotation, isJsonError, isReduceable, isSchemaNode, mergeNode, mergeSchema, oneOfFuzzyKeyword, oneOfKeyword, remotes, render, sanitizeErrors, _default as settings };