z-schema 12.1.1 → 12.3.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.
- package/README.md +2 -2
- package/bin/z-schema +4 -5
- package/cjs/{index.js → index.cjs} +696 -687
- package/cjs/{index.d.ts → index.d.cts} +47 -26
- package/dist/{errors.d.mts → errors.d.ts} +2 -2
- package/dist/{errors.mjs → errors.js} +1 -2
- package/dist/{format-validators.mjs → format-validators.js} +43 -36
- package/dist/{index.d.mts → index.d.ts} +9 -9
- package/dist/{index.mjs → index.js} +3 -3
- package/dist/{json-schema-versions.d.mts → json-schema-versions.d.ts} +34 -3
- package/dist/{json-schema.d.mts → json-schema.d.ts} +7 -7
- package/dist/{json-schema.mjs → json-schema.js} +7 -12
- package/dist/{json-validation.mjs → json-validation.js} +143 -127
- package/dist/{report.d.mts → report.d.ts} +7 -8
- package/dist/{report.mjs → report.js} +28 -31
- package/dist/{schema-cache.d.mts → schema-cache.d.ts} +4 -4
- package/dist/{schema-cache.mjs → schema-cache.js} +10 -11
- package/dist/{schema-compiler.d.mts → schema-compiler.d.ts} +4 -4
- package/dist/{schema-compiler.mjs → schema-compiler.js} +95 -77
- package/dist/{schema-validator.d.mts → schema-validator.d.ts} +5 -5
- package/dist/{schema-validator.mjs → schema-validator.js} +138 -166
- package/dist/utils/{array.mjs → array.js} +4 -3
- package/dist/utils/{base64.mjs → base64.js} +3 -2
- package/dist/utils/{clone.mjs → clone.js} +18 -20
- package/dist/utils/{hostname.mjs → hostname.js} +19 -22
- package/dist/utils/{json.mjs → json.js} +11 -7
- package/dist/utils/{schema-regex.mjs → schema-regex.js} +5 -5
- package/dist/utils/{time.mjs → time.js} +5 -5
- package/dist/utils/unicode.js +22 -0
- package/dist/utils/{what-is.mjs → what-is.js} +1 -2
- package/dist/validation/{array.mjs → array.js} +18 -20
- package/dist/validation/{combinators.mjs → combinators.js} +16 -16
- package/dist/validation/{numeric.mjs → numeric.js} +11 -11
- package/dist/validation/{object.mjs → object.js} +35 -34
- package/dist/validation/{ref.mjs → ref.js} +4 -4
- package/dist/validation/{shared.mjs → shared.js} +12 -11
- package/dist/validation/{string.mjs → string.js} +32 -32
- package/dist/validation/type.js +34 -0
- package/dist/{z-schema-base.d.mts → z-schema-base.d.ts} +11 -12
- package/dist/{z-schema-base.mjs → z-schema-base.js} +45 -40
- package/dist/{z-schema-options.d.mts → z-schema-options.d.ts} +3 -3
- package/dist/{z-schema-options.mjs → z-schema-options.js} +4 -4
- package/dist/{z-schema-reader.d.mts → z-schema-reader.d.ts} +1 -1
- package/dist/{z-schema-versions.mjs → z-schema-versions.js} +21 -21
- package/dist/{z-schema.d.mts → z-schema.d.ts} +5 -13
- package/dist/{z-schema.mjs → z-schema.js} +37 -47
- package/package.json +25 -23
- package/src/errors.ts +1 -2
- package/src/format-validators.ts +139 -59
- package/src/json-schema-versions.ts +56 -2
- package/src/json-schema.ts +10 -9
- package/src/json-validation.ts +189 -146
- package/src/report.ts +37 -49
- package/src/schema-cache.ts +13 -13
- package/src/schema-compiler.ts +170 -117
- package/src/schema-validator.ts +239 -238
- package/src/utils/array.ts +9 -6
- package/src/utils/base64.ts +13 -2
- package/src/utils/clone.ts +28 -30
- package/src/utils/date.ts +6 -3
- package/src/utils/hostname.ts +27 -27
- package/src/utils/json.ts +16 -9
- package/src/utils/properties.ts +2 -2
- package/src/utils/schema-regex.ts +4 -4
- package/src/utils/time.ts +5 -5
- package/src/utils/unicode.ts +12 -5
- package/src/utils/what-is.ts +1 -5
- package/src/validation/array.ts +24 -22
- package/src/validation/combinators.ts +14 -14
- package/src/validation/numeric.ts +14 -28
- package/src/validation/object.ts +32 -36
- package/src/validation/ref.ts +5 -6
- package/src/validation/shared.ts +22 -21
- package/src/validation/string.ts +29 -39
- package/src/validation/type.ts +17 -17
- package/src/z-schema-base.ts +49 -38
- package/src/z-schema-options.ts +4 -3
- package/src/z-schema.ts +35 -45
- package/umd/ZSchema.js +723 -697
- package/umd/ZSchema.min.js +2 -2
- package/umd/package.json +3 -0
- package/dist/utils/unicode.mjs +0 -12
- package/dist/validation/type.mjs +0 -32
- /package/dist/{format-validators.d.mts → format-validators.d.ts} +0 -0
- /package/dist/{json-schema-versions.mjs → json-schema-versions.js} +0 -0
- /package/dist/schemas/{draft-04-schema.mjs → draft-04-schema.js} +0 -0
- /package/dist/schemas/{draft-06-schema.mjs → draft-06-schema.js} +0 -0
- /package/dist/schemas/{draft-07-schema.mjs → draft-07-schema.js} +0 -0
- /package/dist/schemas/{draft-2019-09-meta-applicator.mjs → draft-2019-09-meta-applicator.js} +0 -0
- /package/dist/schemas/{draft-2019-09-meta-content.mjs → draft-2019-09-meta-content.js} +0 -0
- /package/dist/schemas/{draft-2019-09-meta-core.mjs → draft-2019-09-meta-core.js} +0 -0
- /package/dist/schemas/{draft-2019-09-meta-format.mjs → draft-2019-09-meta-format.js} +0 -0
- /package/dist/schemas/{draft-2019-09-meta-meta-data.mjs → draft-2019-09-meta-meta-data.js} +0 -0
- /package/dist/schemas/{draft-2019-09-meta-validation.mjs → draft-2019-09-meta-validation.js} +0 -0
- /package/dist/schemas/{draft-2019-09-schema.mjs → draft-2019-09-schema.js} +0 -0
- /package/dist/schemas/{draft-2020-12-meta-applicator.mjs → draft-2020-12-meta-applicator.js} +0 -0
- /package/dist/schemas/{draft-2020-12-meta-content.mjs → draft-2020-12-meta-content.js} +0 -0
- /package/dist/schemas/{draft-2020-12-meta-core.mjs → draft-2020-12-meta-core.js} +0 -0
- /package/dist/schemas/{draft-2020-12-meta-format-annotation.mjs → draft-2020-12-meta-format-annotation.js} +0 -0
- /package/dist/schemas/{draft-2020-12-meta-format-assertion.mjs → draft-2020-12-meta-format-assertion.js} +0 -0
- /package/dist/schemas/{draft-2020-12-meta-meta-data.mjs → draft-2020-12-meta-meta-data.js} +0 -0
- /package/dist/schemas/{draft-2020-12-meta-unevaluated.mjs → draft-2020-12-meta-unevaluated.js} +0 -0
- /package/dist/schemas/{draft-2020-12-meta-validation.mjs → draft-2020-12-meta-validation.js} +0 -0
- /package/dist/schemas/{draft-2020-12-schema.mjs → draft-2020-12-schema.js} +0 -0
- /package/dist/utils/{constants.mjs → constants.js} +0 -0
- /package/dist/utils/{date.mjs → date.js} +0 -0
- /package/dist/utils/{properties.mjs → properties.js} +0 -0
- /package/dist/utils/{symbols.mjs → symbols.js} +0 -0
- /package/dist/utils/{uri.mjs → uri.js} +0 -0
- /package/dist/{z-schema-reader.mjs → z-schema-reader.js} +0 -0
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import { whatIs } from "../utils/what-is.
|
|
2
|
-
import {
|
|
3
|
-
import { compileSchemaRegex } from "../utils/schema-regex.
|
|
4
|
-
import { isFormatAssertionVocabEnabled, shouldSkipValidate } from "./shared.
|
|
5
|
-
import { decodeBase64, isValidBase64 } from "../utils/base64.
|
|
6
|
-
import { unicodeLength } from "../utils/unicode.
|
|
1
|
+
import { whatIs } from "../utils/what-is.js";
|
|
2
|
+
import { resolveFormatValidator } from "../format-validators.js";
|
|
3
|
+
import { compileSchemaRegex } from "../utils/schema-regex.js";
|
|
4
|
+
import { isFormatAssertionVocabEnabled, shouldSkipValidate } from "./shared.js";
|
|
5
|
+
import { decodeBase64, isValidBase64 } from "../utils/base64.js";
|
|
6
|
+
import { unicodeLength } from "../utils/unicode.js";
|
|
7
7
|
//#region src/validation/string.ts
|
|
8
|
-
function minLengthValidator(report, schema, json) {
|
|
9
|
-
if (shouldSkipValidate(
|
|
8
|
+
function minLengthValidator(ctx, report, schema, json) {
|
|
9
|
+
if (shouldSkipValidate(ctx.validateOptions, ["MIN_LENGTH"])) return;
|
|
10
10
|
if (typeof json !== "string") return;
|
|
11
11
|
if (unicodeLength(json) < schema.minLength) report.addError("MIN_LENGTH", [json.length, schema.minLength], void 0, schema, "minLength");
|
|
12
12
|
}
|
|
13
|
-
function maxLengthValidator(report, schema, json) {
|
|
14
|
-
if (shouldSkipValidate(
|
|
13
|
+
function maxLengthValidator(ctx, report, schema, json) {
|
|
14
|
+
if (shouldSkipValidate(ctx.validateOptions, ["MAX_LENGTH"])) return;
|
|
15
15
|
if (typeof json !== "string") return;
|
|
16
16
|
if (unicodeLength(json) > schema.maxLength) report.addError("MAX_LENGTH", [json.length, schema.maxLength], void 0, schema, "maxLength");
|
|
17
17
|
}
|
|
18
|
-
function patternValidator(report, schema, json) {
|
|
19
|
-
if (shouldSkipValidate(
|
|
18
|
+
function patternValidator(ctx, report, schema, json) {
|
|
19
|
+
if (shouldSkipValidate(ctx.validateOptions, ["PATTERN"])) return;
|
|
20
20
|
if (typeof json !== "string") return;
|
|
21
21
|
const result = compileSchemaRegex(schema.pattern);
|
|
22
22
|
if (!result.ok) {
|
|
@@ -29,46 +29,46 @@ function patternValidator(report, schema, json) {
|
|
|
29
29
|
}
|
|
30
30
|
if (!result.value.test(json)) report.addError("PATTERN", [schema.pattern, json], void 0, schema, "pattern");
|
|
31
31
|
}
|
|
32
|
-
function formatValidator(report, schema, json) {
|
|
33
|
-
if (
|
|
34
|
-
if (
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const isModernDraft = this.options.version === "draft2019-09" || this.options.version === "draft2020-12";
|
|
38
|
-
const formatValidatorFn = getFormatValidators(this.options)[schema.format];
|
|
32
|
+
function formatValidator(ctx, report, schema, json) {
|
|
33
|
+
if (ctx.options.formatAssertions === false) return;
|
|
34
|
+
if (ctx.options.formatAssertions === true && !isFormatAssertionVocabEnabled(schema, report, ctx.options.version)) return;
|
|
35
|
+
const isModernDraft = ctx.options.version === "draft2019-09" || ctx.options.version === "draft2020-12";
|
|
36
|
+
const formatValidatorFn = resolveFormatValidator(schema.format, ctx.options);
|
|
39
37
|
if (typeof formatValidatorFn === "function") {
|
|
40
|
-
if (shouldSkipValidate(
|
|
38
|
+
if (shouldSkipValidate(ctx.validateOptions, ["INVALID_FORMAT"])) return;
|
|
41
39
|
if (report.hasError("INVALID_TYPE", [schema.type, whatIs(json)])) return;
|
|
42
|
-
if (formatValidatorFn.length === 2) report.addAsyncTaskWithPath(formatValidatorFn, [json],
|
|
40
|
+
if (formatValidatorFn.length === 2) report.addAsyncTaskWithPath(formatValidatorFn, [json], (result) => {
|
|
43
41
|
if (result !== true) report.addError("INVALID_FORMAT", [schema.format, JSON.stringify(json)], void 0, schema, "format");
|
|
44
42
|
});
|
|
45
43
|
else {
|
|
46
|
-
const result = formatValidatorFn.call(
|
|
44
|
+
const result = formatValidatorFn.call(ctx, json);
|
|
47
45
|
if (result instanceof Promise) {
|
|
48
46
|
const promiseResult = result;
|
|
49
47
|
report.addAsyncTaskWithPath(async (callback) => {
|
|
50
48
|
try {
|
|
51
49
|
callback(await promiseResult);
|
|
52
|
-
} catch
|
|
50
|
+
} catch {
|
|
53
51
|
callback(false);
|
|
54
52
|
}
|
|
55
|
-
}, [],
|
|
53
|
+
}, [], (resolvedResult) => {
|
|
56
54
|
if (resolvedResult !== true) report.addError("INVALID_FORMAT", [schema.format, JSON.stringify(json)], void 0, schema, "format");
|
|
57
55
|
});
|
|
58
|
-
} else if (result
|
|
56
|
+
} else if (!result) report.addError("INVALID_FORMAT", [schema.format, JSON.stringify(json)], void 0, schema, "format");
|
|
59
57
|
}
|
|
60
|
-
} else if (
|
|
58
|
+
} else if (ctx.options.ignoreUnknownFormats !== true && !isModernDraft) report.addError("UNKNOWN_FORMAT", [schema.format], void 0, schema, "format");
|
|
61
59
|
}
|
|
62
|
-
function contentEncodingValidator(report, schema, json) {
|
|
63
|
-
if (
|
|
60
|
+
function contentEncodingValidator(ctx, report, schema, json) {
|
|
61
|
+
if (ctx.options.version !== "draft-07") return;
|
|
64
62
|
if (typeof json !== "string") return;
|
|
65
|
-
|
|
63
|
+
const { contentEncoding } = schema;
|
|
64
|
+
if (contentEncoding !== "base64") return;
|
|
66
65
|
if (!isValidBase64(json)) report.addError("INVALID_FORMAT", ["contentEncoding:base64", JSON.stringify(json)], void 0, schema, "contentEncoding");
|
|
67
66
|
}
|
|
68
|
-
function contentMediaTypeValidator(report, schema, json) {
|
|
69
|
-
if (
|
|
67
|
+
function contentMediaTypeValidator(ctx, report, schema, json) {
|
|
68
|
+
if (ctx.options.version !== "draft-07") return;
|
|
70
69
|
if (typeof json !== "string") return;
|
|
71
|
-
|
|
70
|
+
const { contentMediaType } = schema;
|
|
71
|
+
if (contentMediaType !== "application/json") return;
|
|
72
72
|
let payload = json;
|
|
73
73
|
if (schema.contentEncoding === "base64") {
|
|
74
74
|
const decoded = decodeBase64(json);
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { whatIs } from "../utils/what-is.js";
|
|
2
|
+
import { areEqual } from "../utils/json.js";
|
|
3
|
+
import { shouldSkipValidate } from "./shared.js";
|
|
4
|
+
//#region src/validation/type.ts
|
|
5
|
+
function typeValidator(ctx, report, schema, json) {
|
|
6
|
+
if (shouldSkipValidate(ctx.validateOptions, ["INVALID_TYPE"])) return;
|
|
7
|
+
const jsonType = whatIs(json);
|
|
8
|
+
if (typeof schema.type === "string") {
|
|
9
|
+
if (jsonType !== schema.type && (jsonType !== "integer" || schema.type !== "number")) report.addError("INVALID_TYPE", [schema.type, jsonType], void 0, schema, "type");
|
|
10
|
+
} else if (!schema.type.includes(jsonType) && (jsonType !== "integer" || !schema.type.includes("number"))) report.addError("INVALID_TYPE", [JSON.stringify(schema.type), jsonType], void 0, schema, "type");
|
|
11
|
+
}
|
|
12
|
+
function enumValidator(ctx, report, schema, json) {
|
|
13
|
+
if (shouldSkipValidate(ctx.validateOptions, ["ENUM_CASE_MISMATCH", "ENUM_MISMATCH"])) return;
|
|
14
|
+
const eqOpts = { maxDepth: ctx.options.maxRecursionDepth };
|
|
15
|
+
const eqOptsCI = {
|
|
16
|
+
caseInsensitiveComparison: true,
|
|
17
|
+
maxDepth: ctx.options.maxRecursionDepth
|
|
18
|
+
};
|
|
19
|
+
let caseInsensitiveMatch = false, match = false;
|
|
20
|
+
for (const enumVal of schema.enum) if (areEqual(json, enumVal, eqOpts)) {
|
|
21
|
+
match = true;
|
|
22
|
+
break;
|
|
23
|
+
} else if (areEqual(json, enumVal, eqOptsCI)) caseInsensitiveMatch = true;
|
|
24
|
+
if (!match) {
|
|
25
|
+
const error = caseInsensitiveMatch && ctx.options.enumCaseInsensitiveComparison ? "ENUM_CASE_MISMATCH" : "ENUM_MISMATCH";
|
|
26
|
+
report.addError(error, [JSON.stringify(json)], void 0, schema, "enum");
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function constValidator(ctx, report, schema, json) {
|
|
30
|
+
const constValue = schema.const;
|
|
31
|
+
if (!areEqual(json, constValue, { maxDepth: ctx.options.maxRecursionDepth })) report.addError("CONST", [JSON.stringify(constValue)], void 0, schema);
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
export { constValidator, enumValidator, typeValidator };
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { FormatValidatorFn } from "./format-validators.
|
|
2
|
-
import { ZSchemaOptions } from "./z-schema-options.
|
|
3
|
-
import { Report } from "./report.
|
|
4
|
-
import { Errors, ValidateError } from "./errors.
|
|
5
|
-
import { SchemaCache } from "./schema-cache.
|
|
6
|
-
import { SchemaValidator } from "./schema-validator.
|
|
7
|
-
import { SchemaCompiler } from "./schema-compiler.
|
|
8
|
-
import { JsonSchema, JsonSchemaInternal } from "./json-schema-versions.
|
|
1
|
+
import { FormatValidatorFn } from "./format-validators.js";
|
|
2
|
+
import { ZSchemaOptions } from "./z-schema-options.js";
|
|
3
|
+
import { Report } from "./report.js";
|
|
4
|
+
import { Errors, ValidateError } from "./errors.js";
|
|
5
|
+
import { SchemaCache } from "./schema-cache.js";
|
|
6
|
+
import { SchemaValidator } from "./schema-validator.js";
|
|
7
|
+
import { SchemaCompiler } from "./schema-compiler.js";
|
|
8
|
+
import { JsonSchema, JsonSchemaInternal } from "./json-schema-versions.js";
|
|
9
9
|
|
|
10
10
|
//#region src/z-schema-base.d.ts
|
|
11
11
|
interface ValidateOptions {
|
|
@@ -13,10 +13,10 @@ interface ValidateOptions {
|
|
|
13
13
|
includeErrors?: Array<keyof typeof Errors>;
|
|
14
14
|
excludeErrors?: Array<keyof typeof Errors>;
|
|
15
15
|
}
|
|
16
|
-
|
|
16
|
+
interface ValidateResponse {
|
|
17
17
|
valid: boolean;
|
|
18
18
|
err?: ValidateError;
|
|
19
|
-
}
|
|
19
|
+
}
|
|
20
20
|
type ValidateCallback = (err: ValidateResponse['err'], valid: ValidateResponse['valid']) => void;
|
|
21
21
|
declare class ZSchemaBase {
|
|
22
22
|
scache: SchemaCache;
|
|
@@ -36,8 +36,7 @@ declare class ZSchemaBase {
|
|
|
36
36
|
getDefaultSchemaId(): string;
|
|
37
37
|
_validate(json: unknown, schema: JsonSchema | string, options: ValidateOptions, callback: ValidateCallback): void;
|
|
38
38
|
_validate(json: unknown, schema: JsonSchema | string, callback: ValidateCallback): void;
|
|
39
|
-
_validate(json: unknown, schema: JsonSchema | string, options
|
|
40
|
-
_validate(json: unknown, schema: JsonSchema | string): true;
|
|
39
|
+
_validate(json: unknown, schema: JsonSchema | string, options?: ValidateOptions): true;
|
|
41
40
|
_validateSchema(schemaOrArr: JsonSchema | JsonSchema[]): true;
|
|
42
41
|
/**
|
|
43
42
|
* Register a format validator on this instance only (does not affect other instances or the global registry).
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import { getRemotePath } from "./utils/uri.
|
|
2
|
-
import { isObject, whatIs } from "./utils/what-is.
|
|
3
|
-
import { isInternalKey } from "./json-schema.
|
|
4
|
-
import { getValidateError } from "./errors.
|
|
5
|
-
import { copyProp } from "./utils/properties.
|
|
6
|
-
import { deepClone } from "./utils/clone.
|
|
7
|
-
import { get, sortedKeys } from "./utils/json.
|
|
8
|
-
import { Report } from "./report.
|
|
9
|
-
import { VERSION_SCHEMA_URL_MAPPING } from "./json-schema-versions.
|
|
10
|
-
import { defaultOptions, normalizeOptions } from "./z-schema-options.
|
|
11
|
-
import { SchemaCache, prepareRemoteSchema } from "./schema-cache.
|
|
12
|
-
import { getSupportedFormats } from "./format-validators.
|
|
13
|
-
import { validate } from "./json-validation.
|
|
14
|
-
import { SchemaCompiler } from "./schema-compiler.
|
|
15
|
-
import { SchemaValidator } from "./schema-validator.
|
|
1
|
+
import { getRemotePath } from "./utils/uri.js";
|
|
2
|
+
import { isObject, whatIs } from "./utils/what-is.js";
|
|
3
|
+
import { isInternalKey } from "./json-schema.js";
|
|
4
|
+
import { getValidateError } from "./errors.js";
|
|
5
|
+
import { copyProp } from "./utils/properties.js";
|
|
6
|
+
import { deepClone } from "./utils/clone.js";
|
|
7
|
+
import { get, sortedKeys } from "./utils/json.js";
|
|
8
|
+
import { Report } from "./report.js";
|
|
9
|
+
import { VERSION_SCHEMA_URL_MAPPING } from "./json-schema-versions.js";
|
|
10
|
+
import { defaultOptions, normalizeOptions } from "./z-schema-options.js";
|
|
11
|
+
import { SchemaCache, prepareRemoteSchema } from "./schema-cache.js";
|
|
12
|
+
import { getSupportedFormats } from "./format-validators.js";
|
|
13
|
+
import { validate } from "./json-validation.js";
|
|
14
|
+
import { SchemaCompiler } from "./schema-compiler.js";
|
|
15
|
+
import { SchemaValidator } from "./schema-validator.js";
|
|
16
16
|
//#region src/z-schema-base.ts
|
|
17
17
|
/**
|
|
18
18
|
* Module-private symbol used by `ZSchema.create()` to authorise construction.
|
|
@@ -40,7 +40,7 @@ var ZSchemaBase = class {
|
|
|
40
40
|
* create a circular dependency).
|
|
41
41
|
*/
|
|
42
42
|
_jsonValidate(report, schema, json) {
|
|
43
|
-
return validate
|
|
43
|
+
return validate(this, report, schema, json);
|
|
44
44
|
}
|
|
45
45
|
getDefaultSchemaId() {
|
|
46
46
|
return this.options.version && this.options.version !== "none" ? VERSION_SCHEMA_URL_MAPPING[this.options.version] : VERSION_SCHEMA_URL_MAPPING[defaultOptions.version];
|
|
@@ -53,9 +53,9 @@ var ZSchemaBase = class {
|
|
|
53
53
|
if (!options) options = {};
|
|
54
54
|
this.validateOptions = options;
|
|
55
55
|
if (typeof schema !== "string" && typeof schema !== "boolean" && !isObject(schema)) {
|
|
56
|
-
const e = /* @__PURE__ */ new Error(
|
|
56
|
+
const e = /* @__PURE__ */ new Error(`Invalid .validate call - schema must be a string or object but ${whatIs(schema)} was passed!`);
|
|
57
57
|
if (callback) {
|
|
58
|
-
setTimeout(
|
|
58
|
+
setTimeout(() => {
|
|
59
59
|
callback(e, false);
|
|
60
60
|
}, 0);
|
|
61
61
|
return;
|
|
@@ -70,9 +70,9 @@ var ZSchemaBase = class {
|
|
|
70
70
|
const schemaName = schema;
|
|
71
71
|
_schema = this.scache.getSchema(report, schemaName);
|
|
72
72
|
if (!_schema) {
|
|
73
|
-
const e = /* @__PURE__ */ new Error(
|
|
73
|
+
const e = /* @__PURE__ */ new Error(`Schema with id '${schemaName}' wasn't found in the validator cache!`);
|
|
74
74
|
if (callback) {
|
|
75
|
-
setTimeout(
|
|
75
|
+
setTimeout(() => {
|
|
76
76
|
callback(e, false);
|
|
77
77
|
}, 0);
|
|
78
78
|
return;
|
|
@@ -90,9 +90,9 @@ var ZSchemaBase = class {
|
|
|
90
90
|
report.rootSchema = _schema;
|
|
91
91
|
_schema = get(_schema, options.schemaPath);
|
|
92
92
|
if (!_schema) {
|
|
93
|
-
const e = /* @__PURE__ */ new Error(
|
|
93
|
+
const e = /* @__PURE__ */ new Error(`Schema path '${options.schemaPath}' wasn't found in the schema!`);
|
|
94
94
|
if (callback) {
|
|
95
|
-
setTimeout(
|
|
95
|
+
setTimeout(() => {
|
|
96
96
|
callback(e, false);
|
|
97
97
|
}, 0);
|
|
98
98
|
return;
|
|
@@ -100,7 +100,7 @@ var ZSchemaBase = class {
|
|
|
100
100
|
throw e;
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
|
-
if (!foundError) validate
|
|
103
|
+
if (!foundError) validate(this, report, _schema, json);
|
|
104
104
|
if (callback) {
|
|
105
105
|
report.processAsyncTasks(this.options.asyncTimeout, callback);
|
|
106
106
|
return;
|
|
@@ -171,8 +171,8 @@ var ZSchemaBase = class {
|
|
|
171
171
|
if (!err) return [];
|
|
172
172
|
const details = err.details || [];
|
|
173
173
|
const missingRefs = [];
|
|
174
|
-
function collect(
|
|
175
|
-
for (const detail of
|
|
174
|
+
function collect(items) {
|
|
175
|
+
for (const detail of items) {
|
|
176
176
|
if (detail.code === "UNRESOLVABLE_REFERENCE" || detail.code === "SCHEMA_NOT_REACHABLE") missingRefs.push(detail.params[0]);
|
|
177
177
|
if (detail.inner) collect(detail.inner);
|
|
178
178
|
}
|
|
@@ -188,9 +188,13 @@ var ZSchemaBase = class {
|
|
|
188
188
|
getMissingRemoteReferences(err) {
|
|
189
189
|
const missingReferences = this.getMissingReferences(err);
|
|
190
190
|
const missingRemoteReferences = [];
|
|
191
|
+
const seen = /* @__PURE__ */ new Set();
|
|
191
192
|
for (const ref of missingReferences) {
|
|
192
193
|
const remoteReference = getRemotePath(ref);
|
|
193
|
-
if (remoteReference && !
|
|
194
|
+
if (remoteReference && !seen.has(remoteReference)) {
|
|
195
|
+
seen.add(remoteReference);
|
|
196
|
+
missingRemoteReferences.push(remoteReference);
|
|
197
|
+
}
|
|
194
198
|
}
|
|
195
199
|
return missingRemoteReferences;
|
|
196
200
|
}
|
|
@@ -203,24 +207,25 @@ var ZSchemaBase = class {
|
|
|
203
207
|
getResolvedSchema(schemaId) {
|
|
204
208
|
const report = new Report(this.options);
|
|
205
209
|
const schema = this.scache.getSchemaByUri(report, schemaId);
|
|
206
|
-
if (!schema) return
|
|
210
|
+
if (!schema) return;
|
|
207
211
|
const clonedSchema = deepClone(schema, this.options.maxRecursionDepth);
|
|
208
212
|
const visited = /* @__PURE__ */ new WeakSet();
|
|
209
|
-
const cleanup =
|
|
210
|
-
|
|
211
|
-
const typeOf = whatIs(schema);
|
|
213
|
+
const cleanup = (node) => {
|
|
214
|
+
const typeOf = whatIs(node);
|
|
212
215
|
if (typeOf !== "object" && typeOf !== "array") return;
|
|
213
|
-
if (visited.has(
|
|
214
|
-
visited.add(
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
const
|
|
218
|
-
|
|
219
|
-
delete
|
|
220
|
-
|
|
216
|
+
if (visited.has(node)) return;
|
|
217
|
+
visited.add(node);
|
|
218
|
+
const schemaNode = node;
|
|
219
|
+
if (schemaNode.$ref && schemaNode.__$refResolved) {
|
|
220
|
+
const from = schemaNode.__$refResolved;
|
|
221
|
+
const to = schemaNode;
|
|
222
|
+
delete schemaNode.$ref;
|
|
223
|
+
delete schemaNode.__$refResolved;
|
|
224
|
+
for (const key in from) if (Object.hasOwn(from, key)) copyProp(from, to, key);
|
|
221
225
|
}
|
|
222
|
-
|
|
223
|
-
|
|
226
|
+
const record = node;
|
|
227
|
+
for (const key in record) if (Object.hasOwn(record, key)) if (isInternalKey(key)) delete record[key];
|
|
228
|
+
else cleanup(record[key]);
|
|
224
229
|
};
|
|
225
230
|
cleanup(clonedSchema);
|
|
226
231
|
return clonedSchema;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { FormatValidatorFn } from "./format-validators.
|
|
2
|
-
import { Report } from "./report.
|
|
3
|
-
import { JsonSchemaVersion } from "./json-schema-versions.
|
|
1
|
+
import { FormatValidatorFn } from "./format-validators.js";
|
|
2
|
+
import { Report } from "./report.js";
|
|
3
|
+
import { JsonSchemaVersion } from "./json-schema-versions.js";
|
|
4
4
|
|
|
5
5
|
//#region src/z-schema-options.d.ts
|
|
6
6
|
interface ZSchemaOptions {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { MAX_ASYNC_TIMEOUT } from "./utils/constants.
|
|
2
|
-
import { shallowClone } from "./utils/clone.
|
|
3
|
-
import { CURRENT_DEFAULT_SCHEMA_VERSION } from "./json-schema-versions.
|
|
1
|
+
import { MAX_ASYNC_TIMEOUT } from "./utils/constants.js";
|
|
2
|
+
import { shallowClone } from "./utils/clone.js";
|
|
3
|
+
import { CURRENT_DEFAULT_SCHEMA_VERSION } from "./json-schema-versions.js";
|
|
4
4
|
//#region src/z-schema-options.ts
|
|
5
5
|
const defaultOptions = {
|
|
6
6
|
version: CURRENT_DEFAULT_SCHEMA_VERSION,
|
|
@@ -33,7 +33,7 @@ const normalizeOptions = (options) => {
|
|
|
33
33
|
let normalized;
|
|
34
34
|
if (typeof options === "object") {
|
|
35
35
|
let keys = Object.keys(options);
|
|
36
|
-
for (const key of keys) if (defaultOptions[key] === void 0) throw new Error(
|
|
36
|
+
for (const key of keys) if (defaultOptions[key] === void 0) throw new Error(`Unexpected option passed to constructor: ${key}`);
|
|
37
37
|
keys = Object.keys(defaultOptions);
|
|
38
38
|
for (const key of keys) if (options[key] === void 0) options[key] = shallowClone(defaultOptions[key]);
|
|
39
39
|
normalized = options;
|
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import { normalizeOptions } from "./z-schema-options.
|
|
2
|
-
import { SchemaCache } from "./schema-cache.
|
|
3
|
-
import draft_04_schema_default from "./schemas/draft-04-schema.
|
|
4
|
-
import draft_06_schema_default from "./schemas/draft-06-schema.
|
|
5
|
-
import draft_07_schema_default from "./schemas/draft-07-schema.
|
|
6
|
-
import draft_2019_09_meta_applicator_default from "./schemas/draft-2019-09-meta-applicator.
|
|
7
|
-
import draft_2019_09_meta_content_default from "./schemas/draft-2019-09-meta-content.
|
|
8
|
-
import draft_2019_09_meta_core_default from "./schemas/draft-2019-09-meta-core.
|
|
9
|
-
import draft_2019_09_meta_format_default from "./schemas/draft-2019-09-meta-format.
|
|
10
|
-
import draft_2019_09_meta_meta_data_default from "./schemas/draft-2019-09-meta-meta-data.
|
|
11
|
-
import draft_2019_09_meta_validation_default from "./schemas/draft-2019-09-meta-validation.
|
|
12
|
-
import draft_2019_09_schema_default from "./schemas/draft-2019-09-schema.
|
|
13
|
-
import draft_2020_12_meta_applicator_default from "./schemas/draft-2020-12-meta-applicator.
|
|
14
|
-
import draft_2020_12_meta_content_default from "./schemas/draft-2020-12-meta-content.
|
|
15
|
-
import draft_2020_12_meta_core_default from "./schemas/draft-2020-12-meta-core.
|
|
16
|
-
import draft_2020_12_meta_format_annotation_default from "./schemas/draft-2020-12-meta-format-annotation.
|
|
17
|
-
import draft_2020_12_meta_format_assertion_default from "./schemas/draft-2020-12-meta-format-assertion.
|
|
18
|
-
import draft_2020_12_meta_meta_data_default from "./schemas/draft-2020-12-meta-meta-data.
|
|
19
|
-
import draft_2020_12_meta_unevaluated_default from "./schemas/draft-2020-12-meta-unevaluated.
|
|
20
|
-
import draft_2020_12_meta_validation_default from "./schemas/draft-2020-12-meta-validation.
|
|
21
|
-
import draft_2020_12_schema_default from "./schemas/draft-2020-12-schema.
|
|
1
|
+
import { normalizeOptions } from "./z-schema-options.js";
|
|
2
|
+
import { SchemaCache } from "./schema-cache.js";
|
|
3
|
+
import draft_04_schema_default from "./schemas/draft-04-schema.js";
|
|
4
|
+
import draft_06_schema_default from "./schemas/draft-06-schema.js";
|
|
5
|
+
import draft_07_schema_default from "./schemas/draft-07-schema.js";
|
|
6
|
+
import draft_2019_09_meta_applicator_default from "./schemas/draft-2019-09-meta-applicator.js";
|
|
7
|
+
import draft_2019_09_meta_content_default from "./schemas/draft-2019-09-meta-content.js";
|
|
8
|
+
import draft_2019_09_meta_core_default from "./schemas/draft-2019-09-meta-core.js";
|
|
9
|
+
import draft_2019_09_meta_format_default from "./schemas/draft-2019-09-meta-format.js";
|
|
10
|
+
import draft_2019_09_meta_meta_data_default from "./schemas/draft-2019-09-meta-meta-data.js";
|
|
11
|
+
import draft_2019_09_meta_validation_default from "./schemas/draft-2019-09-meta-validation.js";
|
|
12
|
+
import draft_2019_09_schema_default from "./schemas/draft-2019-09-schema.js";
|
|
13
|
+
import draft_2020_12_meta_applicator_default from "./schemas/draft-2020-12-meta-applicator.js";
|
|
14
|
+
import draft_2020_12_meta_content_default from "./schemas/draft-2020-12-meta-content.js";
|
|
15
|
+
import draft_2020_12_meta_core_default from "./schemas/draft-2020-12-meta-core.js";
|
|
16
|
+
import draft_2020_12_meta_format_annotation_default from "./schemas/draft-2020-12-meta-format-annotation.js";
|
|
17
|
+
import draft_2020_12_meta_format_assertion_default from "./schemas/draft-2020-12-meta-format-assertion.js";
|
|
18
|
+
import draft_2020_12_meta_meta_data_default from "./schemas/draft-2020-12-meta-meta-data.js";
|
|
19
|
+
import draft_2020_12_meta_unevaluated_default from "./schemas/draft-2020-12-meta-unevaluated.js";
|
|
20
|
+
import draft_2020_12_meta_validation_default from "./schemas/draft-2020-12-meta-validation.js";
|
|
21
|
+
import draft_2020_12_schema_default from "./schemas/draft-2020-12-schema.js";
|
|
22
22
|
//#region src/z-schema-versions.ts
|
|
23
23
|
const Draft4Schema = draft_04_schema_default;
|
|
24
24
|
const Draft6Schema = draft_06_schema_default;
|
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
import { FormatValidatorFn } from "./format-validators.
|
|
2
|
-
import { ZSchemaOptions } from "./z-schema-options.
|
|
3
|
-
import { ValidateOptions, ValidateResponse, ZSchemaBase } from "./z-schema-base.
|
|
4
|
-
import { JsonSchema } from "./json-schema-versions.
|
|
5
|
-
import { SchemaReader } from "./z-schema-reader.
|
|
1
|
+
import { FormatValidatorFn } from "./format-validators.js";
|
|
2
|
+
import { ZSchemaOptions } from "./z-schema-options.js";
|
|
3
|
+
import { ValidateOptions, ValidateResponse, ZSchemaBase } from "./z-schema-base.js";
|
|
4
|
+
import { JsonSchema } from "./json-schema-versions.js";
|
|
5
|
+
import { SchemaReader } from "./z-schema-reader.js";
|
|
6
6
|
|
|
7
7
|
//#region src/z-schema.d.ts
|
|
8
8
|
declare class ZSchema extends ZSchemaBase {
|
|
9
|
-
/** @internal Use ZSchema.create() instead. */
|
|
10
|
-
constructor(options: ZSchemaOptions | undefined, token: symbol);
|
|
11
9
|
/**
|
|
12
10
|
* Register a global format validator available to all instances.
|
|
13
11
|
* @param name - The format name (e.g. `'email'`, `'date'`).
|
|
@@ -125,8 +123,6 @@ declare class ZSchema extends ZSchemaBase {
|
|
|
125
123
|
* Created via `ZSchema.create({ safe: true })`.
|
|
126
124
|
*/
|
|
127
125
|
declare class ZSchemaSafe extends ZSchemaBase {
|
|
128
|
-
/** @internal Use ZSchema.create() instead. */
|
|
129
|
-
constructor(options: ZSchemaOptions | undefined, token: symbol);
|
|
130
126
|
/**
|
|
131
127
|
* Validate JSON data against a schema.
|
|
132
128
|
* @param json - The data to validate.
|
|
@@ -147,8 +143,6 @@ declare class ZSchemaSafe extends ZSchemaBase {
|
|
|
147
143
|
* Created via `ZSchema.create({ async: true })`.
|
|
148
144
|
*/
|
|
149
145
|
declare class ZSchemaAsync extends ZSchemaBase {
|
|
150
|
-
/** @internal Use ZSchema.create() instead. */
|
|
151
|
-
constructor(options: ZSchemaOptions | undefined, token: symbol);
|
|
152
146
|
/**
|
|
153
147
|
* Validate JSON data against a schema asynchronously.
|
|
154
148
|
* @param json - The data to validate.
|
|
@@ -171,8 +165,6 @@ declare class ZSchemaAsync extends ZSchemaBase {
|
|
|
171
165
|
* Created via `ZSchema.create({ async: true, safe: true })`.
|
|
172
166
|
*/
|
|
173
167
|
declare class ZSchemaAsyncSafe extends ZSchemaBase {
|
|
174
|
-
/** @internal Use ZSchema.create() instead. */
|
|
175
|
-
constructor(options: ZSchemaOptions | undefined, token: symbol);
|
|
176
168
|
/**
|
|
177
169
|
* Validate JSON data against a schema asynchronously.
|
|
178
170
|
* The promise always resolves (never rejects).
|