z-schema 12.0.4 → 12.1.0

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 (176) hide show
  1. package/bin/z-schema +7 -2
  2. package/cjs/index.d.ts +541 -530
  3. package/cjs/index.js +3963 -7740
  4. package/dist/errors.d.mts +64 -0
  5. package/dist/errors.mjs +67 -0
  6. package/dist/format-validators.d.mts +14 -0
  7. package/dist/format-validators.mjs +261 -0
  8. package/dist/index.d.mts +10 -0
  9. package/dist/index.mjs +7 -0
  10. package/dist/json-schema-versions.d.mts +137 -0
  11. package/dist/json-schema-versions.mjs +11 -0
  12. package/dist/json-schema.d.mts +61 -0
  13. package/dist/json-schema.mjs +53 -0
  14. package/dist/json-validation.mjs +441 -0
  15. package/dist/report.d.mts +97 -0
  16. package/dist/report.mjs +194 -0
  17. package/dist/schema-cache.d.mts +23 -0
  18. package/dist/schema-cache.mjs +144 -0
  19. package/dist/schema-compiler.d.mts +23 -0
  20. package/dist/schema-compiler.mjs +265 -0
  21. package/dist/schema-validator.d.mts +15 -0
  22. package/dist/schema-validator.mjs +446 -0
  23. package/dist/schemas/draft-04-schema.mjs +125 -0
  24. package/dist/schemas/draft-06-schema.mjs +126 -0
  25. package/dist/schemas/draft-07-schema.mjs +141 -0
  26. package/dist/schemas/draft-2019-09-meta-applicator.mjs +46 -0
  27. package/dist/schemas/draft-2019-09-meta-content.mjs +15 -0
  28. package/dist/schemas/draft-2019-09-meta-core.mjs +52 -0
  29. package/dist/schemas/draft-2019-09-meta-format.mjs +11 -0
  30. package/dist/schemas/draft-2019-09-meta-meta-data.mjs +31 -0
  31. package/dist/schemas/draft-2019-09-meta-validation.mjs +80 -0
  32. package/dist/schemas/draft-2019-09-schema.mjs +39 -0
  33. package/dist/schemas/draft-2020-12-meta-applicator.mjs +45 -0
  34. package/dist/schemas/draft-2020-12-meta-content.mjs +15 -0
  35. package/dist/schemas/draft-2020-12-meta-core.mjs +46 -0
  36. package/dist/schemas/draft-2020-12-meta-format-annotation.mjs +11 -0
  37. package/dist/schemas/draft-2020-12-meta-format-assertion.mjs +11 -0
  38. package/dist/schemas/draft-2020-12-meta-meta-data.mjs +31 -0
  39. package/dist/schemas/draft-2020-12-meta-unevaluated.mjs +14 -0
  40. package/dist/schemas/draft-2020-12-meta-validation.mjs +80 -0
  41. package/dist/schemas/draft-2020-12-schema.mjs +55 -0
  42. package/dist/utils/array.mjs +54 -0
  43. package/dist/utils/base64.mjs +21 -0
  44. package/dist/utils/clone.mjs +42 -0
  45. package/dist/utils/constants.mjs +16 -0
  46. package/dist/utils/date.mjs +19 -0
  47. package/dist/utils/hostname.mjs +73 -0
  48. package/dist/utils/json.mjs +34 -0
  49. package/dist/utils/properties.mjs +11 -0
  50. package/dist/utils/schema-regex.mjs +49 -0
  51. package/dist/utils/symbols.mjs +5 -0
  52. package/dist/utils/time.mjs +42 -0
  53. package/dist/utils/unicode.mjs +12 -0
  54. package/dist/utils/uri.mjs +12 -0
  55. package/dist/utils/what-is.mjs +23 -0
  56. package/dist/validation/array.mjs +57 -0
  57. package/dist/validation/combinators.mjs +62 -0
  58. package/dist/validation/numeric.mjs +38 -0
  59. package/dist/validation/object.mjs +109 -0
  60. package/dist/validation/ref.mjs +49 -0
  61. package/dist/validation/shared.mjs +97 -0
  62. package/dist/validation/string.mjs +88 -0
  63. package/dist/validation/type.mjs +32 -0
  64. package/dist/z-schema-base.d.mts +85 -0
  65. package/dist/z-schema-base.mjs +230 -0
  66. package/dist/z-schema-options.d.mts +35 -0
  67. package/dist/z-schema-options.mjs +55 -0
  68. package/dist/z-schema-reader.d.mts +6 -0
  69. package/dist/z-schema-reader.mjs +10 -0
  70. package/dist/z-schema-versions.mjs +67 -0
  71. package/dist/z-schema.d.mts +193 -0
  72. package/dist/z-schema.mjs +300 -0
  73. package/package.json +49 -64
  74. package/src/index.ts +3 -3
  75. package/src/schema-compiler.ts +30 -10
  76. package/src/schema-validator.ts +1 -1
  77. package/src/utils/schema-regex.ts +13 -2
  78. package/src/z-schema-base.ts +1 -1
  79. package/src/z-schema-versions.ts +1 -2
  80. package/src/z-schema.ts +0 -1
  81. package/umd/ZSchema.js +5934 -8110
  82. package/umd/ZSchema.min.js +2 -1
  83. package/dist/errors.js +0 -72
  84. package/dist/format-validators.js +0 -392
  85. package/dist/index.js +0 -5
  86. package/dist/json-schema-versions.js +0 -8
  87. package/dist/json-schema.js +0 -84
  88. package/dist/json-validation.js +0 -728
  89. package/dist/package.json +0 -3
  90. package/dist/report.js +0 -265
  91. package/dist/schema-cache.js +0 -211
  92. package/dist/schema-compiler.js +0 -415
  93. package/dist/schema-validator.js +0 -721
  94. package/dist/schemas/draft-04-schema.json +0 -149
  95. package/dist/schemas/draft-06-schema.json +0 -155
  96. package/dist/schemas/draft-07-schema.json +0 -172
  97. package/dist/schemas/draft-2019-09-meta-applicator.json +0 -52
  98. package/dist/schemas/draft-2019-09-meta-content.json +0 -12
  99. package/dist/schemas/draft-2019-09-meta-core.json +0 -53
  100. package/dist/schemas/draft-2019-09-meta-format.json +0 -10
  101. package/dist/schemas/draft-2019-09-meta-meta-data.json +0 -32
  102. package/dist/schemas/draft-2019-09-meta-validation.json +0 -94
  103. package/dist/schemas/draft-2019-09-schema.json +0 -41
  104. package/dist/schemas/draft-2020-12-meta-applicator.json +0 -44
  105. package/dist/schemas/draft-2020-12-meta-content.json +0 -12
  106. package/dist/schemas/draft-2020-12-meta-core.json +0 -47
  107. package/dist/schemas/draft-2020-12-meta-format-annotation.json +0 -10
  108. package/dist/schemas/draft-2020-12-meta-format-assertion.json +0 -10
  109. package/dist/schemas/draft-2020-12-meta-meta-data.json +0 -32
  110. package/dist/schemas/draft-2020-12-meta-unevaluated.json +0 -11
  111. package/dist/schemas/draft-2020-12-meta-validation.json +0 -94
  112. package/dist/schemas/draft-2020-12-schema.json +0 -57
  113. package/dist/types/errors.d.ts +0 -64
  114. package/dist/types/format-validators.d.ts +0 -11
  115. package/dist/types/index.d.ts +0 -14
  116. package/dist/types/json-schema-versions.d.ts +0 -142
  117. package/dist/types/json-schema.d.ts +0 -66
  118. package/dist/types/json-validation.d.ts +0 -6
  119. package/dist/types/report.d.ts +0 -94
  120. package/dist/types/schema-cache.d.ts +0 -27
  121. package/dist/types/schema-compiler.d.ts +0 -31
  122. package/dist/types/schema-validator.d.ts +0 -10
  123. package/dist/types/utils/array.d.ts +0 -9
  124. package/dist/types/utils/base64.d.ts +0 -2
  125. package/dist/types/utils/clone.d.ts +0 -2
  126. package/dist/types/utils/constants.d.ts +0 -19
  127. package/dist/types/utils/date.d.ts +0 -1
  128. package/dist/types/utils/hostname.d.ts +0 -2
  129. package/dist/types/utils/json.d.ts +0 -9
  130. package/dist/types/utils/properties.d.ts +0 -1
  131. package/dist/types/utils/schema-regex.d.ts +0 -10
  132. package/dist/types/utils/symbols.d.ts +0 -2
  133. package/dist/types/utils/time.d.ts +0 -12
  134. package/dist/types/utils/unicode.d.ts +0 -5
  135. package/dist/types/utils/uri.d.ts +0 -4
  136. package/dist/types/utils/what-is.d.ts +0 -4
  137. package/dist/types/validation/array.d.ts +0 -12
  138. package/dist/types/validation/combinators.d.ts +0 -10
  139. package/dist/types/validation/numeric.d.ts +0 -8
  140. package/dist/types/validation/object.d.ts +0 -13
  141. package/dist/types/validation/ref.d.ts +0 -11
  142. package/dist/types/validation/shared.d.ts +0 -26
  143. package/dist/types/validation/string.d.ts +0 -9
  144. package/dist/types/validation/type.d.ts +0 -6
  145. package/dist/types/z-schema-base.d.ts +0 -87
  146. package/dist/types/z-schema-options.d.ts +0 -35
  147. package/dist/types/z-schema-reader.d.ts +0 -4
  148. package/dist/types/z-schema-versions.d.ts +0 -1
  149. package/dist/types/z-schema.d.ts +0 -191
  150. package/dist/utils/array.js +0 -69
  151. package/dist/utils/base64.js +0 -29
  152. package/dist/utils/clone.js +0 -59
  153. package/dist/utils/constants.js +0 -19
  154. package/dist/utils/date.js +0 -21
  155. package/dist/utils/hostname.js +0 -146
  156. package/dist/utils/json.js +0 -70
  157. package/dist/utils/properties.js +0 -10
  158. package/dist/utils/schema-regex.js +0 -52
  159. package/dist/utils/symbols.js +0 -2
  160. package/dist/utils/time.js +0 -50
  161. package/dist/utils/unicode.js +0 -12
  162. package/dist/utils/uri.js +0 -15
  163. package/dist/utils/what-is.js +0 -32
  164. package/dist/validation/array.js +0 -127
  165. package/dist/validation/combinators.js +0 -106
  166. package/dist/validation/numeric.js +0 -97
  167. package/dist/validation/object.js +0 -237
  168. package/dist/validation/ref.js +0 -70
  169. package/dist/validation/shared.js +0 -136
  170. package/dist/validation/string.js +0 -170
  171. package/dist/validation/type.js +0 -55
  172. package/dist/z-schema-base.js +0 -286
  173. package/dist/z-schema-options.js +0 -99
  174. package/dist/z-schema-reader.js +0 -7
  175. package/dist/z-schema-versions.js +0 -110
  176. package/dist/z-schema.js +0 -287
@@ -0,0 +1,230 @@
1
+ import { getRemotePath } from "./utils/uri.mjs";
2
+ import { isObject, whatIs } from "./utils/what-is.mjs";
3
+ import { isInternalKey } from "./json-schema.mjs";
4
+ import { getValidateError } from "./errors.mjs";
5
+ import { copyProp } from "./utils/properties.mjs";
6
+ import { deepClone } from "./utils/clone.mjs";
7
+ import { get, sortedKeys } from "./utils/json.mjs";
8
+ import { Report } from "./report.mjs";
9
+ import { VERSION_SCHEMA_URL_MAPPING } from "./json-schema-versions.mjs";
10
+ import { defaultOptions, normalizeOptions } from "./z-schema-options.mjs";
11
+ import { SchemaCache, prepareRemoteSchema } from "./schema-cache.mjs";
12
+ import { getSupportedFormats } from "./format-validators.mjs";
13
+ import { validate } from "./json-validation.mjs";
14
+ import { SchemaCompiler } from "./schema-compiler.mjs";
15
+ import { SchemaValidator } from "./schema-validator.mjs";
16
+ //#region src/z-schema-base.ts
17
+ /**
18
+ * Module-private symbol used by `ZSchema.create()` to authorise construction.
19
+ * Not exported — external code cannot instantiate ZSchema variants directly.
20
+ */
21
+ const FACTORY_TOKEN = Symbol("ZSchema.factory");
22
+ var ZSchemaBase = class {
23
+ scache;
24
+ sc;
25
+ sv;
26
+ validateOptions = {};
27
+ options;
28
+ constructor(options, token) {
29
+ if (token !== FACTORY_TOKEN) throw new Error("do not use new ZSchema(), use ZSchema.create() instead");
30
+ this.scache = new SchemaCache(this);
31
+ this.sc = new SchemaCompiler(this);
32
+ this.sv = new SchemaValidator(this);
33
+ this.options = normalizeOptions(options);
34
+ }
35
+ /**
36
+ * Internal recursive JSON validation — delegates to the `validate` function
37
+ * in `json-validation.ts`. Exposed as a method so that per-keyword validator
38
+ * modules (array, combinators, object) can call back into the core validator
39
+ * via `this` without importing `json-validation.ts` directly (which would
40
+ * create a circular dependency).
41
+ */
42
+ _jsonValidate(report, schema, json) {
43
+ return validate.call(this, report, schema, json);
44
+ }
45
+ getDefaultSchemaId() {
46
+ return this.options.version && this.options.version !== "none" ? VERSION_SCHEMA_URL_MAPPING[this.options.version] : VERSION_SCHEMA_URL_MAPPING[defaultOptions.version];
47
+ }
48
+ _validate(json, schema, options, callback) {
49
+ if (typeof options === "function") {
50
+ callback = options;
51
+ options = {};
52
+ }
53
+ if (!options) options = {};
54
+ this.validateOptions = options;
55
+ if (typeof schema !== "string" && typeof schema !== "boolean" && !isObject(schema)) {
56
+ const e = /* @__PURE__ */ new Error("Invalid .validate call - schema must be a string or object but " + whatIs(schema) + " was passed!");
57
+ if (callback) {
58
+ setTimeout(function() {
59
+ callback(e, false);
60
+ }, 0);
61
+ return;
62
+ }
63
+ throw e;
64
+ }
65
+ let foundError = false;
66
+ const report = new Report(this.options, options);
67
+ report.json = json;
68
+ let _schema;
69
+ if (typeof schema === "string") {
70
+ const schemaName = schema;
71
+ _schema = this.scache.getSchema(report, schemaName);
72
+ if (!_schema) {
73
+ const e = /* @__PURE__ */ new Error("Schema with id '" + schemaName + "' wasn't found in the validator cache!");
74
+ if (callback) {
75
+ setTimeout(function() {
76
+ callback(e, false);
77
+ }, 0);
78
+ return;
79
+ }
80
+ throw e;
81
+ }
82
+ } else _schema = this.scache.getSchema(report, schema);
83
+ let compiled = false;
84
+ if (!foundError) compiled = this.sc.compileSchema(report, _schema);
85
+ if (!compiled) foundError = true;
86
+ let validated = false;
87
+ if (!foundError) validated = this.sv.validateSchema(report, _schema);
88
+ if (!validated) foundError = true;
89
+ if (options.schemaPath) {
90
+ report.rootSchema = _schema;
91
+ _schema = get(_schema, options.schemaPath);
92
+ if (!_schema) {
93
+ const e = /* @__PURE__ */ new Error("Schema path '" + options.schemaPath + "' wasn't found in the schema!");
94
+ if (callback) {
95
+ setTimeout(function() {
96
+ callback(e, false);
97
+ }, 0);
98
+ return;
99
+ }
100
+ throw e;
101
+ }
102
+ }
103
+ if (!foundError) validate.call(this, report, _schema, json);
104
+ if (callback) {
105
+ report.processAsyncTasks(this.options.asyncTimeout, callback);
106
+ return;
107
+ } else if (report.asyncTasks.length > 0) throw new Error("This validation has async tasks and cannot be done in sync mode, please provide callback argument.");
108
+ if (!report.isValid()) throw getValidateError({
109
+ message: report.commonErrorMessage,
110
+ details: report.errors
111
+ });
112
+ return true;
113
+ }
114
+ _validateSchema(schemaOrArr) {
115
+ if (Array.isArray(schemaOrArr) && schemaOrArr.length === 0) throw new Error(".compileSchema was called with an empty array");
116
+ const report = new Report(this.options);
117
+ if (Array.isArray(schemaOrArr)) {
118
+ const arr = this.scache.getSchema(report, schemaOrArr);
119
+ if (this.sc.compileSchema(report, arr)) this.sv.validateSchema(report, arr);
120
+ } else {
121
+ const schema = this.scache.getSchema(report, schemaOrArr);
122
+ if (this.sc.compileSchema(report, schema)) this.sv.validateSchema(report, schema);
123
+ }
124
+ if (!report.isValid()) throw getValidateError({
125
+ message: report.commonErrorMessage,
126
+ details: report.errors
127
+ });
128
+ return true;
129
+ }
130
+ /**
131
+ * Register a format validator on this instance only (does not affect other instances or the global registry).
132
+ * @param name - The format name.
133
+ * @param validatorFunction - A sync or async function `(value: unknown) => boolean | Promise<boolean>`.
134
+ */
135
+ registerFormat(name, validatorFunction) {
136
+ if (!this.options.customFormats) this.options.customFormats = {};
137
+ this.options.customFormats[name] = validatorFunction;
138
+ }
139
+ /**
140
+ * Unregister an instance-scoped format validator.
141
+ * @param name - The format name to unregister.
142
+ */
143
+ unregisterFormat(name) {
144
+ if (!this.options.customFormats) this.options.customFormats = {};
145
+ this.options.customFormats[name] = null;
146
+ }
147
+ /** Returns the names of format validators registered on this instance. */
148
+ getRegisteredFormats() {
149
+ return sortedKeys(this.options.customFormats || {}).filter((key) => this.options.customFormats?.[key] != null);
150
+ }
151
+ /** Returns all supported format names (global + instance-registered). */
152
+ getSupportedFormats() {
153
+ return getSupportedFormats(this.options.customFormats);
154
+ }
155
+ /**
156
+ * Register a remote schema in this instance's cache so `$ref` can resolve to it.
157
+ * @param uri - The URI the schema will be known by.
158
+ * @param schema - The schema object or JSON string.
159
+ * @param validationOptions - Optional options used for schema preparation.
160
+ */
161
+ setRemoteReference(uri, schema, validationOptions) {
162
+ const _schema = prepareRemoteSchema(schema, uri, validationOptions, this.options.maxRecursionDepth);
163
+ this.scache.cacheSchemaByUri(uri, _schema);
164
+ }
165
+ /**
166
+ * Extract unresolvable `$ref` URIs from a validation error.
167
+ * @param err - A `ValidateError` from a failed validation.
168
+ * @returns An array of unresolvable reference URIs.
169
+ */
170
+ getMissingReferences(err) {
171
+ if (!err) return [];
172
+ const details = err.details || [];
173
+ const missingRefs = [];
174
+ function collect(details) {
175
+ for (const detail of details) {
176
+ if (detail.code === "UNRESOLVABLE_REFERENCE" || detail.code === "SCHEMA_NOT_REACHABLE") missingRefs.push(detail.params[0]);
177
+ if (detail.inner) collect(detail.inner);
178
+ }
179
+ }
180
+ collect(details);
181
+ return missingRefs;
182
+ }
183
+ /**
184
+ * Extract unresolvable **remote** `$ref` URIs from a validation error (local fragment-only refs are excluded).
185
+ * @param err - A `ValidateError` from a failed validation.
186
+ * @returns An array of remote reference base URIs.
187
+ */
188
+ getMissingRemoteReferences(err) {
189
+ const missingReferences = this.getMissingReferences(err);
190
+ const missingRemoteReferences = [];
191
+ for (const ref of missingReferences) {
192
+ const remoteReference = getRemotePath(ref);
193
+ if (remoteReference && !missingRemoteReferences.includes(remoteReference)) missingRemoteReferences.push(remoteReference);
194
+ }
195
+ return missingRemoteReferences;
196
+ }
197
+ /**
198
+ * Resolve a previously compiled schema by its `$id` / `id`, cleaning up internal bookkeeping properties
199
+ * and inlining resolved `$ref` targets.
200
+ * @param schemaId - The schema identifier to look up.
201
+ * @returns A clean, resolved copy of the schema, or `undefined` if not found.
202
+ */
203
+ getResolvedSchema(schemaId) {
204
+ const report = new Report(this.options);
205
+ const schema = this.scache.getSchemaByUri(report, schemaId);
206
+ if (!schema) return void 0;
207
+ const clonedSchema = deepClone(schema, this.options.maxRecursionDepth);
208
+ const visited = /* @__PURE__ */ new WeakSet();
209
+ const cleanup = function(schema) {
210
+ let key;
211
+ const typeOf = whatIs(schema);
212
+ if (typeOf !== "object" && typeOf !== "array") return;
213
+ if (visited.has(schema)) return;
214
+ visited.add(schema);
215
+ if (schema.$ref && schema.__$refResolved) {
216
+ const from = schema.__$refResolved;
217
+ const to = schema;
218
+ delete schema.$ref;
219
+ delete schema.__$refResolved;
220
+ for (key in from) copyProp(from, to, key);
221
+ }
222
+ for (key in schema) if (Object.hasOwn(schema, key)) if (isInternalKey(key)) delete schema[key];
223
+ else cleanup(schema[key]);
224
+ };
225
+ cleanup(clonedSchema);
226
+ return clonedSchema;
227
+ }
228
+ };
229
+ //#endregion
230
+ export { FACTORY_TOKEN, ZSchemaBase };
@@ -0,0 +1,35 @@
1
+ import { FormatValidatorFn } from "./format-validators.mjs";
2
+ import { Report } from "./report.mjs";
3
+ import { JsonSchemaVersion } from "./json-schema-versions.mjs";
4
+
5
+ //#region src/z-schema-options.d.ts
6
+ interface ZSchemaOptions {
7
+ version?: JsonSchemaVersion | 'none';
8
+ asyncTimeout?: number;
9
+ forceAdditional?: boolean;
10
+ assumeAdditional?: boolean | string[];
11
+ enumCaseInsensitiveComparison?: boolean;
12
+ forceItems?: boolean;
13
+ forceMinItems?: boolean;
14
+ forceMaxItems?: boolean;
15
+ forceMinLength?: boolean;
16
+ forceMaxLength?: boolean;
17
+ forceProperties?: boolean;
18
+ ignoreUnresolvableReferences?: boolean;
19
+ noExtraKeywords?: boolean;
20
+ noTypeless?: boolean;
21
+ noEmptyStrings?: boolean;
22
+ noEmptyArrays?: boolean;
23
+ strictUris?: boolean;
24
+ strictMode?: boolean;
25
+ reportPathAsArray?: boolean;
26
+ breakOnFirstError?: boolean;
27
+ pedanticCheck?: boolean;
28
+ ignoreUnknownFormats?: boolean;
29
+ formatAssertions?: boolean | null;
30
+ customValidator?: (report: Report, schema: unknown, json: unknown) => void;
31
+ customFormats?: Record<string, FormatValidatorFn | null>;
32
+ maxRecursionDepth?: number;
33
+ }
34
+ //#endregion
35
+ export { ZSchemaOptions };
@@ -0,0 +1,55 @@
1
+ import { MAX_ASYNC_TIMEOUT } from "./utils/constants.mjs";
2
+ import { shallowClone } from "./utils/clone.mjs";
3
+ import { CURRENT_DEFAULT_SCHEMA_VERSION } from "./json-schema-versions.mjs";
4
+ //#region src/z-schema-options.ts
5
+ const defaultOptions = {
6
+ version: CURRENT_DEFAULT_SCHEMA_VERSION,
7
+ asyncTimeout: 2e3,
8
+ forceAdditional: false,
9
+ assumeAdditional: false,
10
+ enumCaseInsensitiveComparison: false,
11
+ forceItems: false,
12
+ forceMinItems: false,
13
+ forceMaxItems: false,
14
+ forceMinLength: false,
15
+ forceMaxLength: false,
16
+ forceProperties: false,
17
+ ignoreUnresolvableReferences: false,
18
+ noExtraKeywords: false,
19
+ noTypeless: false,
20
+ noEmptyStrings: false,
21
+ noEmptyArrays: false,
22
+ strictUris: false,
23
+ strictMode: false,
24
+ reportPathAsArray: false,
25
+ breakOnFirstError: false,
26
+ pedanticCheck: false,
27
+ ignoreUnknownFormats: false,
28
+ formatAssertions: null,
29
+ customValidator: null,
30
+ maxRecursionDepth: 100
31
+ };
32
+ const normalizeOptions = (options) => {
33
+ let normalized;
34
+ if (typeof options === "object") {
35
+ let keys = Object.keys(options);
36
+ for (const key of keys) if (defaultOptions[key] === void 0) throw new Error("Unexpected option passed to constructor: " + key);
37
+ keys = Object.keys(defaultOptions);
38
+ for (const key of keys) if (options[key] === void 0) options[key] = shallowClone(defaultOptions[key]);
39
+ normalized = options;
40
+ } else normalized = shallowClone(defaultOptions);
41
+ if (normalized.asyncTimeout != null) normalized.asyncTimeout = Math.min(Math.max(normalized.asyncTimeout, 0), MAX_ASYNC_TIMEOUT);
42
+ if (normalized.strictMode === true) {
43
+ normalized.forceAdditional = true;
44
+ normalized.forceItems = true;
45
+ normalized.forceMaxLength = true;
46
+ normalized.forceProperties = true;
47
+ normalized.noExtraKeywords = true;
48
+ normalized.noTypeless = true;
49
+ normalized.noEmptyStrings = true;
50
+ normalized.noEmptyArrays = true;
51
+ }
52
+ return normalized;
53
+ };
54
+ //#endregion
55
+ export { defaultOptions, normalizeOptions };
@@ -0,0 +1,6 @@
1
+ import { JsonSchema } from "./json-schema-versions.mjs";
2
+
3
+ //#region src/z-schema-reader.d.ts
4
+ type SchemaReader = (uri: string) => JsonSchema;
5
+ //#endregion
6
+ export { SchemaReader };
@@ -0,0 +1,10 @@
1
+ //#region src/z-schema-reader.ts
2
+ let _schemaReader;
3
+ function getSchemaReader() {
4
+ return _schemaReader;
5
+ }
6
+ function setSchemaReader(schemaReader) {
7
+ _schemaReader = schemaReader;
8
+ }
9
+ //#endregion
10
+ export { getSchemaReader, setSchemaReader };
@@ -0,0 +1,67 @@
1
+ import { normalizeOptions } from "./z-schema-options.mjs";
2
+ import { SchemaCache } from "./schema-cache.mjs";
3
+ import draft_04_schema_default from "./schemas/draft-04-schema.mjs";
4
+ import draft_06_schema_default from "./schemas/draft-06-schema.mjs";
5
+ import draft_07_schema_default from "./schemas/draft-07-schema.mjs";
6
+ import draft_2019_09_meta_applicator_default from "./schemas/draft-2019-09-meta-applicator.mjs";
7
+ import draft_2019_09_meta_content_default from "./schemas/draft-2019-09-meta-content.mjs";
8
+ import draft_2019_09_meta_core_default from "./schemas/draft-2019-09-meta-core.mjs";
9
+ import draft_2019_09_meta_format_default from "./schemas/draft-2019-09-meta-format.mjs";
10
+ import draft_2019_09_meta_meta_data_default from "./schemas/draft-2019-09-meta-meta-data.mjs";
11
+ import draft_2019_09_meta_validation_default from "./schemas/draft-2019-09-meta-validation.mjs";
12
+ import draft_2019_09_schema_default from "./schemas/draft-2019-09-schema.mjs";
13
+ import draft_2020_12_meta_applicator_default from "./schemas/draft-2020-12-meta-applicator.mjs";
14
+ import draft_2020_12_meta_content_default from "./schemas/draft-2020-12-meta-content.mjs";
15
+ import draft_2020_12_meta_core_default from "./schemas/draft-2020-12-meta-core.mjs";
16
+ import draft_2020_12_meta_format_annotation_default from "./schemas/draft-2020-12-meta-format-annotation.mjs";
17
+ import draft_2020_12_meta_format_assertion_default from "./schemas/draft-2020-12-meta-format-assertion.mjs";
18
+ import draft_2020_12_meta_meta_data_default from "./schemas/draft-2020-12-meta-meta-data.mjs";
19
+ import draft_2020_12_meta_unevaluated_default from "./schemas/draft-2020-12-meta-unevaluated.mjs";
20
+ import draft_2020_12_meta_validation_default from "./schemas/draft-2020-12-meta-validation.mjs";
21
+ import draft_2020_12_schema_default from "./schemas/draft-2020-12-schema.mjs";
22
+ //#region src/z-schema-versions.ts
23
+ const Draft4Schema = draft_04_schema_default;
24
+ const Draft6Schema = draft_06_schema_default;
25
+ const Draft7Schema = draft_07_schema_default;
26
+ const Draft201909Schema = draft_2019_09_schema_default;
27
+ const Draft201909MetaApplicator = draft_2019_09_meta_applicator_default;
28
+ const Draft201909MetaContent = draft_2019_09_meta_content_default;
29
+ const Draft201909MetaCore = draft_2019_09_meta_core_default;
30
+ const Draft201909MetaFormat = draft_2019_09_meta_format_default;
31
+ const Draft201909MetaMetaData = draft_2019_09_meta_meta_data_default;
32
+ const Draft201909MetaValidation = draft_2019_09_meta_validation_default;
33
+ const Draft202012Schema = draft_2020_12_schema_default;
34
+ const Draft202012MetaApplicator = draft_2020_12_meta_applicator_default;
35
+ const Draft202012MetaContent = draft_2020_12_meta_content_default;
36
+ const Draft202012MetaCore = draft_2020_12_meta_core_default;
37
+ const Draft202012MetaFormatAnnotation = draft_2020_12_meta_format_annotation_default;
38
+ const Draft202012MetaFormatAssertion = draft_2020_12_meta_format_assertion_default;
39
+ const Draft202012MetaMetaData = draft_2020_12_meta_meta_data_default;
40
+ const Draft202012MetaUnevaluated = draft_2020_12_meta_unevaluated_default;
41
+ const Draft202012MetaValidation = draft_2020_12_meta_validation_default;
42
+ const registerRemoteReference = (uri, schema, validationOptions) => {
43
+ const preparedSchema = schema;
44
+ if (!preparedSchema.id) preparedSchema.id = uri;
45
+ if (validationOptions) preparedSchema.__$validationOptions = normalizeOptions(validationOptions);
46
+ SchemaCache.cacheSchemaByUri(uri, preparedSchema);
47
+ };
48
+ registerRemoteReference("http://json-schema.org/draft-04/schema", Draft4Schema, { version: "none" });
49
+ registerRemoteReference("http://json-schema.org/draft-06/schema", Draft6Schema, { version: "none" });
50
+ registerRemoteReference("http://json-schema.org/draft-07/schema", Draft7Schema, { version: "none" });
51
+ registerRemoteReference("https://json-schema.org/draft/2019-09/schema", Draft201909Schema, { version: "none" });
52
+ registerRemoteReference("https://json-schema.org/draft/2019-09/meta/applicator", Draft201909MetaApplicator, { version: "none" });
53
+ registerRemoteReference("https://json-schema.org/draft/2019-09/meta/content", Draft201909MetaContent, { version: "none" });
54
+ registerRemoteReference("https://json-schema.org/draft/2019-09/meta/core", Draft201909MetaCore, { version: "none" });
55
+ registerRemoteReference("https://json-schema.org/draft/2019-09/meta/format", Draft201909MetaFormat, { version: "none" });
56
+ registerRemoteReference("https://json-schema.org/draft/2019-09/meta/meta-data", Draft201909MetaMetaData, { version: "none" });
57
+ registerRemoteReference("https://json-schema.org/draft/2019-09/meta/validation", Draft201909MetaValidation, { version: "none" });
58
+ registerRemoteReference("https://json-schema.org/draft/2020-12/schema", Draft202012Schema, { version: "none" });
59
+ registerRemoteReference("https://json-schema.org/draft/2020-12/meta/applicator", Draft202012MetaApplicator, { version: "none" });
60
+ registerRemoteReference("https://json-schema.org/draft/2020-12/meta/content", Draft202012MetaContent, { version: "none" });
61
+ registerRemoteReference("https://json-schema.org/draft/2020-12/meta/core", Draft202012MetaCore, { version: "none" });
62
+ registerRemoteReference("https://json-schema.org/draft/2020-12/meta/format-annotation", Draft202012MetaFormatAnnotation, { version: "none" });
63
+ registerRemoteReference("https://json-schema.org/draft/2020-12/meta/format-assertion", Draft202012MetaFormatAssertion, { version: "none" });
64
+ registerRemoteReference("https://json-schema.org/draft/2020-12/meta/meta-data", Draft202012MetaMetaData, { version: "none" });
65
+ registerRemoteReference("https://json-schema.org/draft/2020-12/meta/unevaluated", Draft202012MetaUnevaluated, { version: "none" });
66
+ registerRemoteReference("https://json-schema.org/draft/2020-12/meta/validation", Draft202012MetaValidation, { version: "none" });
67
+ //#endregion
@@ -0,0 +1,193 @@
1
+ import { FormatValidatorFn } from "./format-validators.mjs";
2
+ import { ZSchemaOptions } from "./z-schema-options.mjs";
3
+ import { ValidateOptions, ValidateResponse, ZSchemaBase } from "./z-schema-base.mjs";
4
+ import { JsonSchema } from "./json-schema-versions.mjs";
5
+ import { SchemaReader } from "./z-schema-reader.mjs";
6
+
7
+ //#region src/z-schema.d.ts
8
+ declare class ZSchema extends ZSchemaBase {
9
+ /** @internal Use ZSchema.create() instead. */
10
+ constructor(options: ZSchemaOptions | undefined, token: symbol);
11
+ /**
12
+ * Register a global format validator available to all instances.
13
+ * @param name - The format name (e.g. `'email'`, `'date'`).
14
+ * @param validatorFunction - A sync or async function `(value: unknown) => boolean | Promise<boolean>`.
15
+ */
16
+ static registerFormat(name: string, validatorFunction: FormatValidatorFn): void;
17
+ /**
18
+ * Remove a globally registered format validator.
19
+ * @param name - The format name to unregister.
20
+ */
21
+ static unregisterFormat(name: string): void;
22
+ /** Returns the names of all globally registered format validators. */
23
+ static getRegisteredFormats(): string[];
24
+ /** Returns a deep clone of the default options. */
25
+ static getDefaultOptions(): ZSchemaOptions;
26
+ /**
27
+ * Register a remote schema in the global cache so any instance can resolve `$ref` to it.
28
+ * @param uri - The URI the schema will be known by.
29
+ * @param schema - The schema object or JSON string.
30
+ * @param validationOptions - Optional options used for schema preparation.
31
+ */
32
+ static setRemoteReference(uri: string, schema: string | JsonSchema, validationOptions?: ZSchemaOptions): void;
33
+ /** Returns the current global schema reader, or `undefined` if none is set. */
34
+ static getSchemaReader(): SchemaReader | undefined;
35
+ /**
36
+ * Set a global schema reader function used to resolve remote `$ref` URIs.
37
+ * @param schemaReader - A function `(uri: string) => JsonSchema | undefined`, or `undefined` to clear.
38
+ */
39
+ static setSchemaReader(schemaReader: SchemaReader | undefined): void;
40
+ static schemaSymbol: symbol;
41
+ static jsonSymbol: symbol;
42
+ /**
43
+ * Create a validator instance.
44
+ *
45
+ * The returned type depends on the `async` and `safe` options:
46
+ * - `{}` → `ZSchema` — `validate()` returns `true` or throws.
47
+ * - `{ safe: true }` → `ZSchemaSafe` — `validate()` returns `{ valid, err? }`.
48
+ * - `{ async: true }` → `ZSchemaAsync` — `validate()` returns `Promise<true>` or rejects.
49
+ * - `{ async: true, safe: true }` → `ZSchemaAsyncSafe` — `validate()` returns `Promise<{ valid, err? }>`.
50
+ *
51
+ * @param options - Validator configuration. See `ZSchemaOptions` for all available settings.
52
+ * @returns A validator instance of the appropriate variant.
53
+ *
54
+ * @example
55
+ * ```ts
56
+ * const validator = ZSchema.create();
57
+ * validator.validate(data, schema); // throws on error
58
+ *
59
+ * const safe = ZSchema.create({ safe: true });
60
+ * const result = safe.validate(data, schema); // { valid, err? }
61
+ * ```
62
+ */
63
+ static create(options: ZSchemaOptions & {
64
+ async: true;
65
+ safe: true;
66
+ }): ZSchemaAsyncSafe;
67
+ static create(options: ZSchemaOptions & {
68
+ async: true;
69
+ }): ZSchemaAsync;
70
+ static create(options: ZSchemaOptions & {
71
+ safe: true;
72
+ }): ZSchemaSafe;
73
+ static create(options?: ZSchemaOptions): ZSchema;
74
+ /**
75
+ * Validate JSON data against a schema.
76
+ * @param json - The data to validate.
77
+ * @param schema - A JSON Schema object or a schema id string (previously registered via `validateSchema`).
78
+ * @param options - Per-call options (`schemaPath`, `includeErrors`, `excludeErrors`).
79
+ * @returns `true` if valid.
80
+ * @throws {@link ValidateError} if validation fails, with a `details` array of structured errors.
81
+ */
82
+ validate(json: unknown, schema: JsonSchema | string, options?: ValidateOptions): true;
83
+ /**
84
+ * Validate JSON data against a schema, returning a result object instead of throwing.
85
+ * @param json - The data to validate.
86
+ * @param schema - A JSON Schema object or a schema id string.
87
+ * @param options - Per-call options.
88
+ * @returns `{ valid: true }` on success, or `{ valid: false, err: ValidateError }` on failure.
89
+ */
90
+ validateSafe(json: unknown, schema: JsonSchema | string, options?: ValidateOptions): ValidateResponse;
91
+ /**
92
+ * Validate JSON data against a schema asynchronously (supports async format validators).
93
+ * @param json - The data to validate.
94
+ * @param schema - A JSON Schema object or a schema id string.
95
+ * @param options - Per-call options.
96
+ * @returns A promise that resolves to `true` if valid.
97
+ * @throws {@link ValidateError} if validation fails (the promise rejects).
98
+ */
99
+ validateAsync(json: unknown, schema: JsonSchema | string, options?: ValidateOptions): Promise<true>;
100
+ /**
101
+ * Validate JSON data against a schema asynchronously, returning a result object.
102
+ * The promise always resolves (never rejects).
103
+ * @param json - The data to validate.
104
+ * @param schema - A JSON Schema object or a schema id string.
105
+ * @param options - Per-call options.
106
+ * @returns A promise resolving to `{ valid: true }` or `{ valid: false, err: ValidateError }`.
107
+ */
108
+ validateAsyncSafe(json: unknown, schema: JsonSchema | string, options?: ValidateOptions): Promise<ValidateResponse>;
109
+ /**
110
+ * Validate one or more JSON Schemas, compiling and caching them for later use with `validate()`.
111
+ * @param schemaOrArr - A single schema or an array of schemas (for cross-referencing).
112
+ * @returns `true` if all schemas are valid.
113
+ * @throws {@link ValidateError} if any schema is invalid.
114
+ */
115
+ validateSchema(schemaOrArr: JsonSchema | JsonSchema[]): true;
116
+ /**
117
+ * Validate one or more JSON Schemas, returning a result object instead of throwing.
118
+ * @param schemaOrArr - A single schema or an array of schemas.
119
+ * @returns `{ valid: true }` on success, or `{ valid: false, err: ValidateError }` on failure.
120
+ */
121
+ validateSchemaSafe(schemaOrArr: JsonSchema | JsonSchema[]): ValidateResponse;
122
+ }
123
+ /**
124
+ * Synchronous safe validator — `validate()` returns `{ valid, err? }` instead of throwing.
125
+ * Created via `ZSchema.create({ safe: true })`.
126
+ */
127
+ declare class ZSchemaSafe extends ZSchemaBase {
128
+ /** @internal Use ZSchema.create() instead. */
129
+ constructor(options: ZSchemaOptions | undefined, token: symbol);
130
+ /**
131
+ * Validate JSON data against a schema.
132
+ * @param json - The data to validate.
133
+ * @param schema - A JSON Schema object or a schema id string.
134
+ * @param options - Per-call options.
135
+ * @returns `{ valid: true }` on success, or `{ valid: false, err: ValidateError }` on failure.
136
+ */
137
+ validate(json: unknown, schema: JsonSchema | string, options?: ValidateOptions): ValidateResponse;
138
+ /**
139
+ * Validate one or more JSON Schemas.
140
+ * @param schemaOrArr - A single schema or an array of schemas.
141
+ * @returns `{ valid: true }` on success, or `{ valid: false, err: ValidateError }` on failure.
142
+ */
143
+ validateSchema(schemaOrArr: JsonSchema | JsonSchema[]): ValidateResponse;
144
+ }
145
+ /**
146
+ * Asynchronous throw validator — `validate()` returns `Promise<true>` or rejects.
147
+ * Created via `ZSchema.create({ async: true })`.
148
+ */
149
+ declare class ZSchemaAsync extends ZSchemaBase {
150
+ /** @internal Use ZSchema.create() instead. */
151
+ constructor(options: ZSchemaOptions | undefined, token: symbol);
152
+ /**
153
+ * Validate JSON data against a schema asynchronously.
154
+ * @param json - The data to validate.
155
+ * @param schema - A JSON Schema object or a schema id string.
156
+ * @param options - Per-call options.
157
+ * @returns A promise that resolves to `true` if valid.
158
+ * @throws {@link ValidateError} if validation fails (the promise rejects).
159
+ */
160
+ validate(json: unknown, schema: JsonSchema | string, options?: ValidateOptions): Promise<true>;
161
+ /**
162
+ * Validate one or more JSON Schemas (synchronous, throws on error).
163
+ * @param schemaOrArr - A single schema or an array of schemas.
164
+ * @returns `true` if all schemas are valid.
165
+ * @throws {@link ValidateError} if any schema is invalid.
166
+ */
167
+ validateSchema(schemaOrArr: JsonSchema | JsonSchema[]): true;
168
+ }
169
+ /**
170
+ * Asynchronous safe validator — `validate()` returns `Promise<{ valid, err? }>` (never rejects).
171
+ * Created via `ZSchema.create({ async: true, safe: true })`.
172
+ */
173
+ declare class ZSchemaAsyncSafe extends ZSchemaBase {
174
+ /** @internal Use ZSchema.create() instead. */
175
+ constructor(options: ZSchemaOptions | undefined, token: symbol);
176
+ /**
177
+ * Validate JSON data against a schema asynchronously.
178
+ * The promise always resolves (never rejects).
179
+ * @param json - The data to validate.
180
+ * @param schema - A JSON Schema object or a schema id string.
181
+ * @param options - Per-call options.
182
+ * @returns A promise resolving to `{ valid: true }` or `{ valid: false, err: ValidateError }`.
183
+ */
184
+ validate(json: unknown, schema: JsonSchema | string, options?: ValidateOptions): Promise<ValidateResponse>;
185
+ /**
186
+ * Validate one or more JSON Schemas.
187
+ * @param schemaOrArr - A single schema or an array of schemas.
188
+ * @returns `{ valid: true }` on success, or `{ valid: false, err: ValidateError }` on failure.
189
+ */
190
+ validateSchema(schemaOrArr: JsonSchema | JsonSchema[]): ValidateResponse;
191
+ }
192
+ //#endregion
193
+ export { ZSchema, ZSchemaAsync, ZSchemaAsyncSafe, ZSchemaSafe };