@sinclair/typebox 0.24.21 → 0.24.24

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/value/check.js CHANGED
@@ -27,8 +27,15 @@ THE SOFTWARE.
27
27
 
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.ValueCheck = void 0;
31
- const Types = require("../typebox");
30
+ exports.ValueCheck = exports.ValueCheckInvalidTypeError = void 0;
31
+ const index_1 = require("../guard/index");
32
+ class ValueCheckInvalidTypeError extends Error {
33
+ constructor(schema) {
34
+ super('ValueCheck: Invalid type');
35
+ this.schema = schema;
36
+ }
37
+ }
38
+ exports.ValueCheckInvalidTypeError = ValueCheckInvalidTypeError;
32
39
  var ValueCheck;
33
40
  (function (ValueCheck) {
34
41
  function Any(schema, references, value) {
@@ -244,53 +251,72 @@ var ValueCheck;
244
251
  return value === null;
245
252
  }
246
253
  function Visit(schema, references, value) {
247
- const anyReferences = schema.$id === undefined ? references : [schema, ...references];
248
- const anySchema = schema;
249
- switch (anySchema[Types.Kind]) {
250
- case 'Any':
251
- return Any(anySchema, anyReferences, value);
252
- case 'Array':
253
- return Array(anySchema, anyReferences, value);
254
- case 'Boolean':
255
- return Boolean(anySchema, anyReferences, value);
256
- case 'Constructor':
257
- return Constructor(anySchema, anyReferences, value);
258
- case 'Function':
259
- return Function(anySchema, anyReferences, value);
260
- case 'Integer':
261
- return Integer(anySchema, anyReferences, value);
262
- case 'Literal':
263
- return Literal(anySchema, anyReferences, value);
264
- case 'Null':
265
- return Null(anySchema, anyReferences, value);
266
- case 'Number':
267
- return Number(anySchema, anyReferences, value);
268
- case 'Object':
269
- return Object(anySchema, anyReferences, value);
270
- case 'Promise':
271
- return Promise(anySchema, anyReferences, value);
272
- case 'Record':
273
- return Record(anySchema, anyReferences, value);
274
- case 'Ref':
275
- return Ref(anySchema, anyReferences, value);
276
- case 'Self':
277
- return Self(anySchema, anyReferences, value);
278
- case 'String':
279
- return String(anySchema, anyReferences, value);
280
- case 'Tuple':
281
- return Tuple(anySchema, anyReferences, value);
282
- case 'Undefined':
283
- return Undefined(anySchema, anyReferences, value);
284
- case 'Union':
285
- return Union(anySchema, anyReferences, value);
286
- case 'Uint8Array':
287
- return Uint8Array(anySchema, anyReferences, value);
288
- case 'Unknown':
289
- return Unknown(anySchema, anyReferences, value);
290
- case 'Void':
291
- return Void(anySchema, anyReferences, value);
292
- default:
293
- throw new Error(`ValueCheck: Unknown schema kind '${schema[Types.Kind]}'`);
254
+ const refs = schema.$id === undefined ? references : [schema, ...references];
255
+ if (index_1.TypeGuard.TAny(schema)) {
256
+ return Any(schema, refs, value);
257
+ }
258
+ else if (index_1.TypeGuard.TArray(schema)) {
259
+ return Array(schema, refs, value);
260
+ }
261
+ else if (index_1.TypeGuard.TBoolean(schema)) {
262
+ return Boolean(schema, refs, value);
263
+ }
264
+ else if (index_1.TypeGuard.TConstructor(schema)) {
265
+ return Constructor(schema, refs, value);
266
+ }
267
+ else if (index_1.TypeGuard.TFunction(schema)) {
268
+ return Function(schema, refs, value);
269
+ }
270
+ else if (index_1.TypeGuard.TInteger(schema)) {
271
+ return Integer(schema, refs, value);
272
+ }
273
+ else if (index_1.TypeGuard.TLiteral(schema)) {
274
+ return Literal(schema, refs, value);
275
+ }
276
+ else if (index_1.TypeGuard.TNull(schema)) {
277
+ return Null(schema, refs, value);
278
+ }
279
+ else if (index_1.TypeGuard.TNumber(schema)) {
280
+ return Number(schema, refs, value);
281
+ }
282
+ else if (index_1.TypeGuard.TObject(schema)) {
283
+ return Object(schema, refs, value);
284
+ }
285
+ else if (index_1.TypeGuard.TPromise(schema)) {
286
+ return Promise(schema, refs, value);
287
+ }
288
+ else if (index_1.TypeGuard.TRecord(schema)) {
289
+ return Record(schema, refs, value);
290
+ }
291
+ else if (index_1.TypeGuard.TRef(schema)) {
292
+ return Ref(schema, refs, value);
293
+ }
294
+ else if (index_1.TypeGuard.TSelf(schema)) {
295
+ return Self(schema, refs, value);
296
+ }
297
+ else if (index_1.TypeGuard.TString(schema)) {
298
+ return String(schema, refs, value);
299
+ }
300
+ else if (index_1.TypeGuard.TTuple(schema)) {
301
+ return Tuple(schema, refs, value);
302
+ }
303
+ else if (index_1.TypeGuard.TUndefined(schema)) {
304
+ return Undefined(schema, refs, value);
305
+ }
306
+ else if (index_1.TypeGuard.TUnion(schema)) {
307
+ return Union(schema, refs, value);
308
+ }
309
+ else if (index_1.TypeGuard.TUint8Array(schema)) {
310
+ return Uint8Array(schema, refs, value);
311
+ }
312
+ else if (index_1.TypeGuard.TUnknown(schema)) {
313
+ return Unknown(schema, refs, value);
314
+ }
315
+ else if (index_1.TypeGuard.TVoid(schema)) {
316
+ return Void(schema, refs, value);
317
+ }
318
+ else {
319
+ throw new ValueCheckInvalidTypeError(schema);
294
320
  }
295
321
  }
296
322
  // -------------------------------------------------------------------------
package/value/create.d.ts CHANGED
@@ -1,4 +1,8 @@
1
1
  import * as Types from '../typebox';
2
+ export declare class ValueCreateInvalidTypeError extends Error {
3
+ readonly schema: Types.TSchema;
4
+ constructor(schema: Types.TSchema);
5
+ }
2
6
  export declare namespace ValueCreate {
3
7
  /** Creates a value from the given schema. If the schema specifies a default value, then that value is returned. */
4
8
  function Visit<T extends Types.TSchema>(schema: T, references: Types.TSchema[]): Types.Static<T>;
package/value/create.js CHANGED
@@ -27,8 +27,15 @@ THE SOFTWARE.
27
27
 
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.ValueCreate = void 0;
31
- const Types = require("../typebox");
30
+ exports.ValueCreate = exports.ValueCreateInvalidTypeError = void 0;
31
+ const index_1 = require("../guard/index");
32
+ class ValueCreateInvalidTypeError extends Error {
33
+ constructor(schema) {
34
+ super('ValueCreate: Invalid type');
35
+ this.schema = schema;
36
+ }
37
+ }
38
+ exports.ValueCreateInvalidTypeError = ValueCreateInvalidTypeError;
32
39
  var ValueCreate;
33
40
  (function (ValueCreate) {
34
41
  function Any(schema, references) {
@@ -269,57 +276,72 @@ var ValueCreate;
269
276
  }
270
277
  /** Creates a value from the given schema. If the schema specifies a default value, then that value is returned. */
271
278
  function Visit(schema, references) {
272
- const anyReferences = schema.$id === undefined ? references : [schema, ...references];
273
- const anySchema = schema;
274
- switch (anySchema[Types.Kind]) {
275
- case 'Any':
276
- return Any(anySchema, anyReferences);
277
- case 'Array':
278
- return Array(anySchema, anyReferences);
279
- case 'Boolean':
280
- return Boolean(anySchema, anyReferences);
281
- case 'Constructor':
282
- return Constructor(anySchema, anyReferences);
283
- case 'Enum':
284
- return Enum(anySchema, anyReferences);
285
- case 'Function':
286
- return Function(anySchema, anyReferences);
287
- case 'Integer':
288
- return Integer(anySchema, anyReferences);
289
- case 'Literal':
290
- return Literal(anySchema, anyReferences);
291
- case 'Null':
292
- return Null(anySchema, anyReferences);
293
- case 'Number':
294
- return Number(anySchema, anyReferences);
295
- case 'Object':
296
- return Object(anySchema, anyReferences);
297
- case 'Promise':
298
- return Promise(anySchema, anyReferences);
299
- case 'Record':
300
- return Record(anySchema, anyReferences);
301
- case 'Rec':
302
- return Recursive(anySchema, anyReferences);
303
- case 'Ref':
304
- return Ref(anySchema, anyReferences);
305
- case 'Self':
306
- return Self(anySchema, anyReferences);
307
- case 'String':
308
- return String(anySchema, anyReferences);
309
- case 'Tuple':
310
- return Tuple(anySchema, anyReferences);
311
- case 'Undefined':
312
- return Undefined(anySchema, anyReferences);
313
- case 'Union':
314
- return Union(anySchema, anyReferences);
315
- case 'Uint8Array':
316
- return Uint8Array(anySchema, anyReferences);
317
- case 'Unknown':
318
- return Unknown(anySchema, anyReferences);
319
- case 'Void':
320
- return Void(anySchema, anyReferences);
321
- default:
322
- throw new Error(`ValueCreate.Visit: Unknown schema kind '${schema[Types.Kind]}'`);
279
+ const refs = schema.$id === undefined ? references : [schema, ...references];
280
+ if (index_1.TypeGuard.TAny(schema)) {
281
+ return Any(schema, refs);
282
+ }
283
+ else if (index_1.TypeGuard.TArray(schema)) {
284
+ return Array(schema, refs);
285
+ }
286
+ else if (index_1.TypeGuard.TBoolean(schema)) {
287
+ return Boolean(schema, refs);
288
+ }
289
+ else if (index_1.TypeGuard.TConstructor(schema)) {
290
+ return Constructor(schema, refs);
291
+ }
292
+ else if (index_1.TypeGuard.TFunction(schema)) {
293
+ return Function(schema, refs);
294
+ }
295
+ else if (index_1.TypeGuard.TInteger(schema)) {
296
+ return Integer(schema, refs);
297
+ }
298
+ else if (index_1.TypeGuard.TLiteral(schema)) {
299
+ return Literal(schema, refs);
300
+ }
301
+ else if (index_1.TypeGuard.TNull(schema)) {
302
+ return Null(schema, refs);
303
+ }
304
+ else if (index_1.TypeGuard.TNumber(schema)) {
305
+ return Number(schema, refs);
306
+ }
307
+ else if (index_1.TypeGuard.TObject(schema)) {
308
+ return Object(schema, refs);
309
+ }
310
+ else if (index_1.TypeGuard.TPromise(schema)) {
311
+ return Promise(schema, refs);
312
+ }
313
+ else if (index_1.TypeGuard.TRecord(schema)) {
314
+ return Record(schema, refs);
315
+ }
316
+ else if (index_1.TypeGuard.TRef(schema)) {
317
+ return Ref(schema, refs);
318
+ }
319
+ else if (index_1.TypeGuard.TSelf(schema)) {
320
+ return Self(schema, refs);
321
+ }
322
+ else if (index_1.TypeGuard.TString(schema)) {
323
+ return String(schema, refs);
324
+ }
325
+ else if (index_1.TypeGuard.TTuple(schema)) {
326
+ return Tuple(schema, refs);
327
+ }
328
+ else if (index_1.TypeGuard.TUndefined(schema)) {
329
+ return Undefined(schema, refs);
330
+ }
331
+ else if (index_1.TypeGuard.TUnion(schema)) {
332
+ return Union(schema, refs);
333
+ }
334
+ else if (index_1.TypeGuard.TUint8Array(schema)) {
335
+ return Uint8Array(schema, refs);
336
+ }
337
+ else if (index_1.TypeGuard.TUnknown(schema)) {
338
+ return Unknown(schema, refs);
339
+ }
340
+ else if (index_1.TypeGuard.TVoid(schema)) {
341
+ return Void(schema, refs);
342
+ }
343
+ else {
344
+ throw new ValueCreateInvalidTypeError(schema);
323
345
  }
324
346
  }
325
347
  ValueCreate.Visit = Visit;
package/value/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export type { ValueError } from './errors';
1
+ export { ValueError, ValueErrorType } from '../errors/errors';
2
2
  export * from './value';
package/value/index.js CHANGED
@@ -41,4 +41,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
41
41
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
42
42
  };
43
43
  Object.defineProperty(exports, "__esModule", { value: true });
44
+ exports.ValueErrorType = void 0;
45
+ var errors_1 = require("../errors/errors");
46
+ Object.defineProperty(exports, "ValueErrorType", { enumerable: true, get: function () { return errors_1.ValueErrorType; } });
44
47
  __exportStar(require("./value"), exports);
package/value/value.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as Types from '../typebox';
2
- import { ValueError } from './errors';
2
+ import { ValueError } from '../errors/index';
3
3
  /** Creates Values from TypeBox Types */
4
4
  export declare namespace Value {
5
5
  /** Creates a value from the given type */
package/value/value.js CHANGED
@@ -28,7 +28,7 @@ THE SOFTWARE.
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
30
  exports.Value = void 0;
31
- const errors_1 = require("./errors");
31
+ const index_1 = require("../errors/index");
32
32
  const cast_1 = require("./cast");
33
33
  const create_1 = require("./create");
34
34
  const check_1 = require("./check");
@@ -52,7 +52,7 @@ var Value;
52
52
  Value.Cast = Cast;
53
53
  function* Errors(...args) {
54
54
  const [schema, references, value] = args.length === 3 ? [args[0], args[1], args[2]] : [args[0], [], args[1]];
55
- yield* errors_1.ValueErrors.Errors(schema, references, value);
55
+ yield* index_1.ValueErrors.Errors(schema, references, value);
56
56
  }
57
57
  Value.Errors = Errors;
58
58
  })(Value = exports.Value || (exports.Value = {}));
package/value/errors.d.ts DELETED
@@ -1,10 +0,0 @@
1
- import * as Types from '../typebox';
2
- export interface ValueError {
3
- schema: Types.TSchema;
4
- path: string;
5
- value: unknown;
6
- message: string;
7
- }
8
- export declare namespace ValueErrors {
9
- function Errors<T extends Types.TSchema>(schema: T, references: Types.TSchema[], value: any): IterableIterator<ValueError>;
10
- }
package/value/errors.js DELETED
@@ -1,305 +0,0 @@
1
- "use strict";
2
- /*--------------------------------------------------------------------------
3
-
4
- @sinclair/typebox/value
5
-
6
- The MIT License (MIT)
7
-
8
- Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
9
-
10
- Permission is hereby granted, free of charge, to any person obtaining a copy
11
- of this software and associated documentation files (the "Software"), to deal
12
- in the Software without restriction, including without limitation the rights
13
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
- copies of the Software, and to permit persons to whom the Software is
15
- furnished to do so, subject to the following conditions:
16
-
17
- The above copyright notice and this permission notice shall be included in
18
- all copies or substantial portions of the Software.
19
-
20
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26
- THE SOFTWARE.
27
-
28
- ---------------------------------------------------------------------------*/
29
- Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.ValueErrors = void 0;
31
- const Types = require("../typebox");
32
- var ValueErrors;
33
- (function (ValueErrors) {
34
- function* Any(schema, references, path, value) { }
35
- function* Array(schema, references, path, value) {
36
- if (!globalThis.Array.isArray(value)) {
37
- return yield { schema, path, value, message: `Expected array` };
38
- }
39
- if (schema.minItems !== undefined && !(value.length >= schema.minItems)) {
40
- yield { schema, path, value, message: `Expected array length to be greater or equal to ${schema.minItems}` };
41
- }
42
- if (schema.maxItems !== undefined && !(value.length <= schema.maxItems)) {
43
- yield { schema, path, value, message: `Expected array length to be less or equal to ${schema.maxItems}` };
44
- }
45
- if (schema.uniqueItems === true && !(new Set(value).size === value.length)) {
46
- yield { schema, path, value, message: `Expected array elements to be unique` };
47
- }
48
- for (let i = 0; i < value.length; i++) {
49
- yield* Visit(schema.items, references, `${path}/${i}`, value[i]);
50
- }
51
- }
52
- function* Boolean(schema, references, path, value) {
53
- if (!(typeof value === 'boolean')) {
54
- return yield { schema, path, value, message: `Expected boolean` };
55
- }
56
- }
57
- function* Constructor(schema, references, path, value) {
58
- yield* Visit(schema.returns, references, path, value);
59
- }
60
- function* Function(schema, references, path, value) {
61
- if (!(typeof value === 'function')) {
62
- return yield { schema, path, value, message: `Expected function` };
63
- }
64
- }
65
- function* Integer(schema, references, path, value) {
66
- if (!(typeof value === 'number')) {
67
- return yield { schema, path, value, message: `Expected number` };
68
- }
69
- if (!globalThis.Number.isInteger(value)) {
70
- yield { schema, path, value, message: `Expected integer` };
71
- }
72
- if (schema.multipleOf && !(value % schema.multipleOf === 0)) {
73
- yield { schema, path, value, message: `Expected integer to be a multiple of ${schema.multipleOf}` };
74
- }
75
- if (schema.exclusiveMinimum && !(value > schema.exclusiveMinimum)) {
76
- yield { schema, path, value, message: `Expected integer to be greater than ${schema.exclusiveMinimum}` };
77
- }
78
- if (schema.exclusiveMaximum && !(value < schema.exclusiveMaximum)) {
79
- yield { schema, path, value, message: `Expected integer to be less than ${schema.exclusiveMaximum}` };
80
- }
81
- if (schema.minimum && !(value >= schema.minimum)) {
82
- yield { schema, path, value, message: `Expected integer to be greater or equal to ${schema.minimum}` };
83
- }
84
- if (schema.maximum && !(value <= schema.maximum)) {
85
- yield { schema, path, value, message: `Expected integer to be less or equal to ${schema.maximum}` };
86
- }
87
- }
88
- function* Literal(schema, references, path, value) {
89
- if (!(value === schema.const)) {
90
- const error = typeof schema.const === 'string' ? `'${schema.const}'` : schema.const;
91
- return yield { schema, path, value, message: `Expected ${error}` };
92
- }
93
- }
94
- function* Null(schema, references, path, value) {
95
- if (!(value === null)) {
96
- return yield { schema, path, value, message: `Expected null` };
97
- }
98
- }
99
- function* Number(schema, references, path, value) {
100
- if (!(typeof value === 'number')) {
101
- return yield { schema, path, value, message: `Expected number` };
102
- }
103
- if (schema.multipleOf && !(value % schema.multipleOf === 0)) {
104
- yield { schema, path, value, message: `Expected number to be a multiple of ${schema.multipleOf}` };
105
- }
106
- if (schema.exclusiveMinimum && !(value > schema.exclusiveMinimum)) {
107
- yield { schema, path, value, message: `Expected number to be greater than ${schema.exclusiveMinimum}` };
108
- }
109
- if (schema.exclusiveMaximum && !(value < schema.exclusiveMaximum)) {
110
- yield { schema, path, value, message: `Expected number to be less than ${schema.exclusiveMaximum}` };
111
- }
112
- if (schema.minimum && !(value >= schema.minimum)) {
113
- yield { schema, path, value, message: `Expected number to be greater or equal to ${schema.minimum}` };
114
- }
115
- if (schema.maximum && !(value <= schema.maximum)) {
116
- yield { schema, path, value, message: `Expected number to be less or equal to ${schema.maximum}` };
117
- }
118
- }
119
- function* Object(schema, references, path, value) {
120
- if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value))) {
121
- return yield { schema, path, value, message: `Expected object` };
122
- }
123
- if (schema.minProperties !== undefined && !(globalThis.Object.keys(value).length >= schema.minProperties)) {
124
- yield { schema, path, value, message: `Expected object to have at least ${schema.minProperties} properties` };
125
- }
126
- if (schema.maxProperties !== undefined && !(globalThis.Object.keys(value).length <= schema.maxProperties)) {
127
- yield { schema, path, value, message: `Expected object to have less than ${schema.minProperties} properties` };
128
- }
129
- const propertyKeys = globalThis.Object.keys(schema.properties);
130
- if (schema.additionalProperties === false) {
131
- for (const propKey of globalThis.Object.keys(value)) {
132
- if (!propertyKeys.includes(propKey)) {
133
- yield { schema, path: `${path}/${propKey}`, value: value[propKey], message: 'Unexpected property' };
134
- }
135
- }
136
- }
137
- for (const propertyKey of propertyKeys) {
138
- const propertySchema = schema.properties[propertyKey];
139
- if (schema.required && schema.required.includes(propertyKey)) {
140
- yield* Visit(propertySchema, references, `${path}/${propertyKey}`, value[propertyKey]);
141
- }
142
- else {
143
- if (value[propertyKey] !== undefined) {
144
- yield* Visit(propertySchema, references, `${path}/${propertyKey}`, value[propertyKey]);
145
- }
146
- }
147
- }
148
- }
149
- function* Promise(schema, references, path, value) {
150
- if (!(typeof value === 'object' && typeof value.then === 'function')) {
151
- yield { schema, path, value, message: `Expected Promise` };
152
- }
153
- }
154
- function* Record(schema, references, path, value) {
155
- if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value))) {
156
- return yield { schema, path, value, message: `Expected object` };
157
- }
158
- const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
159
- const regex = new RegExp(keyPattern);
160
- if (!globalThis.Object.keys(value).every((key) => regex.test(key))) {
161
- const message = keyPattern === '^(0|[1-9][0-9]*)$' ? 'Expected all object property keys to be numeric' : 'Expected all object property keys to be strings';
162
- return yield { schema, path, value, message };
163
- }
164
- for (const [propKey, propValue] of globalThis.Object.entries(value)) {
165
- yield* Visit(valueSchema, references, `${path}/${propKey}`, propValue);
166
- }
167
- }
168
- function* Ref(schema, references, path, value) {
169
- const reference = references.find((reference) => reference.$id === schema.$ref);
170
- if (reference === undefined)
171
- throw new Error(`ValueErrors.Ref: Cannot find schema with $id '${schema.$ref}'.`);
172
- yield* Visit(reference, references, path, value);
173
- }
174
- function* Self(schema, references, path, value) {
175
- const reference = references.find((reference) => reference.$id === schema.$ref);
176
- if (reference === undefined)
177
- throw new Error(`ValueErrors.Self: Cannot find schema with $id '${schema.$ref}'.`);
178
- yield* Visit(reference, references, path, value);
179
- }
180
- function* String(schema, references, path, value) {
181
- if (!(typeof value === 'string')) {
182
- return yield { schema, path, value, message: 'Expected string' };
183
- }
184
- if (schema.minLength !== undefined && !(value.length >= schema.minLength)) {
185
- yield { schema, path, value, message: `Expected string length greater or equal to ${schema.minLength}` };
186
- }
187
- if (schema.maxLength !== undefined && !(value.length <= schema.maxLength)) {
188
- yield { schema, path, value, message: `Expected string length less or equal to ${schema.maxLength}` };
189
- }
190
- if (schema.pattern !== undefined) {
191
- const regex = new RegExp(schema.pattern);
192
- if (!regex.test(value)) {
193
- yield { schema, path, value, message: `Expected string to match pattern ${schema.pattern}` };
194
- }
195
- }
196
- }
197
- function* Tuple(schema, references, path, value) {
198
- if (!globalThis.Array.isArray(value)) {
199
- return yield { schema, path, value, message: 'Expected Array' };
200
- }
201
- if (schema.items === undefined && !(value.length === 0)) {
202
- return yield { schema, path, value, message: 'Expected tuple to have 0 elements' };
203
- }
204
- if (!(value.length === schema.maxItems)) {
205
- yield { schema, path, value, message: `Expected tuple to have ${schema.maxItems} elements` };
206
- }
207
- if (!schema.items) {
208
- return;
209
- }
210
- for (let i = 0; i < schema.items.length; i++) {
211
- yield* Visit(schema.items[i], references, `${path}/${i}`, value[i]);
212
- }
213
- }
214
- function* Undefined(schema, references, path, value) {
215
- if (!(value === undefined)) {
216
- yield { schema, path, value, message: `Expected undefined` };
217
- }
218
- }
219
- function* Union(schema, references, path, value) {
220
- const errors = [];
221
- for (const inner of schema.anyOf) {
222
- const variantErrors = [...Visit(inner, references, path, value)];
223
- if (variantErrors.length === 0)
224
- return;
225
- errors.push(...variantErrors);
226
- }
227
- for (const error of errors) {
228
- yield error;
229
- }
230
- if (errors.length > 0) {
231
- yield { schema, path, value, message: 'Expected value of union' };
232
- }
233
- }
234
- function* Uint8Array(schema, references, path, value) {
235
- if (!(value instanceof globalThis.Uint8Array)) {
236
- return yield { schema, path, value, message: `Expected Uint8Array` };
237
- }
238
- if (schema.maxByteLength && !(value.length <= schema.maxByteLength)) {
239
- yield { schema, path, value, message: `Expected Uint8Array to have a byte length less or equal to ${schema.maxByteLength}` };
240
- }
241
- if (schema.minByteLength && !(value.length >= schema.minByteLength)) {
242
- yield { schema, path, value, message: `Expected Uint8Array to have a byte length greater or equal to ${schema.maxByteLength}` };
243
- }
244
- }
245
- function* Unknown(schema, references, path, value) { }
246
- function* Void(schema, references, path, value) {
247
- if (!(value === null)) {
248
- return yield { schema, path, value, message: `Expected null` };
249
- }
250
- }
251
- function* Visit(schema, references, path, value) {
252
- const anyReferences = schema.$id === undefined ? references : [schema, ...references];
253
- const anySchema = schema;
254
- switch (anySchema[Types.Kind]) {
255
- case 'Any':
256
- return yield* Any(anySchema, anyReferences, path, value);
257
- case 'Array':
258
- return yield* Array(anySchema, anyReferences, path, value);
259
- case 'Boolean':
260
- return yield* Boolean(anySchema, anyReferences, path, value);
261
- case 'Constructor':
262
- return yield* Constructor(anySchema, anyReferences, path, value);
263
- case 'Function':
264
- return yield* Function(anySchema, anyReferences, path, value);
265
- case 'Integer':
266
- return yield* Integer(anySchema, anyReferences, path, value);
267
- case 'Literal':
268
- return yield* Literal(anySchema, anyReferences, path, value);
269
- case 'Null':
270
- return yield* Null(anySchema, anyReferences, path, value);
271
- case 'Number':
272
- return yield* Number(anySchema, anyReferences, path, value);
273
- case 'Object':
274
- return yield* Object(anySchema, anyReferences, path, value);
275
- case 'Promise':
276
- return yield* Promise(anySchema, anyReferences, path, value);
277
- case 'Record':
278
- return yield* Record(anySchema, anyReferences, path, value);
279
- case 'Ref':
280
- return yield* Ref(anySchema, anyReferences, path, value);
281
- case 'Self':
282
- return yield* Self(anySchema, anyReferences, path, value);
283
- case 'String':
284
- return yield* String(anySchema, anyReferences, path, value);
285
- case 'Tuple':
286
- return yield* Tuple(anySchema, anyReferences, path, value);
287
- case 'Undefined':
288
- return yield* Undefined(anySchema, anyReferences, path, value);
289
- case 'Union':
290
- return yield* Union(anySchema, anyReferences, path, value);
291
- case 'Uint8Array':
292
- return yield* Uint8Array(anySchema, anyReferences, path, value);
293
- case 'Unknown':
294
- return yield* Unknown(anySchema, anyReferences, path, value);
295
- case 'Void':
296
- return yield* Void(anySchema, anyReferences, path, value);
297
- default:
298
- throw new Error(`ValueErrors: Unknown schema kind '${schema[Types.Kind]}'`);
299
- }
300
- }
301
- function* Errors(schema, references, value) {
302
- yield* Visit(schema, references, '', value);
303
- }
304
- ValueErrors.Errors = Errors;
305
- })(ValueErrors = exports.ValueErrors || (exports.ValueErrors = {}));