z-schema 7.0.5 → 7.0.7

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 (75) hide show
  1. package/README.md +0 -9
  2. package/cjs/index.d.ts +179 -119
  3. package/cjs/index.js +1048 -1101
  4. package/{src/FormatValidators.ts → dist/format-validators.js} +97 -65
  5. package/dist/index.js +1 -1
  6. package/dist/json-schema.js +40 -0
  7. package/dist/{JsonValidation.js → json-validation.js} +75 -69
  8. package/dist/{Report.js → report.js} +35 -45
  9. package/dist/schema-cache.js +109 -0
  10. package/dist/schema-compiler.js +255 -0
  11. package/dist/{SchemaValidation.js → schema-validator.js} +153 -149
  12. package/dist/types/{Errors.d.ts → errors.d.ts} +2 -0
  13. package/dist/types/format-validators.d.ts +10 -0
  14. package/dist/types/index.d.ts +6 -1
  15. package/dist/types/json-schema.d.ts +50 -0
  16. package/dist/types/json-validation.d.ts +7 -0
  17. package/dist/types/{Report.d.ts → report.d.ts} +22 -23
  18. package/dist/types/schema-cache.d.ts +17 -0
  19. package/dist/types/schema-compiler.d.ts +16 -0
  20. package/dist/types/schema-validator.d.ts +10 -0
  21. package/dist/types/utils/array.d.ts +2 -0
  22. package/dist/types/utils/clone.d.ts +2 -0
  23. package/dist/types/utils/json.d.ts +7 -0
  24. package/dist/types/utils/symbols.d.ts +2 -0
  25. package/dist/types/utils/unicode.d.ts +14 -0
  26. package/dist/types/utils/uri.d.ts +4 -0
  27. package/dist/types/utils/what-is.d.ts +3 -0
  28. package/dist/types/z-schema.d.ts +75 -0
  29. package/dist/utils/array.js +27 -0
  30. package/dist/utils/clone.js +61 -0
  31. package/dist/utils/json.js +59 -0
  32. package/dist/utils/symbols.js +2 -0
  33. package/dist/utils/unicode.js +45 -0
  34. package/dist/utils/uri.js +15 -0
  35. package/dist/utils/what-is.js +29 -0
  36. package/dist/{ZSchema.js → z-schema.js} +69 -84
  37. package/package.json +6 -3
  38. package/src/{Errors.ts → errors.ts} +4 -0
  39. package/src/format-validators.ts +191 -0
  40. package/src/index.ts +8 -1
  41. package/src/json-schema.ts +97 -0
  42. package/src/{JsonValidation.ts → json-validation.ts} +137 -127
  43. package/src/{Report.ts → report.ts} +60 -70
  44. package/src/schema-cache.ts +122 -0
  45. package/src/schema-compiler.ts +300 -0
  46. package/src/{SchemaValidation.ts → schema-validator.ts} +213 -215
  47. package/src/utils/array.ts +29 -0
  48. package/src/utils/clone.ts +63 -0
  49. package/src/utils/json.ts +74 -0
  50. package/src/utils/symbols.ts +3 -0
  51. package/src/utils/unicode.ts +43 -0
  52. package/src/utils/uri.ts +18 -0
  53. package/src/utils/what-is.ts +46 -0
  54. package/src/{ZSchema.ts → z-schema.ts} +112 -121
  55. package/umd/ZSchema.js +1048 -1101
  56. package/umd/ZSchema.min.js +1 -1
  57. package/dist/FormatValidators.js +0 -136
  58. package/dist/SchemaCache.js +0 -172
  59. package/dist/SchemaCompilation.js +0 -259
  60. package/dist/Utils.js +0 -266
  61. package/dist/types/FormatValidators.d.ts +0 -12
  62. package/dist/types/JsonValidation.d.ts +0 -37
  63. package/dist/types/SchemaCache.d.ts +0 -26
  64. package/dist/types/SchemaCompilation.d.ts +0 -1
  65. package/dist/types/SchemaValidation.d.ts +0 -6
  66. package/dist/types/Utils.d.ts +0 -64
  67. package/dist/types/ZSchema.d.ts +0 -99
  68. package/src/SchemaCache.ts +0 -188
  69. package/src/SchemaCompilation.ts +0 -293
  70. package/src/Utils.ts +0 -286
  71. /package/dist/{Errors.js → errors.js} +0 -0
  72. /package/dist/schemas/{hyper-schema.json → draft-04-hyper-schema.json} +0 -0
  73. /package/dist/schemas/{schema.json → draft-04-schema.json} +0 -0
  74. /package/src/schemas/{hyper-schema.json → draft-04-hyper-schema.json} +0 -0
  75. /package/src/schemas/{schema.json → draft-04-schema.json} +0 -0
package/README.md CHANGED
@@ -10,7 +10,6 @@
10
10
  - [Usage](#usage)
11
11
  - [Features](#features)
12
12
  - [Options](#options)
13
- - [Benchmarks](#benchmarks)
14
13
  - [Contributing](#contributing)
15
14
  - [Contributors](#contributors)
16
15
 
@@ -601,14 +600,6 @@ console.log(validator.getLastErrors());
601
600
 
602
601
  **Note:** before creating your own keywords you should consider all compatibility issues.
603
602
 
604
- ## Benchmarks
605
-
606
- So how does it compare to version 2.x and others?
607
-
608
- **NOTE: these tests are purely orientational, they don't consider extra features any of the validator may support and implement**
609
-
610
- [rawgithub.com/zaggino/z-schema/master/benchmark/results.html](https://rawgithub.com/zaggino/z-schema/master/benchmark/results.html)
611
-
612
603
  ## Contributing:
613
604
 
614
605
  These repository has several submodules and should be cloned as follows:
package/cjs/index.d.ts CHANGED
@@ -1,3 +1,113 @@
1
+ type ErrorCode = keyof typeof Errors;
2
+ type ErrorParam = string | number | Array<string | number>;
3
+ declare const Errors: {
4
+ INVALID_TYPE: string;
5
+ INVALID_FORMAT: string;
6
+ ENUM_MISMATCH: string;
7
+ ENUM_CASE_MISMATCH: string;
8
+ ANY_OF_MISSING: string;
9
+ ONE_OF_MISSING: string;
10
+ ONE_OF_MULTIPLE: string;
11
+ NOT_PASSED: string;
12
+ ARRAY_LENGTH_SHORT: string;
13
+ ARRAY_LENGTH_LONG: string;
14
+ ARRAY_UNIQUE: string;
15
+ ARRAY_ADDITIONAL_ITEMS: string;
16
+ MULTIPLE_OF: string;
17
+ MINIMUM: string;
18
+ MINIMUM_EXCLUSIVE: string;
19
+ MAXIMUM: string;
20
+ MAXIMUM_EXCLUSIVE: string;
21
+ OBJECT_PROPERTIES_MINIMUM: string;
22
+ OBJECT_PROPERTIES_MAXIMUM: string;
23
+ OBJECT_MISSING_REQUIRED_PROPERTY: string;
24
+ OBJECT_ADDITIONAL_PROPERTIES: string;
25
+ OBJECT_DEPENDENCY_KEY: string;
26
+ MIN_LENGTH: string;
27
+ MAX_LENGTH: string;
28
+ PATTERN: string;
29
+ KEYWORD_TYPE_EXPECTED: string;
30
+ KEYWORD_UNDEFINED_STRICT: string;
31
+ KEYWORD_UNEXPECTED: string;
32
+ KEYWORD_MUST_BE: string;
33
+ KEYWORD_DEPENDENCY: string;
34
+ KEYWORD_PATTERN: string;
35
+ KEYWORD_VALUE_TYPE: string;
36
+ UNKNOWN_FORMAT: string;
37
+ CUSTOM_MODE_FORCE_PROPERTIES: string;
38
+ REF_UNRESOLVED: string;
39
+ UNRESOLVABLE_REFERENCE: string;
40
+ SCHEMA_NOT_REACHABLE: string;
41
+ SCHEMA_TYPE_EXPECTED: string;
42
+ SCHEMA_NOT_AN_OBJECT: string;
43
+ ASYNC_TIMEOUT: string;
44
+ PARENT_SCHEMA_VALIDATION_FAILED: string;
45
+ REMOTE_NOT_VALID: string;
46
+ };
47
+
48
+ interface Reference {
49
+ ref: string;
50
+ key: '$ref' | '$schema';
51
+ obj: JsonSchemaInternal;
52
+ path: Array<string | number>;
53
+ }
54
+ declare class SchemaCompiler {
55
+ private validator;
56
+ constructor(validator: ZSchema);
57
+ compileSchema(report: Report, schema: JsonSchemaInternal): boolean;
58
+ compileArrayOfSchemas(report: Report, arr: JsonSchemaInternal[]): boolean;
59
+ compileArrayOfSchemasLoop(mainReport: Report, arr: JsonSchemaInternal[]): number;
60
+ }
61
+
62
+ interface JsonSchema {
63
+ $ref?: string;
64
+ id?: string;
65
+ $schema?: string;
66
+ title?: string;
67
+ description?: string;
68
+ default?: unknown;
69
+ definitions?: Record<string, JsonSchema>;
70
+ type?: string | string[];
71
+ properties?: Record<string, JsonSchema>;
72
+ patternProperties?: Record<string, JsonSchema>;
73
+ dependencies?: Record<string, string[]>;
74
+ multipleOf?: number;
75
+ minimum?: number;
76
+ exclusiveMinimum?: boolean;
77
+ maximum?: number;
78
+ exclusiveMaximum?: boolean;
79
+ minLength?: number;
80
+ maxLength?: number;
81
+ pattern?: string;
82
+ additionalItems?: boolean | JsonSchema;
83
+ items?: JsonSchema | JsonSchema[];
84
+ minItems?: number;
85
+ maxItems?: number;
86
+ uniqueItems?: boolean;
87
+ minProperties?: number;
88
+ maxProperties?: number;
89
+ required?: string[];
90
+ additionalProperties?: boolean | JsonSchema;
91
+ enum?: Array<unknown>;
92
+ format?: string;
93
+ allOf?: JsonSchema[];
94
+ anyOf?: JsonSchema[];
95
+ oneOf?: JsonSchema[];
96
+ not?: JsonSchema;
97
+ }
98
+ type JsonSchemaType = 'array' | 'boolean' | 'integer' | 'null' | 'number' | 'object' | 'string';
99
+ interface ZSchemaInternalProperties {
100
+ __$compiled?: unknown;
101
+ __$missingReferences?: Reference[];
102
+ __$refResolved?: JsonSchema;
103
+ __$schemaResolved?: unknown;
104
+ __$validated?: boolean;
105
+ __$validationOptions?: unknown;
106
+ __$visited?: boolean;
107
+ }
108
+ interface JsonSchemaInternal extends JsonSchema, ZSchemaInternalProperties {
109
+ }
110
+
1
111
  interface SchemaError extends Error {
2
112
  /**
3
113
  * Implements the Error.name contract. The value is always "z-schema validation error".
@@ -28,12 +138,12 @@ interface SchemaErrorDetail {
28
138
  * Format parameters that can be used to format a custom error message.
29
139
  * Example: ["string","array"]
30
140
  */
31
- params: Array<string>;
141
+ params: ErrorParam[];
32
142
  /**
33
143
  * A JSON path indicating the location of the error.
34
144
  * Example: "#/projects/1"
35
145
  */
36
- path: string | string[];
146
+ path: string | Array<string | number>;
37
147
  /**
38
148
  * The schema rule description, which is included for certain errors where
39
149
  * this information is useful (e.g. to describe a constraint).
@@ -57,82 +167,60 @@ type TaskFnArgs = Parameters<TaskFn>;
57
167
  type TaskProcessFn = (result: ReturnType<TaskFn>) => void;
58
168
  type AsyncTask = [TaskFn, TaskFnArgs, TaskProcessFn];
59
169
  declare class Report {
170
+ asyncTasks: AsyncTask[];
171
+ commonErrorMessage?: string;
60
172
  errors: SchemaErrorDetail[];
173
+ json?: unknown;
174
+ path: Array<number | string>;
175
+ rootSchema?: JsonSchemaInternal;
61
176
  parentReport?: Report;
62
177
  options: ZSchemaOptions;
63
178
  reportOptions: ReportOptions;
64
- path: string[];
65
- asyncTasks: AsyncTask[];
66
- rootSchema?: {
67
- id?: string;
68
- };
69
- commonErrorMessage?: string;
70
- json?: unknown;
71
- constructor(parentOrOptions: any, reportOptions?: any);
179
+ constructor(zschemaOptions: ZSchemaOptions);
180
+ constructor(parentReport: Report);
181
+ constructor(parentReport: Report, reportOptions: ReportOptions);
72
182
  isValid(): boolean;
73
- addAsyncTask(fn: any, args: any, asyncTaskResultProcessFn: any): void;
74
- getAncestor(id: any): any;
75
- processAsyncTasks(timeout: any, callback: any): void;
76
- getPath(returnPathAsString: any): string | any[];
77
- getSchemaId(): any;
78
- hasError(errorCode: any, params: any): boolean;
79
- addError(errorCode: any, params: any, subReports?: any, schema?: any): void;
80
- getJson(): any;
81
- addCustomError(errorCode: string, errorMessage: string, params: string[], subReports?: Report[] | Report, schema?: {
82
- title?: string;
83
- description?: string;
84
- }): void;
183
+ addAsyncTask<FV, FN extends (...args: any[]) => FV>(fn: FN, args: Parameters<FN>, asyncTaskResultProcessFn: (result: ReturnType<FN>) => void): void;
184
+ getAncestor(id: string): Report | undefined;
185
+ processAsyncTasks(timeout: number | undefined, callback: ValidateCallback): void;
186
+ getPath(returnPathAsString?: boolean): string | (string | number)[];
187
+ getSchemaId(): string | undefined;
188
+ hasError(errCode: string, errParams: Array<any>): boolean;
189
+ addError(errCode: ErrorCode, errParams?: ErrorParam[], subReports?: Report | Report[], schema?: JsonSchema): void;
190
+ getJson(): unknown;
191
+ addCustomError(errorCode: string, errorMessage: string, params?: ErrorParam[], subReports?: Report | Report[], schema?: JsonSchema): void;
85
192
  }
86
193
 
87
- declare const Errors: {
88
- INVALID_TYPE: string;
89
- INVALID_FORMAT: string;
90
- ENUM_MISMATCH: string;
91
- ENUM_CASE_MISMATCH: string;
92
- ANY_OF_MISSING: string;
93
- ONE_OF_MISSING: string;
94
- ONE_OF_MULTIPLE: string;
95
- NOT_PASSED: string;
96
- ARRAY_LENGTH_SHORT: string;
97
- ARRAY_LENGTH_LONG: string;
98
- ARRAY_UNIQUE: string;
99
- ARRAY_ADDITIONAL_ITEMS: string;
100
- MULTIPLE_OF: string;
101
- MINIMUM: string;
102
- MINIMUM_EXCLUSIVE: string;
103
- MAXIMUM: string;
104
- MAXIMUM_EXCLUSIVE: string;
105
- OBJECT_PROPERTIES_MINIMUM: string;
106
- OBJECT_PROPERTIES_MAXIMUM: string;
107
- OBJECT_MISSING_REQUIRED_PROPERTY: string;
108
- OBJECT_ADDITIONAL_PROPERTIES: string;
109
- OBJECT_DEPENDENCY_KEY: string;
110
- MIN_LENGTH: string;
111
- MAX_LENGTH: string;
112
- PATTERN: string;
113
- KEYWORD_TYPE_EXPECTED: string;
114
- KEYWORD_UNDEFINED_STRICT: string;
115
- KEYWORD_UNEXPECTED: string;
116
- KEYWORD_MUST_BE: string;
117
- KEYWORD_DEPENDENCY: string;
118
- KEYWORD_PATTERN: string;
119
- KEYWORD_VALUE_TYPE: string;
120
- UNKNOWN_FORMAT: string;
121
- CUSTOM_MODE_FORCE_PROPERTIES: string;
122
- REF_UNRESOLVED: string;
123
- UNRESOLVABLE_REFERENCE: string;
124
- SCHEMA_NOT_REACHABLE: string;
125
- SCHEMA_TYPE_EXPECTED: string;
126
- SCHEMA_NOT_AN_OBJECT: string;
127
- ASYNC_TIMEOUT: string;
128
- PARENT_SCHEMA_VALIDATION_FAILED: string;
129
- REMOTE_NOT_VALID: string;
130
- };
194
+ type FormatValidatorFn = (input: unknown) => boolean;
195
+
196
+ type SchemaCacheStorage = Record<string, JsonSchemaInternal>;
197
+ type ReferenceSchemaCacheStorage = Array<[JsonSchemaInternal, JsonSchemaInternal]>;
198
+ declare class SchemaCache {
199
+ private validator;
200
+ cache: SchemaCacheStorage;
201
+ referenceCache: ReferenceSchemaCacheStorage;
202
+ constructor(validator: ZSchema);
203
+ cacheSchemaByUri(uri: string, schema: JsonSchemaInternal): void;
204
+ removeFromCacheByUri(uri: string): void;
205
+ checkCacheForUri(uri: string): boolean;
206
+ getSchema(report: Report, refOrSchema: string | JsonSchemaInternal): JsonSchemaInternal | undefined;
207
+ getSchemaByUri(report: Report, uri: string, root?: JsonSchemaInternal): JsonSchemaInternal | undefined;
208
+ getSchemaByReference(report: Report, schema: JsonSchemaInternal): JsonSchemaInternal;
209
+ }
210
+
211
+ declare class SchemaValidator {
212
+ private validator;
213
+ constructor(validator: ZSchema);
214
+ get options(): ZSchemaOptions;
215
+ validateArrayOfSchemas(report: Report, arr: JsonSchemaInternal[]): boolean;
216
+ validateSchema(report: Report, schema: JsonSchemaInternal | JsonSchemaInternal[]): boolean;
217
+ }
131
218
 
132
219
  interface ZSchemaOptions {
133
220
  asyncTimeout?: number;
134
221
  forceAdditional?: boolean;
135
- assumeAdditional?: boolean;
222
+ assumeAdditional?: boolean | string[];
223
+ enumCaseInsensitiveComparison?: boolean;
136
224
  forceItems?: boolean;
137
225
  forceMinItems?: boolean;
138
226
  forceMaxItems?: boolean;
@@ -156,74 +244,46 @@ interface ValidateOptions {
156
244
  schemaPath?: string;
157
245
  includeErrors?: Array<keyof typeof Errors>;
158
246
  }
159
- type ValidateCallback = (e: Error, valid: boolean) => void;
160
- type SchemaReader = (uri: string) => unknown;
247
+ type ValidateCallback = (e: Error | SchemaErrorDetail[] | null, valid: boolean) => void;
248
+ type SchemaReader = (uri: string) => JsonSchema;
161
249
  declare class ZSchema {
162
- lastReport: Report | undefined;
163
- /**
164
- * Register a custom format.
165
- *
166
- * @param name - name of the custom format
167
- * @param validatorFunction - custom format validator function.
168
- * Returns `true` if `value` matches the custom format.
169
- */
170
- static registerFormat(formatName: string, validatorFunction: (value: unknown) => boolean): void;
171
- /**
172
- * Unregister a format.
173
- *
174
- * @param name - name of the custom format
175
- */
250
+ static registerFormat(name: string, validatorFunction: FormatValidatorFn): void;
176
251
  static unregisterFormat(name: string): void;
177
- /**
178
- * Get the list of all registered formats.
179
- *
180
- * Both the names of the burned-in formats and the custom format names are
181
- * returned by this function.
182
- *
183
- * @returns {string[]} the list of all registered format names.
184
- */
185
252
  static getRegisteredFormats(): string[];
186
253
  static getDefaultOptions(): ZSchemaOptions;
187
- private cache;
188
- private referenceCache;
189
- private validateOptions;
254
+ lastReport: Report | undefined;
255
+ scache: SchemaCache;
256
+ sc: SchemaCompiler;
257
+ sv: SchemaValidator;
258
+ validateOptions: ValidateOptions;
190
259
  options: ZSchemaOptions;
191
260
  constructor(options?: ZSchemaOptions);
192
- /** Used by SchemaCache to break circular dependency with SchemaCompilation */
193
- _compileSchema(report: Report, schema: unknown): boolean;
194
- /**
195
- * @param schema - JSON object representing schema
196
- * @returns {boolean} true if schema is valid.
197
- */
198
- validateSchema(schema: unknown): boolean;
199
- /**
200
- * @param json - either a JSON string or a parsed JSON object
201
- * @param schema - the JSON object representing the schema
202
- * @returns true if json matches schema
203
- */
204
- validate(json: any, schema: any, options?: ValidateOptions, callback?: ValidateCallback): boolean;
205
- validate(json: any, schema: any, callback?: any): boolean;
206
- validate(json: any, schema: any): boolean;
261
+ validateSchema(schema: JsonSchema): boolean;
262
+ validate(json: unknown, schema: JsonSchema | string, options?: ValidateOptions, callback?: ValidateCallback): void;
263
+ validate(json: unknown, schema: JsonSchema | string, callback?: ValidateCallback): void;
264
+ validate(json: unknown, schema: JsonSchema | string, options?: ValidateOptions): boolean;
265
+ validate(json: unknown, schema: JsonSchema | string): boolean;
207
266
  /**
208
267
  * Returns an Error object for the most recent failed validation, or null if the validation was successful.
209
268
  */
210
- getLastError(): SchemaError;
269
+ getLastError(): SchemaError | null;
211
270
  /**
212
271
  * Returns the error details for the most recent validation, or undefined if the validation was successful.
213
272
  * This is the same list as the SchemaError.details property.
214
273
  */
215
- getLastErrors(): SchemaErrorDetail[];
216
- setRemoteReference(uri: any, schema: any, validationOptions: any): void;
217
- compileSchema(schema: any): boolean;
218
- getMissingReferences(arr?: any): any[];
219
- getMissingRemoteReferences(): any[];
220
- getResolvedSchema(schema: any): any;
274
+ getLastErrors(): SchemaErrorDetail[] | null;
275
+ setRemoteReference(uri: string, schema: string | JsonSchema, validationOptions: ZSchemaOptions): void;
276
+ compileSchema(schema: JsonSchema): boolean;
277
+ getMissingReferences(arr?: SchemaErrorDetail[]): string[];
278
+ getMissingRemoteReferences(): string[];
279
+ getResolvedSchema(schema: JsonSchema): JsonSchema;
221
280
  static schemaReader: SchemaReader;
222
- setSchemaReader(schemaReader: any): void;
281
+ setSchemaReader(schemaReader: SchemaReader): void;
223
282
  getSchemaReader(): SchemaReader;
224
- static setSchemaReader(schemaReader: any): void;
283
+ static setSchemaReader(schemaReader: SchemaReader): void;
225
284
  static schemaSymbol: symbol;
226
285
  static jsonSymbol: symbol;
227
286
  }
228
287
 
229
- export { ZSchema as default };
288
+ export { Errors, Report, ZSchema as default };
289
+ export type { ErrorCode, ErrorParam, FormatValidatorFn, JsonSchema, JsonSchemaType, SchemaError, SchemaErrorDetail, SchemaReader, ValidateCallback, ValidateOptions, ZSchemaOptions };