z-schema 7.0.5 → 7.0.6

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 +192 -119
  3. package/cjs/index.js +1044 -1097
  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 +10 -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} +66 -81
  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 +12 -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} +108 -118
  55. package/umd/ZSchema.js +1044 -1097
  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,114 @@
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
+ declare const findId: (schema: JsonSchemaInternal, id: string) => JsonSchemaInternal | undefined;
111
+
1
112
  interface SchemaError extends Error {
2
113
  /**
3
114
  * Implements the Error.name contract. The value is always "z-schema validation error".
@@ -28,12 +139,12 @@ interface SchemaErrorDetail {
28
139
  * Format parameters that can be used to format a custom error message.
29
140
  * Example: ["string","array"]
30
141
  */
31
- params: Array<string>;
142
+ params: ErrorParam[];
32
143
  /**
33
144
  * A JSON path indicating the location of the error.
34
145
  * Example: "#/projects/1"
35
146
  */
36
- path: string | string[];
147
+ path: string | Array<string | number>;
37
148
  /**
38
149
  * The schema rule description, which is included for certain errors where
39
150
  * this information is useful (e.g. to describe a constraint).
@@ -57,82 +168,69 @@ type TaskFnArgs = Parameters<TaskFn>;
57
168
  type TaskProcessFn = (result: ReturnType<TaskFn>) => void;
58
169
  type AsyncTask = [TaskFn, TaskFnArgs, TaskProcessFn];
59
170
  declare class Report {
171
+ asyncTasks: AsyncTask[];
172
+ commonErrorMessage?: string;
60
173
  errors: SchemaErrorDetail[];
174
+ json?: unknown;
175
+ path: Array<number | string>;
176
+ rootSchema?: JsonSchemaInternal;
61
177
  parentReport?: Report;
62
178
  options: ZSchemaOptions;
63
179
  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);
180
+ constructor(zschemaOptions: ZSchemaOptions);
181
+ constructor(parentReport: Report);
182
+ constructor(parentReport: Report, reportOptions: ReportOptions);
72
183
  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;
184
+ addAsyncTask<FV, FN extends (...args: any[]) => FV>(fn: FN, args: Parameters<FN>, asyncTaskResultProcessFn: (result: ReturnType<FN>) => void): void;
185
+ getAncestor(id: string): Report | undefined;
186
+ processAsyncTasks(timeout: number | undefined, callback: ValidateCallback): void;
187
+ getPath(returnPathAsString?: boolean): string | (string | number)[];
188
+ getSchemaId(): string | undefined;
189
+ hasError(errCode: string, errParams: Array<any>): boolean;
190
+ addError(errCode: ErrorCode, errParams?: ErrorParam[], subReports?: Report | Report[], schema?: JsonSchema): void;
191
+ getJson(): unknown;
192
+ addCustomError(errorCode: string, errorMessage: string, params?: ErrorParam[], subReports?: Report | Report[], schema?: JsonSchema): void;
85
193
  }
86
194
 
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
- };
195
+ type FormatValidatorFn = (input: unknown) => boolean;
196
+ interface FormatValidatorsOptions {
197
+ strictUris?: boolean;
198
+ }
199
+ declare function getFormatValidators(options?: FormatValidatorsOptions): Record<string, FormatValidatorFn>;
200
+ declare function registerFormat(name: string, validatorFunction: FormatValidatorFn): void;
201
+ declare function unregisterFormat(name: string): void;
202
+ declare function getSupportedFormats(): string[];
203
+ declare function isFormatSupported(name: string): boolean;
204
+ declare function getRegisteredFormats(): string[];
205
+
206
+ type SchemaCacheStorage = Record<string, JsonSchemaInternal>;
207
+ type ReferenceSchemaCacheStorage = Array<[JsonSchemaInternal, JsonSchemaInternal]>;
208
+ declare class SchemaCache {
209
+ private validator;
210
+ cache: SchemaCacheStorage;
211
+ referenceCache: ReferenceSchemaCacheStorage;
212
+ constructor(validator: ZSchema);
213
+ cacheSchemaByUri(uri: string, schema: JsonSchemaInternal): void;
214
+ removeFromCacheByUri(uri: string): void;
215
+ checkCacheForUri(uri: string): boolean;
216
+ getSchema(report: Report, refOrSchema: string | JsonSchemaInternal): JsonSchemaInternal | undefined;
217
+ getSchemaByUri(report: Report, uri: string, root?: JsonSchemaInternal): JsonSchemaInternal | undefined;
218
+ getSchemaByReference(report: Report, schema: JsonSchemaInternal): JsonSchemaInternal;
219
+ }
220
+
221
+ declare class SchemaValidator {
222
+ private validator;
223
+ constructor(validator: ZSchema);
224
+ get options(): ZSchemaOptions;
225
+ validateArrayOfSchemas(report: Report, arr: JsonSchemaInternal[]): boolean;
226
+ validateSchema(report: Report, schema: JsonSchemaInternal | JsonSchemaInternal[]): boolean;
227
+ }
131
228
 
132
229
  interface ZSchemaOptions {
133
230
  asyncTimeout?: number;
134
231
  forceAdditional?: boolean;
135
- assumeAdditional?: boolean;
232
+ assumeAdditional?: boolean | string[];
233
+ enumCaseInsensitiveComparison?: boolean;
136
234
  forceItems?: boolean;
137
235
  forceMinItems?: boolean;
138
236
  forceMaxItems?: boolean;
@@ -156,74 +254,49 @@ interface ValidateOptions {
156
254
  schemaPath?: string;
157
255
  includeErrors?: Array<keyof typeof Errors>;
158
256
  }
159
- type ValidateCallback = (e: Error, valid: boolean) => void;
160
- type SchemaReader = (uri: string) => unknown;
257
+ type ValidateCallback = (e: Error | SchemaErrorDetail[] | null, valid: boolean) => void;
258
+ type SchemaReader = (uri: string) => JsonSchema;
161
259
  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
- */
260
+ static registerFormat(name: string, validatorFunction: FormatValidatorFn): void;
176
261
  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
262
  static getRegisteredFormats(): string[];
186
263
  static getDefaultOptions(): ZSchemaOptions;
187
- private cache;
188
- private referenceCache;
189
- private validateOptions;
264
+ lastReport: Report | undefined;
265
+ scache: SchemaCache;
266
+ sc: SchemaCompiler;
267
+ sv: SchemaValidator;
268
+ validateOptions: ValidateOptions;
190
269
  options: ZSchemaOptions;
191
270
  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;
271
+ validateSchema(schema: JsonSchemaInternal): boolean;
272
+ validate(json: unknown, schema: JsonSchema, options?: ValidateOptions, callback?: ValidateCallback): void;
273
+ validate(json: unknown, schema: JsonSchema, callback?: ValidateCallback): void;
274
+ validate(json: unknown, schema: JsonSchema): boolean;
207
275
  /**
208
276
  * Returns an Error object for the most recent failed validation, or null if the validation was successful.
209
277
  */
210
- getLastError(): SchemaError;
278
+ getLastError(): SchemaError | null;
211
279
  /**
212
280
  * Returns the error details for the most recent validation, or undefined if the validation was successful.
213
281
  * This is the same list as the SchemaError.details property.
214
282
  */
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;
283
+ getLastErrors(): SchemaErrorDetail[] | null;
284
+ setRemoteReference(uri: string, schema: string | JsonSchema, validationOptions: ZSchemaOptions): void;
285
+ compileSchema(schema: JsonSchema): boolean;
286
+ getMissingReferences(arr?: SchemaErrorDetail[]): string[];
287
+ getMissingRemoteReferences(): string[];
288
+ getResolvedSchema(schema: JsonSchemaInternal): JsonSchema;
221
289
  static schemaReader: SchemaReader;
222
- setSchemaReader(schemaReader: any): void;
290
+ setSchemaReader(schemaReader: SchemaReader): void;
223
291
  getSchemaReader(): SchemaReader;
224
- static setSchemaReader(schemaReader: any): void;
292
+ static setSchemaReader(schemaReader: SchemaReader): void;
225
293
  static schemaSymbol: symbol;
226
294
  static jsonSymbol: symbol;
227
295
  }
228
296
 
229
- export { ZSchema as default };
297
+ type JsonValidatorFn = (this: ZSchema, report: Report, schema: JsonSchemaInternal, json: unknown) => void;
298
+ declare const JsonValidators: Record<keyof JsonSchema, JsonValidatorFn>;
299
+ declare function validate(this: ZSchema, report: Report, schema: JsonSchemaInternal, json: unknown): boolean;
300
+
301
+ export { Errors, JsonValidators, Report, SchemaCache, SchemaCompiler, SchemaValidator, ZSchema, ZSchema as default, findId, getFormatValidators, getRegisteredFormats, getSupportedFormats, isFormatSupported, registerFormat, unregisterFormat, validate };
302
+ export type { ErrorCode, ErrorParam, FormatValidatorFn, FormatValidatorsOptions, JsonSchema, JsonSchemaInternal, JsonSchemaType, Reference, ReferenceSchemaCacheStorage, ReportOptions, SchemaCacheStorage, SchemaError, SchemaErrorDetail, ValidateCallback, ValidateOptions, ZSchemaInternalProperties, ZSchemaOptions };