z-schema 7.0.6 → 7.0.8
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.
- package/cjs/index.d.ts +12 -23
- package/cjs/index.js +27 -15
- package/dist/schema-cache.js +3 -0
- package/dist/types/index.d.ts +5 -9
- package/dist/types/json-validation.d.ts +1 -1
- package/dist/types/schema-cache.d.ts +4 -2
- package/dist/types/schema-compiler.d.ts +1 -1
- package/dist/types/z-schema.d.ts +8 -8
- package/dist/z-schema.js +24 -15
- package/package.json +1 -1
- package/src/index.ts +5 -9
- package/src/json-validation.ts +1 -1
- package/src/schema-cache.ts +8 -2
- package/src/schema-compiler.ts +1 -1
- package/src/z-schema.ts +30 -22
- package/umd/ZSchema.js +27 -15
- package/umd/ZSchema.min.js +1 -1
package/cjs/index.d.ts
CHANGED
|
@@ -54,7 +54,7 @@ interface Reference {
|
|
|
54
54
|
declare class SchemaCompiler {
|
|
55
55
|
private validator;
|
|
56
56
|
constructor(validator: ZSchema);
|
|
57
|
-
compileSchema(report: Report, schema: JsonSchemaInternal): boolean;
|
|
57
|
+
compileSchema(report: Report, schema: JsonSchemaInternal | JsonSchemaInternal[]): boolean;
|
|
58
58
|
compileArrayOfSchemas(report: Report, arr: JsonSchemaInternal[]): boolean;
|
|
59
59
|
compileArrayOfSchemasLoop(mainReport: Report, arr: JsonSchemaInternal[]): number;
|
|
60
60
|
}
|
|
@@ -107,7 +107,6 @@ interface ZSchemaInternalProperties {
|
|
|
107
107
|
}
|
|
108
108
|
interface JsonSchemaInternal extends JsonSchema, ZSchemaInternalProperties {
|
|
109
109
|
}
|
|
110
|
-
declare const findId: (schema: JsonSchemaInternal, id: string) => JsonSchemaInternal | undefined;
|
|
111
110
|
|
|
112
111
|
interface SchemaError extends Error {
|
|
113
112
|
/**
|
|
@@ -193,15 +192,6 @@ declare class Report {
|
|
|
193
192
|
}
|
|
194
193
|
|
|
195
194
|
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
195
|
|
|
206
196
|
type SchemaCacheStorage = Record<string, JsonSchemaInternal>;
|
|
207
197
|
type ReferenceSchemaCacheStorage = Array<[JsonSchemaInternal, JsonSchemaInternal]>;
|
|
@@ -213,7 +203,9 @@ declare class SchemaCache {
|
|
|
213
203
|
cacheSchemaByUri(uri: string, schema: JsonSchemaInternal): void;
|
|
214
204
|
removeFromCacheByUri(uri: string): void;
|
|
215
205
|
checkCacheForUri(uri: string): boolean;
|
|
216
|
-
getSchema(report: Report, refOrSchema: string
|
|
206
|
+
getSchema(report: Report, refOrSchema: string): JsonSchemaInternal | undefined;
|
|
207
|
+
getSchema(report: Report, refOrSchema: JsonSchema): JsonSchemaInternal;
|
|
208
|
+
getSchema(report: Report, refOrSchema: JsonSchema[]): JsonSchemaInternal[];
|
|
217
209
|
getSchemaByUri(report: Report, uri: string, root?: JsonSchemaInternal): JsonSchemaInternal | undefined;
|
|
218
210
|
getSchemaByReference(report: Report, schema: JsonSchemaInternal): JsonSchemaInternal;
|
|
219
211
|
}
|
|
@@ -268,10 +260,11 @@ declare class ZSchema {
|
|
|
268
260
|
validateOptions: ValidateOptions;
|
|
269
261
|
options: ZSchemaOptions;
|
|
270
262
|
constructor(options?: ZSchemaOptions);
|
|
271
|
-
validateSchema(
|
|
272
|
-
validate(json: unknown, schema: JsonSchema, options
|
|
273
|
-
validate(json: unknown, schema: JsonSchema, callback
|
|
274
|
-
validate(json: unknown, schema: JsonSchema): boolean;
|
|
263
|
+
validateSchema(schemaOrArr: JsonSchema | JsonSchema[]): boolean;
|
|
264
|
+
validate(json: unknown, schema: JsonSchema | string, options: ValidateOptions, callback: ValidateCallback): void;
|
|
265
|
+
validate(json: unknown, schema: JsonSchema | string, callback: ValidateCallback): void;
|
|
266
|
+
validate(json: unknown, schema: JsonSchema | string, options: ValidateOptions): boolean;
|
|
267
|
+
validate(json: unknown, schema: JsonSchema | string): boolean;
|
|
275
268
|
/**
|
|
276
269
|
* Returns an Error object for the most recent failed validation, or null if the validation was successful.
|
|
277
270
|
*/
|
|
@@ -285,7 +278,7 @@ declare class ZSchema {
|
|
|
285
278
|
compileSchema(schema: JsonSchema): boolean;
|
|
286
279
|
getMissingReferences(arr?: SchemaErrorDetail[]): string[];
|
|
287
280
|
getMissingRemoteReferences(): string[];
|
|
288
|
-
getResolvedSchema(schema:
|
|
281
|
+
getResolvedSchema(schema: JsonSchema): JsonSchema;
|
|
289
282
|
static schemaReader: SchemaReader;
|
|
290
283
|
setSchemaReader(schemaReader: SchemaReader): void;
|
|
291
284
|
getSchemaReader(): SchemaReader;
|
|
@@ -294,9 +287,5 @@ declare class ZSchema {
|
|
|
294
287
|
static jsonSymbol: symbol;
|
|
295
288
|
}
|
|
296
289
|
|
|
297
|
-
|
|
298
|
-
|
|
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 };
|
|
290
|
+
export { Errors, Report, ZSchema as default };
|
|
291
|
+
export type { ErrorCode, ErrorParam, FormatValidatorFn, JsonSchema, JsonSchemaType, SchemaError, SchemaErrorDetail, SchemaReader, ValidateCallback, ValidateOptions, ZSchemaOptions };
|
package/cjs/index.js
CHANGED
|
@@ -12027,6 +12027,9 @@ class SchemaCache {
|
|
|
12027
12027
|
return remotePath ? this.cache[remotePath] != null : false;
|
|
12028
12028
|
}
|
|
12029
12029
|
getSchema(report, refOrSchema) {
|
|
12030
|
+
if (Array.isArray(refOrSchema)) {
|
|
12031
|
+
return refOrSchema.map((i) => this.getSchema(report, i));
|
|
12032
|
+
}
|
|
12030
12033
|
if (typeof refOrSchema === 'string') {
|
|
12031
12034
|
// ref input
|
|
12032
12035
|
return this.getSchemaByUri(report, refOrSchema);
|
|
@@ -13511,15 +13514,24 @@ class ZSchema {
|
|
|
13511
13514
|
this.setRemoteReference('http://json-schema.org/draft-04/schema', Draft4Schema, metaschemaOptions);
|
|
13512
13515
|
this.setRemoteReference('http://json-schema.org/draft-04/hyper-schema', Draft4HyperSchema, metaschemaOptions);
|
|
13513
13516
|
}
|
|
13514
|
-
validateSchema(
|
|
13515
|
-
if (Array.isArray(
|
|
13517
|
+
validateSchema(schemaOrArr) {
|
|
13518
|
+
if (Array.isArray(schemaOrArr) && schemaOrArr.length === 0) {
|
|
13516
13519
|
throw new Error('.validateSchema was called with an empty array');
|
|
13517
13520
|
}
|
|
13518
13521
|
const report = new Report(this.options);
|
|
13519
|
-
|
|
13520
|
-
|
|
13521
|
-
|
|
13522
|
-
|
|
13522
|
+
if (Array.isArray(schemaOrArr)) {
|
|
13523
|
+
const arr = this.scache.getSchema(report, schemaOrArr);
|
|
13524
|
+
const compiled = this.sc.compileSchema(report, arr);
|
|
13525
|
+
if (compiled) {
|
|
13526
|
+
this.sv.validateSchema(report, arr);
|
|
13527
|
+
}
|
|
13528
|
+
}
|
|
13529
|
+
else {
|
|
13530
|
+
const schema = this.scache.getSchema(report, schemaOrArr);
|
|
13531
|
+
const compiled = this.sc.compileSchema(report, schema);
|
|
13532
|
+
if (compiled) {
|
|
13533
|
+
this.sv.validateSchema(report, schema);
|
|
13534
|
+
}
|
|
13523
13535
|
}
|
|
13524
13536
|
this.lastReport = report;
|
|
13525
13537
|
return report.isValid();
|
|
@@ -13547,20 +13559,20 @@ class ZSchema {
|
|
|
13547
13559
|
let foundError = false;
|
|
13548
13560
|
const report = new Report(this.options);
|
|
13549
13561
|
report.json = json;
|
|
13562
|
+
let _schema;
|
|
13550
13563
|
if (typeof schema === 'string') {
|
|
13551
13564
|
const schemaName = schema;
|
|
13552
|
-
|
|
13565
|
+
_schema = this.scache.getSchema(report, schemaName);
|
|
13553
13566
|
if (!_schema) {
|
|
13554
13567
|
throw new Error("Schema with id '" + schemaName + "' wasn't found in the validator cache!");
|
|
13555
13568
|
}
|
|
13556
|
-
schema = _schema;
|
|
13557
13569
|
}
|
|
13558
13570
|
else {
|
|
13559
|
-
|
|
13571
|
+
_schema = this.scache.getSchema(report, schema);
|
|
13560
13572
|
}
|
|
13561
13573
|
let compiled = false;
|
|
13562
13574
|
if (!foundError) {
|
|
13563
|
-
compiled = this.sc.compileSchema(report,
|
|
13575
|
+
compiled = this.sc.compileSchema(report, _schema);
|
|
13564
13576
|
}
|
|
13565
13577
|
if (!compiled) {
|
|
13566
13578
|
this.lastReport = report;
|
|
@@ -13568,21 +13580,21 @@ class ZSchema {
|
|
|
13568
13580
|
}
|
|
13569
13581
|
let validated = false;
|
|
13570
13582
|
if (!foundError) {
|
|
13571
|
-
validated = this.sv.validateSchema(report,
|
|
13583
|
+
validated = this.sv.validateSchema(report, _schema);
|
|
13572
13584
|
}
|
|
13573
13585
|
if (!validated) {
|
|
13574
13586
|
this.lastReport = report;
|
|
13575
13587
|
foundError = true;
|
|
13576
13588
|
}
|
|
13577
13589
|
if (options.schemaPath) {
|
|
13578
|
-
report.rootSchema =
|
|
13579
|
-
|
|
13580
|
-
if (!
|
|
13590
|
+
report.rootSchema = _schema;
|
|
13591
|
+
_schema = get(_schema, options.schemaPath);
|
|
13592
|
+
if (!_schema) {
|
|
13581
13593
|
throw new Error("Schema path '" + options.schemaPath + "' wasn't found in the schema!");
|
|
13582
13594
|
}
|
|
13583
13595
|
}
|
|
13584
13596
|
if (!foundError) {
|
|
13585
|
-
validate.call(this, report,
|
|
13597
|
+
validate.call(this, report, _schema, json);
|
|
13586
13598
|
}
|
|
13587
13599
|
if (callback) {
|
|
13588
13600
|
report.processAsyncTasks(this.options.asyncTimeout, callback);
|
package/dist/schema-cache.js
CHANGED
|
@@ -28,6 +28,9 @@ export class SchemaCache {
|
|
|
28
28
|
return remotePath ? this.cache[remotePath] != null : false;
|
|
29
29
|
}
|
|
30
30
|
getSchema(report, refOrSchema) {
|
|
31
|
+
if (Array.isArray(refOrSchema)) {
|
|
32
|
+
return refOrSchema.map((i) => this.getSchema(report, i));
|
|
33
|
+
}
|
|
31
34
|
if (typeof refOrSchema === 'string') {
|
|
32
35
|
// ref input
|
|
33
36
|
return this.getSchemaByUri(report, refOrSchema);
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
import { ZSchema } from './z-schema.js';
|
|
2
|
-
export type
|
|
3
|
-
export type
|
|
4
|
-
export type
|
|
5
|
-
export type
|
|
6
|
-
export type
|
|
7
|
-
export type * from './schema-cache.js';
|
|
8
|
-
export type * from './schema-compiler.js';
|
|
9
|
-
export type * from './schema-validator.js';
|
|
10
|
-
export type * from './z-schema.js';
|
|
2
|
+
export type { FormatValidatorFn } from './format-validators.js';
|
|
3
|
+
export type { ErrorCode, ErrorParam, Errors } from './errors.js';
|
|
4
|
+
export type { JsonSchema, JsonSchemaType } from './json-schema.js';
|
|
5
|
+
export type { Report, SchemaError, SchemaErrorDetail } from './report.js';
|
|
6
|
+
export type { ZSchemaOptions, SchemaReader, ValidateOptions, ValidateCallback } from './z-schema.js';
|
|
11
7
|
export default ZSchema;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Report } from './report.js';
|
|
2
2
|
import { JsonSchema, JsonSchemaInternal } from './json-schema.js';
|
|
3
3
|
import type { ZSchema } from './z-schema.js';
|
|
4
|
-
type JsonValidatorFn = (this: ZSchema, report: Report, schema:
|
|
4
|
+
type JsonValidatorFn = (this: ZSchema, report: Report, schema: JsonSchema, json: unknown) => void;
|
|
5
5
|
export declare const JsonValidators: Record<keyof JsonSchema, JsonValidatorFn>;
|
|
6
6
|
export declare function validate(this: ZSchema, report: Report, schema: JsonSchemaInternal, json: unknown): boolean;
|
|
7
7
|
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ZSchema } from './z-schema.js';
|
|
2
2
|
import { Report } from './report.js';
|
|
3
|
-
import { JsonSchemaInternal } from './json-schema.js';
|
|
3
|
+
import { JsonSchema, JsonSchemaInternal } from './json-schema.js';
|
|
4
4
|
export type SchemaCacheStorage = Record<string, JsonSchemaInternal>;
|
|
5
5
|
export type ReferenceSchemaCacheStorage = Array<[JsonSchemaInternal, JsonSchemaInternal]>;
|
|
6
6
|
export declare class SchemaCache {
|
|
@@ -11,7 +11,9 @@ export declare class SchemaCache {
|
|
|
11
11
|
cacheSchemaByUri(uri: string, schema: JsonSchemaInternal): void;
|
|
12
12
|
removeFromCacheByUri(uri: string): void;
|
|
13
13
|
checkCacheForUri(uri: string): boolean;
|
|
14
|
-
getSchema(report: Report, refOrSchema: string
|
|
14
|
+
getSchema(report: Report, refOrSchema: string): JsonSchemaInternal | undefined;
|
|
15
|
+
getSchema(report: Report, refOrSchema: JsonSchema): JsonSchemaInternal;
|
|
16
|
+
getSchema(report: Report, refOrSchema: JsonSchema[]): JsonSchemaInternal[];
|
|
15
17
|
getSchemaByUri(report: Report, uri: string, root?: JsonSchemaInternal): JsonSchemaInternal | undefined;
|
|
16
18
|
getSchemaByReference(report: Report, schema: JsonSchemaInternal): JsonSchemaInternal;
|
|
17
19
|
}
|
|
@@ -10,7 +10,7 @@ export interface Reference {
|
|
|
10
10
|
export declare class SchemaCompiler {
|
|
11
11
|
private validator;
|
|
12
12
|
constructor(validator: ZSchema);
|
|
13
|
-
compileSchema(report: Report, schema: JsonSchemaInternal): boolean;
|
|
13
|
+
compileSchema(report: Report, schema: JsonSchemaInternal | JsonSchemaInternal[]): boolean;
|
|
14
14
|
compileArrayOfSchemas(report: Report, arr: JsonSchemaInternal[]): boolean;
|
|
15
15
|
compileArrayOfSchemasLoop(mainReport: Report, arr: JsonSchemaInternal[]): number;
|
|
16
16
|
}
|
package/dist/types/z-schema.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { SchemaCache } from './schema-cache.js';
|
|
|
4
4
|
import { SchemaCompiler } from './schema-compiler.js';
|
|
5
5
|
import { SchemaValidator } from './schema-validator.js';
|
|
6
6
|
import type { Errors } from './errors.js';
|
|
7
|
-
import type { JsonSchema
|
|
7
|
+
import type { JsonSchema } from './json-schema.js';
|
|
8
8
|
export interface ZSchemaOptions {
|
|
9
9
|
asyncTimeout?: number;
|
|
10
10
|
forceAdditional?: boolean;
|
|
@@ -34,7 +34,7 @@ export interface ValidateOptions {
|
|
|
34
34
|
includeErrors?: Array<keyof typeof Errors>;
|
|
35
35
|
}
|
|
36
36
|
export type ValidateCallback = (e: Error | SchemaErrorDetail[] | null, valid: boolean) => void;
|
|
37
|
-
type SchemaReader = (uri: string) => JsonSchema;
|
|
37
|
+
export type SchemaReader = (uri: string) => JsonSchema;
|
|
38
38
|
export declare class ZSchema {
|
|
39
39
|
static registerFormat(name: string, validatorFunction: FormatValidatorFn): void;
|
|
40
40
|
static unregisterFormat(name: string): void;
|
|
@@ -47,10 +47,11 @@ export declare class ZSchema {
|
|
|
47
47
|
validateOptions: ValidateOptions;
|
|
48
48
|
options: ZSchemaOptions;
|
|
49
49
|
constructor(options?: ZSchemaOptions);
|
|
50
|
-
validateSchema(
|
|
51
|
-
validate(json: unknown, schema: JsonSchema, options
|
|
52
|
-
validate(json: unknown, schema: JsonSchema, callback
|
|
53
|
-
validate(json: unknown, schema: JsonSchema): boolean;
|
|
50
|
+
validateSchema(schemaOrArr: JsonSchema | JsonSchema[]): boolean;
|
|
51
|
+
validate(json: unknown, schema: JsonSchema | string, options: ValidateOptions, callback: ValidateCallback): void;
|
|
52
|
+
validate(json: unknown, schema: JsonSchema | string, callback: ValidateCallback): void;
|
|
53
|
+
validate(json: unknown, schema: JsonSchema | string, options: ValidateOptions): boolean;
|
|
54
|
+
validate(json: unknown, schema: JsonSchema | string): boolean;
|
|
54
55
|
/**
|
|
55
56
|
* Returns an Error object for the most recent failed validation, or null if the validation was successful.
|
|
56
57
|
*/
|
|
@@ -64,7 +65,7 @@ export declare class ZSchema {
|
|
|
64
65
|
compileSchema(schema: JsonSchema): boolean;
|
|
65
66
|
getMissingReferences(arr?: SchemaErrorDetail[]): string[];
|
|
66
67
|
getMissingRemoteReferences(): string[];
|
|
67
|
-
getResolvedSchema(schema:
|
|
68
|
+
getResolvedSchema(schema: JsonSchema): JsonSchema;
|
|
68
69
|
static schemaReader: SchemaReader;
|
|
69
70
|
setSchemaReader(schemaReader: SchemaReader): void;
|
|
70
71
|
getSchemaReader(): SchemaReader;
|
|
@@ -72,4 +73,3 @@ export declare class ZSchema {
|
|
|
72
73
|
static schemaSymbol: symbol;
|
|
73
74
|
static jsonSymbol: symbol;
|
|
74
75
|
}
|
|
75
|
-
export {};
|
package/dist/z-schema.js
CHANGED
|
@@ -131,15 +131,24 @@ export class ZSchema {
|
|
|
131
131
|
this.setRemoteReference('http://json-schema.org/draft-04/schema', Draft4Schema, metaschemaOptions);
|
|
132
132
|
this.setRemoteReference('http://json-schema.org/draft-04/hyper-schema', Draft4HyperSchema, metaschemaOptions);
|
|
133
133
|
}
|
|
134
|
-
validateSchema(
|
|
135
|
-
if (Array.isArray(
|
|
134
|
+
validateSchema(schemaOrArr) {
|
|
135
|
+
if (Array.isArray(schemaOrArr) && schemaOrArr.length === 0) {
|
|
136
136
|
throw new Error('.validateSchema was called with an empty array');
|
|
137
137
|
}
|
|
138
138
|
const report = new Report(this.options);
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
139
|
+
if (Array.isArray(schemaOrArr)) {
|
|
140
|
+
const arr = this.scache.getSchema(report, schemaOrArr);
|
|
141
|
+
const compiled = this.sc.compileSchema(report, arr);
|
|
142
|
+
if (compiled) {
|
|
143
|
+
this.sv.validateSchema(report, arr);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
const schema = this.scache.getSchema(report, schemaOrArr);
|
|
148
|
+
const compiled = this.sc.compileSchema(report, schema);
|
|
149
|
+
if (compiled) {
|
|
150
|
+
this.sv.validateSchema(report, schema);
|
|
151
|
+
}
|
|
143
152
|
}
|
|
144
153
|
this.lastReport = report;
|
|
145
154
|
return report.isValid();
|
|
@@ -167,20 +176,20 @@ export class ZSchema {
|
|
|
167
176
|
let foundError = false;
|
|
168
177
|
const report = new Report(this.options);
|
|
169
178
|
report.json = json;
|
|
179
|
+
let _schema;
|
|
170
180
|
if (typeof schema === 'string') {
|
|
171
181
|
const schemaName = schema;
|
|
172
|
-
|
|
182
|
+
_schema = this.scache.getSchema(report, schemaName);
|
|
173
183
|
if (!_schema) {
|
|
174
184
|
throw new Error("Schema with id '" + schemaName + "' wasn't found in the validator cache!");
|
|
175
185
|
}
|
|
176
|
-
schema = _schema;
|
|
177
186
|
}
|
|
178
187
|
else {
|
|
179
|
-
|
|
188
|
+
_schema = this.scache.getSchema(report, schema);
|
|
180
189
|
}
|
|
181
190
|
let compiled = false;
|
|
182
191
|
if (!foundError) {
|
|
183
|
-
compiled = this.sc.compileSchema(report,
|
|
192
|
+
compiled = this.sc.compileSchema(report, _schema);
|
|
184
193
|
}
|
|
185
194
|
if (!compiled) {
|
|
186
195
|
this.lastReport = report;
|
|
@@ -188,21 +197,21 @@ export class ZSchema {
|
|
|
188
197
|
}
|
|
189
198
|
let validated = false;
|
|
190
199
|
if (!foundError) {
|
|
191
|
-
validated = this.sv.validateSchema(report,
|
|
200
|
+
validated = this.sv.validateSchema(report, _schema);
|
|
192
201
|
}
|
|
193
202
|
if (!validated) {
|
|
194
203
|
this.lastReport = report;
|
|
195
204
|
foundError = true;
|
|
196
205
|
}
|
|
197
206
|
if (options.schemaPath) {
|
|
198
|
-
report.rootSchema =
|
|
199
|
-
|
|
200
|
-
if (!
|
|
207
|
+
report.rootSchema = _schema;
|
|
208
|
+
_schema = get(_schema, options.schemaPath);
|
|
209
|
+
if (!_schema) {
|
|
201
210
|
throw new Error("Schema path '" + options.schemaPath + "' wasn't found in the schema!");
|
|
202
211
|
}
|
|
203
212
|
}
|
|
204
213
|
if (!foundError) {
|
|
205
|
-
validateJson.call(this, report,
|
|
214
|
+
validateJson.call(this, report, _schema, json);
|
|
206
215
|
}
|
|
207
216
|
if (callback) {
|
|
208
217
|
report.processAsyncTasks(this.options.asyncTimeout, callback);
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
import { ZSchema } from './z-schema.js';
|
|
2
2
|
|
|
3
3
|
// Export types and interfaces from relevant files
|
|
4
|
-
export type
|
|
5
|
-
export type
|
|
6
|
-
export type
|
|
7
|
-
export type
|
|
8
|
-
export type
|
|
9
|
-
export type * from './schema-cache.js';
|
|
10
|
-
export type * from './schema-compiler.js';
|
|
11
|
-
export type * from './schema-validator.js';
|
|
12
|
-
export type * from './z-schema.js';
|
|
4
|
+
export type { FormatValidatorFn } from './format-validators.js';
|
|
5
|
+
export type { ErrorCode, ErrorParam, Errors } from './errors.js';
|
|
6
|
+
export type { JsonSchema, JsonSchemaType } from './json-schema.js';
|
|
7
|
+
export type { Report, SchemaError, SchemaErrorDetail } from './report.js';
|
|
8
|
+
export type { ZSchemaOptions, SchemaReader, ValidateOptions, ValidateCallback } from './z-schema.js';
|
|
13
9
|
|
|
14
10
|
export default ZSchema;
|
package/src/json-validation.ts
CHANGED
|
@@ -19,7 +19,7 @@ const shouldSkipValidate = function (options: ValidateOptions, errors: any) {
|
|
|
19
19
|
);
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
-
type JsonValidatorFn = (this: ZSchema, report: Report, schema:
|
|
22
|
+
type JsonValidatorFn = (this: ZSchema, report: Report, schema: JsonSchema, json: unknown) => void;
|
|
23
23
|
|
|
24
24
|
export const JsonValidators: Record<keyof JsonSchema, JsonValidatorFn> = {
|
|
25
25
|
id: () => {},
|
package/src/schema-cache.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ZSchema } from './z-schema.js';
|
|
2
2
|
import isequal from 'lodash.isequal';
|
|
3
3
|
import { Report } from './report.js';
|
|
4
|
-
import { findId, JsonSchemaInternal } from './json-schema.js';
|
|
4
|
+
import { findId, JsonSchema, JsonSchemaInternal } from './json-schema.js';
|
|
5
5
|
import { getQueryPath, getRemotePath } from './utils/uri.js';
|
|
6
6
|
import { deepClone } from './utils/clone.js';
|
|
7
7
|
import { decodeJSONPointer } from './utils/json.js';
|
|
@@ -34,7 +34,13 @@ export class SchemaCache {
|
|
|
34
34
|
return remotePath ? this.cache[remotePath] != null : false;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
getSchema(report: Report, refOrSchema: string |
|
|
37
|
+
getSchema(report: Report, refOrSchema: string): JsonSchemaInternal | undefined;
|
|
38
|
+
getSchema(report: Report, refOrSchema: JsonSchema): JsonSchemaInternal;
|
|
39
|
+
getSchema(report: Report, refOrSchema: JsonSchema[]): JsonSchemaInternal[];
|
|
40
|
+
getSchema(report: Report, refOrSchema: string | JsonSchema | JsonSchema[]) {
|
|
41
|
+
if (Array.isArray(refOrSchema)) {
|
|
42
|
+
return refOrSchema.map((i) => this.getSchema(report, i));
|
|
43
|
+
}
|
|
38
44
|
if (typeof refOrSchema === 'string') {
|
|
39
45
|
// ref input
|
|
40
46
|
return this.getSchemaByUri(report, refOrSchema);
|
package/src/schema-compiler.ts
CHANGED
|
@@ -107,7 +107,7 @@ const mergeReference = (scope: string[], ref: string) => {
|
|
|
107
107
|
export class SchemaCompiler {
|
|
108
108
|
constructor(private validator: ZSchema) {}
|
|
109
109
|
|
|
110
|
-
compileSchema(report: Report, schema: JsonSchemaInternal) {
|
|
110
|
+
compileSchema(report: Report, schema: JsonSchemaInternal | JsonSchemaInternal[]) {
|
|
111
111
|
report.commonErrorMessage = 'SCHEMA_COMPILATION_FAILED';
|
|
112
112
|
|
|
113
113
|
// if schema is a string, assume it's a uri
|
package/src/z-schema.ts
CHANGED
|
@@ -147,7 +147,7 @@ export interface ValidateOptions {
|
|
|
147
147
|
export type ValidateCallback = (e: Error | SchemaErrorDetail[] | null, valid: boolean) => void;
|
|
148
148
|
|
|
149
149
|
// a sync function that loads schemas for future use, for example from schemas directory, during server startup
|
|
150
|
-
type SchemaReader = (uri: string) => JsonSchema;
|
|
150
|
+
export type SchemaReader = (uri: string) => JsonSchema;
|
|
151
151
|
|
|
152
152
|
export class ZSchema {
|
|
153
153
|
public static registerFormat(name: string, validatorFunction: FormatValidatorFn): void {
|
|
@@ -186,30 +186,38 @@ export class ZSchema {
|
|
|
186
186
|
this.setRemoteReference('http://json-schema.org/draft-04/hyper-schema', Draft4HyperSchema, metaschemaOptions);
|
|
187
187
|
}
|
|
188
188
|
|
|
189
|
-
validateSchema(
|
|
190
|
-
if (Array.isArray(
|
|
189
|
+
validateSchema(schemaOrArr: JsonSchema | JsonSchema[]): boolean {
|
|
190
|
+
if (Array.isArray(schemaOrArr) && schemaOrArr.length === 0) {
|
|
191
191
|
throw new Error('.validateSchema was called with an empty array');
|
|
192
192
|
}
|
|
193
193
|
|
|
194
194
|
const report = new Report(this.options);
|
|
195
195
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
196
|
+
if (Array.isArray(schemaOrArr)) {
|
|
197
|
+
const arr = this.scache.getSchema(report, schemaOrArr)!;
|
|
198
|
+
const compiled = this.sc.compileSchema(report, arr);
|
|
199
|
+
if (compiled) {
|
|
200
|
+
this.sv.validateSchema(report, arr);
|
|
201
|
+
}
|
|
202
|
+
} else {
|
|
203
|
+
const schema = this.scache.getSchema(report, schemaOrArr)!;
|
|
204
|
+
const compiled = this.sc.compileSchema(report, schema);
|
|
205
|
+
if (compiled) {
|
|
206
|
+
this.sv.validateSchema(report, schema);
|
|
207
|
+
}
|
|
201
208
|
}
|
|
202
209
|
|
|
203
210
|
this.lastReport = report;
|
|
204
211
|
return report.isValid();
|
|
205
212
|
}
|
|
206
213
|
|
|
207
|
-
validate(json: unknown, schema: JsonSchema, options
|
|
208
|
-
validate(json: unknown, schema: JsonSchema, callback
|
|
209
|
-
validate(json: unknown, schema: JsonSchema): boolean;
|
|
214
|
+
validate(json: unknown, schema: JsonSchema | string, options: ValidateOptions, callback: ValidateCallback): void;
|
|
215
|
+
validate(json: unknown, schema: JsonSchema | string, callback: ValidateCallback): void;
|
|
216
|
+
validate(json: unknown, schema: JsonSchema | string, options: ValidateOptions): boolean;
|
|
217
|
+
validate(json: unknown, schema: JsonSchema | string): boolean;
|
|
210
218
|
validate(
|
|
211
219
|
json: unknown,
|
|
212
|
-
schema: JsonSchema,
|
|
220
|
+
schema: JsonSchema | string,
|
|
213
221
|
options?: ValidateOptions | ValidateCallback,
|
|
214
222
|
callback?: ValidateCallback
|
|
215
223
|
): boolean | void {
|
|
@@ -241,20 +249,20 @@ export class ZSchema {
|
|
|
241
249
|
const report = new Report(this.options);
|
|
242
250
|
report.json = json;
|
|
243
251
|
|
|
252
|
+
let _schema: JsonSchemaInternal;
|
|
244
253
|
if (typeof schema === 'string') {
|
|
245
254
|
const schemaName = schema;
|
|
246
|
-
|
|
255
|
+
_schema = this.scache.getSchema(report, schemaName)!;
|
|
247
256
|
if (!_schema) {
|
|
248
257
|
throw new Error("Schema with id '" + schemaName + "' wasn't found in the validator cache!");
|
|
249
258
|
}
|
|
250
|
-
schema = _schema;
|
|
251
259
|
} else {
|
|
252
|
-
|
|
260
|
+
_schema = this.scache.getSchema(report, schema)!;
|
|
253
261
|
}
|
|
254
262
|
|
|
255
263
|
let compiled = false;
|
|
256
264
|
if (!foundError) {
|
|
257
|
-
compiled = this.sc.compileSchema(report,
|
|
265
|
+
compiled = this.sc.compileSchema(report, _schema);
|
|
258
266
|
}
|
|
259
267
|
if (!compiled) {
|
|
260
268
|
this.lastReport = report;
|
|
@@ -263,7 +271,7 @@ export class ZSchema {
|
|
|
263
271
|
|
|
264
272
|
let validated = false;
|
|
265
273
|
if (!foundError) {
|
|
266
|
-
validated = this.sv.validateSchema(report,
|
|
274
|
+
validated = this.sv.validateSchema(report, _schema);
|
|
267
275
|
}
|
|
268
276
|
if (!validated) {
|
|
269
277
|
this.lastReport = report;
|
|
@@ -271,15 +279,15 @@ export class ZSchema {
|
|
|
271
279
|
}
|
|
272
280
|
|
|
273
281
|
if (options.schemaPath) {
|
|
274
|
-
report.rootSchema =
|
|
275
|
-
|
|
276
|
-
if (!
|
|
282
|
+
report.rootSchema = _schema;
|
|
283
|
+
_schema = get(_schema, options.schemaPath);
|
|
284
|
+
if (!_schema) {
|
|
277
285
|
throw new Error("Schema path '" + options.schemaPath + "' wasn't found in the schema!");
|
|
278
286
|
}
|
|
279
287
|
}
|
|
280
288
|
|
|
281
289
|
if (!foundError) {
|
|
282
|
-
validateJson.call(this, report,
|
|
290
|
+
validateJson.call(this, report, _schema, json);
|
|
283
291
|
}
|
|
284
292
|
|
|
285
293
|
if (callback) {
|
|
@@ -380,7 +388,7 @@ export class ZSchema {
|
|
|
380
388
|
return missingRemoteReferences;
|
|
381
389
|
}
|
|
382
390
|
|
|
383
|
-
getResolvedSchema(schema:
|
|
391
|
+
getResolvedSchema(schema: JsonSchema): JsonSchema {
|
|
384
392
|
const report = new Report(this.options);
|
|
385
393
|
schema = this.scache.getSchema(report, schema)!;
|
|
386
394
|
|