@sinclair/typebox 0.25.23 → 0.26.0-dev

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 (44) hide show
  1. package/compiler/compiler.d.ts +9 -4
  2. package/compiler/compiler.js +160 -119
  3. package/errors/errors.d.ts +56 -46
  4. package/errors/errors.js +234 -149
  5. package/package.json +1 -6
  6. package/readme.md +395 -396
  7. package/system/system.d.ts +9 -6
  8. package/system/system.js +17 -17
  9. package/typebox.d.ts +386 -162
  10. package/typebox.js +1710 -229
  11. package/value/cast.d.ts +2 -2
  12. package/value/cast.js +120 -192
  13. package/value/check.d.ts +1 -1
  14. package/value/check.js +162 -107
  15. package/value/convert.d.ts +13 -0
  16. package/value/convert.js +345 -0
  17. package/value/create.d.ts +6 -2
  18. package/value/create.js +149 -107
  19. package/{hash → value}/hash.js +39 -14
  20. package/value/index.d.ts +1 -0
  21. package/value/index.js +3 -1
  22. package/value/value.d.ts +2 -8
  23. package/value/value.js +20 -14
  24. package/conditional/conditional.d.ts +0 -17
  25. package/conditional/conditional.js +0 -91
  26. package/conditional/index.d.ts +0 -2
  27. package/conditional/index.js +0 -45
  28. package/conditional/structural.d.ts +0 -11
  29. package/conditional/structural.js +0 -685
  30. package/custom/custom.d.ts +0 -12
  31. package/custom/custom.js +0 -55
  32. package/custom/index.d.ts +0 -1
  33. package/custom/index.js +0 -44
  34. package/format/format.d.ts +0 -12
  35. package/format/format.js +0 -55
  36. package/format/index.d.ts +0 -1
  37. package/format/index.js +0 -44
  38. package/guard/guard.d.ts +0 -60
  39. package/guard/guard.js +0 -440
  40. package/guard/index.d.ts +0 -1
  41. package/guard/index.js +0 -44
  42. package/hash/index.d.ts +0 -1
  43. package/hash/index.js +0 -44
  44. /package/{hash → value}/hash.d.ts +0 -0
package/value/check.js CHANGED
@@ -30,9 +30,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
30
30
  exports.ValueCheck = exports.ValueCheckUnknownTypeError = void 0;
31
31
  const Types = require("../typebox");
32
32
  const index_1 = require("../system/index");
33
- const index_2 = require("../format/index");
34
- const index_3 = require("../custom/index");
35
- const index_4 = require("../hash/index");
33
+ const hash_1 = require("./hash");
36
34
  class ValueCheckUnknownTypeError extends Error {
37
35
  constructor(schema) {
38
36
  super(`ValueCheck: ${schema[Types.Kind] ? `Unknown type '${schema[Types.Kind]}'` : 'Unknown type'}`);
@@ -42,13 +40,22 @@ class ValueCheckUnknownTypeError extends Error {
42
40
  exports.ValueCheckUnknownTypeError = ValueCheckUnknownTypeError;
43
41
  var ValueCheck;
44
42
  (function (ValueCheck) {
43
+ // --------------------------------------------------------
44
+ // Guards
45
+ // --------------------------------------------------------
46
+ function IsBigInt(value) {
47
+ return typeof value === 'bigint';
48
+ }
45
49
  function IsNumber(value) {
46
- return typeof value === 'number' && !isNaN(value);
50
+ return typeof value === 'number' && globalThis.Number.isFinite(value);
47
51
  }
48
- function Any(schema, references, value) {
52
+ // --------------------------------------------------------
53
+ // Guards
54
+ // --------------------------------------------------------
55
+ function Any(schema, value) {
49
56
  return true;
50
57
  }
51
- function Array(schema, references, value) {
58
+ function Array(schema, value) {
52
59
  if (!globalThis.Array.isArray(value)) {
53
60
  return false;
54
61
  }
@@ -60,7 +67,7 @@ var ValueCheck;
60
67
  }
61
68
  // prettier-ignore
62
69
  if (schema.uniqueItems === true && !((function () { const set = new Set(); for (const element of value) {
63
- const hashed = index_4.ValueHash.Create(element);
70
+ const hashed = hash_1.ValueHash.Create(element);
64
71
  if (set.has(hashed)) {
65
72
  return false;
66
73
  }
@@ -70,19 +77,40 @@ var ValueCheck;
70
77
  } return true; })())) {
71
78
  return false;
72
79
  }
73
- return value.every((val) => Visit(schema.items, references, val));
80
+ return value.every((value) => Visit(schema.items, value));
81
+ }
82
+ function BigInt(schema, value) {
83
+ if (typeof value !== 'bigint') {
84
+ return false;
85
+ }
86
+ if (IsBigInt(schema.multipleOf) && !(value % schema.multipleOf === globalThis.BigInt(0))) {
87
+ return false;
88
+ }
89
+ if (IsBigInt(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
90
+ return false;
91
+ }
92
+ if (IsBigInt(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
93
+ return false;
94
+ }
95
+ if (IsBigInt(schema.minimum) && !(value >= schema.minimum)) {
96
+ return false;
97
+ }
98
+ if (IsBigInt(schema.maximum) && !(value <= schema.maximum)) {
99
+ return false;
100
+ }
101
+ return true;
74
102
  }
75
- function Boolean(schema, references, value) {
103
+ function Boolean(schema, value) {
76
104
  return typeof value === 'boolean';
77
105
  }
78
- function Constructor(schema, references, value) {
79
- return Visit(schema.returns, references, value.prototype);
106
+ function Constructor(schema, value) {
107
+ return Visit(schema.returns, value.prototype);
80
108
  }
81
- function Date(schema, references, value) {
109
+ function Date(schema, value) {
82
110
  if (!(value instanceof globalThis.Date)) {
83
111
  return false;
84
112
  }
85
- if (isNaN(value.getTime())) {
113
+ if (!globalThis.Number.isFinite(value.getTime())) {
86
114
  return false;
87
115
  }
88
116
  if (IsNumber(schema.exclusiveMinimumTimestamp) && !(value.getTime() > schema.exclusiveMinimumTimestamp)) {
@@ -99,10 +127,10 @@ var ValueCheck;
99
127
  }
100
128
  return true;
101
129
  }
102
- function Function(schema, references, value) {
130
+ function Function(schema, value) {
103
131
  return typeof value === 'function';
104
132
  }
105
- function Integer(schema, references, value) {
133
+ function Integer(schema, value) {
106
134
  if (!(typeof value === 'number' && globalThis.Number.isInteger(value))) {
107
135
  return false;
108
136
  }
@@ -123,23 +151,44 @@ var ValueCheck;
123
151
  }
124
152
  return true;
125
153
  }
126
- function Literal(schema, references, value) {
154
+ function Intersect(schema, value) {
155
+ if (!schema.allOf.every((schema) => Visit(schema, value))) {
156
+ return false;
157
+ }
158
+ else if (schema.unevaluatedProperties === false) {
159
+ const schemaKeys = Types.KeyResolver.Resolve(schema);
160
+ const valueKeys = globalThis.Object.getOwnPropertyNames(value);
161
+ return valueKeys.every((key) => schemaKeys.includes(key));
162
+ }
163
+ else if (Types.TypeGuard.TSchema(schema.unevaluatedProperties)) {
164
+ const schemaKeys = Types.KeyResolver.Resolve(schema);
165
+ const valueKeys = globalThis.Object.getOwnPropertyNames(value);
166
+ return valueKeys.every((key) => schemaKeys.includes(key) || Visit(schema.unevaluatedProperties, value[key]));
167
+ }
168
+ else {
169
+ return true;
170
+ }
171
+ }
172
+ function Literal(schema, value) {
127
173
  return value === schema.const;
128
174
  }
129
- function Never(schema, references, value) {
175
+ function Never(schema, value) {
130
176
  return false;
131
177
  }
132
- function Null(schema, references, value) {
178
+ function Not(schema, value) {
179
+ return !Visit(schema.allOf[0].not, value) && Visit(schema.allOf[1], value);
180
+ }
181
+ function Null(schema, value) {
133
182
  return value === null;
134
183
  }
135
- function Number(schema, references, value) {
184
+ function Number(schema, value) {
136
185
  if (index_1.TypeSystem.AllowNaN) {
137
186
  if (!(typeof value === 'number')) {
138
187
  return false;
139
188
  }
140
189
  }
141
190
  else {
142
- if (!(typeof value === 'number' && !isNaN(value))) {
191
+ if (!(typeof value === 'number' && globalThis.Number.isFinite(value))) {
143
192
  return false;
144
193
  }
145
194
  }
@@ -160,7 +209,7 @@ var ValueCheck;
160
209
  }
161
210
  return true;
162
211
  }
163
- function Object(schema, references, value) {
212
+ function Object(schema, value) {
164
213
  if (index_1.TypeSystem.AllowArrayObjects) {
165
214
  if (!(typeof value === 'object' && value !== null)) {
166
215
  return false;
@@ -177,51 +226,45 @@ var ValueCheck;
177
226
  if (IsNumber(schema.maxProperties) && !(globalThis.Object.getOwnPropertyNames(value).length <= schema.maxProperties)) {
178
227
  return false;
179
228
  }
180
- const propertyKeys = globalThis.Object.getOwnPropertyNames(schema.properties);
181
- if (schema.additionalProperties === false) {
182
- // optimization: If the property key length matches the required keys length
183
- // then we only need check that the values property key length matches that
184
- // of the property key length. This is because exhaustive testing for values
185
- // will occur in subsequent property tests.
186
- if (schema.required && schema.required.length === propertyKeys.length && !(globalThis.Object.getOwnPropertyNames(value).length === propertyKeys.length)) {
187
- return false;
188
- }
189
- else {
190
- if (!globalThis.Object.getOwnPropertyNames(value).every((key) => propertyKeys.includes(key))) {
229
+ const schemaKeys = globalThis.Object.getOwnPropertyNames(schema.properties);
230
+ for (const schemaKey of schemaKeys) {
231
+ const property = schema.properties[schemaKey];
232
+ if (schema.required && schema.required.includes(schemaKey)) {
233
+ if (!Visit(property, value[schemaKey])) {
191
234
  return false;
192
235
  }
236
+ if (Types.ExtendsUndefined.Check(property)) {
237
+ return schemaKey in value;
238
+ }
193
239
  }
194
- }
195
- if (typeof schema.additionalProperties === 'object') {
196
- for (const objectKey of globalThis.Object.getOwnPropertyNames(value)) {
197
- if (propertyKeys.includes(objectKey))
198
- continue;
199
- if (!Visit(schema.additionalProperties, references, value[objectKey])) {
240
+ else {
241
+ if (schemaKey in value && !Visit(property, value[schemaKey])) {
200
242
  return false;
201
243
  }
202
244
  }
203
245
  }
204
- for (const propertyKey of propertyKeys) {
205
- const propertySchema = schema.properties[propertyKey];
206
- if (schema.required && schema.required.includes(propertyKey)) {
207
- if (!Visit(propertySchema, references, value[propertyKey])) {
208
- return false;
209
- }
246
+ if (schema.additionalProperties === false) {
247
+ const valueKeys = globalThis.Object.getOwnPropertyNames(value);
248
+ // optimization: value is valid if schemaKey length matches the valueKey length
249
+ if (schema.required && schema.required.length === schemaKeys.length && valueKeys.length === schemaKeys.length) {
250
+ return true;
210
251
  }
211
252
  else {
212
- if (value[propertyKey] !== undefined) {
213
- if (!Visit(propertySchema, references, value[propertyKey])) {
214
- return false;
215
- }
216
- }
253
+ return valueKeys.every((valueKey) => schemaKeys.includes(valueKey));
217
254
  }
218
255
  }
219
- return true;
256
+ else if (typeof schema.additionalProperties === 'object') {
257
+ const valueKeys = globalThis.Object.getOwnPropertyNames(value);
258
+ return valueKeys.every((key) => schemaKeys.includes(key) || Visit(schema.additionalProperties, value[key]));
259
+ }
260
+ else {
261
+ return true;
262
+ }
220
263
  }
221
- function Promise(schema, references, value) {
264
+ function Promise(schema, value) {
222
265
  return typeof value === 'object' && typeof value.then === 'function';
223
266
  }
224
- function Record(schema, references, value) {
267
+ function Record(schema, value) {
225
268
  if (index_1.TypeSystem.AllowArrayObjects) {
226
269
  if (!(typeof value === 'object' && value !== null && !(value instanceof globalThis.Date))) {
227
270
  return false;
@@ -238,24 +281,18 @@ var ValueCheck;
238
281
  return false;
239
282
  }
240
283
  for (const propValue of globalThis.Object.values(value)) {
241
- if (!Visit(valueSchema, references, propValue))
284
+ if (!Visit(valueSchema, propValue))
242
285
  return false;
243
286
  }
244
287
  return true;
245
288
  }
246
- function Ref(schema, references, value) {
247
- const reference = references.find((reference) => reference.$id === schema.$ref);
248
- if (reference === undefined)
249
- throw new Error(`ValueCheck.Ref: Cannot find schema with $id '${schema.$ref}'.`);
250
- return Visit(reference, references, value);
289
+ function Ref(schema, value) {
290
+ return Visit(Types.ReferenceRegistry.DerefOne(schema), value);
251
291
  }
252
- function Self(schema, references, value) {
253
- const reference = references.find((reference) => reference.$id === schema.$ref);
254
- if (reference === undefined)
255
- throw new Error(`ValueCheck.Self: Cannot find schema with $id '${schema.$ref}'.`);
256
- return Visit(reference, references, value);
292
+ function Self(schema, value) {
293
+ return Visit(Types.ReferenceRegistry.DerefOne(schema), value);
257
294
  }
258
- function String(schema, references, value) {
295
+ function String(schema, value) {
259
296
  if (!(typeof value === 'string')) {
260
297
  return false;
261
298
  }
@@ -273,14 +310,20 @@ var ValueCheck;
273
310
  return false;
274
311
  }
275
312
  if (schema.format !== undefined) {
276
- if (!index_2.Format.Has(schema.format))
313
+ if (!Types.FormatRegistry.Has(schema.format))
277
314
  return false;
278
- const func = index_2.Format.Get(schema.format);
315
+ const func = Types.FormatRegistry.Get(schema.format);
279
316
  return func(value);
280
317
  }
281
318
  return true;
282
319
  }
283
- function Tuple(schema, references, value) {
320
+ function Symbol(schema, value) {
321
+ if (!(typeof value === 'symbol')) {
322
+ return false;
323
+ }
324
+ return true;
325
+ }
326
+ function Tuple(schema, value) {
284
327
  if (!globalThis.Array.isArray(value)) {
285
328
  return false;
286
329
  }
@@ -294,18 +337,18 @@ var ValueCheck;
294
337
  return true;
295
338
  }
296
339
  for (let i = 0; i < schema.items.length; i++) {
297
- if (!Visit(schema.items[i], references, value[i]))
340
+ if (!Visit(schema.items[i], value[i]))
298
341
  return false;
299
342
  }
300
343
  return true;
301
344
  }
302
- function Undefined(schema, references, value) {
345
+ function Undefined(schema, value) {
303
346
  return value === undefined;
304
347
  }
305
- function Union(schema, references, value) {
306
- return schema.anyOf.some((inner) => Visit(inner, references, value));
348
+ function Union(schema, value) {
349
+ return schema.anyOf.some((inner) => Visit(inner, value));
307
350
  }
308
- function Uint8Array(schema, references, value) {
351
+ function Uint8Array(schema, value) {
309
352
  if (!(value instanceof globalThis.Uint8Array)) {
310
353
  return false;
311
354
  }
@@ -317,79 +360,91 @@ var ValueCheck;
317
360
  }
318
361
  return true;
319
362
  }
320
- function Unknown(schema, references, value) {
363
+ function Unknown(schema, value) {
321
364
  return true;
322
365
  }
323
- function Void(schema, references, value) {
324
- return value === null;
366
+ function Void(schema, value) {
367
+ if (index_1.TypeSystem.AllowVoidNull) {
368
+ return value === undefined || value === null;
369
+ }
370
+ else {
371
+ return value === undefined;
372
+ }
325
373
  }
326
- function UserDefined(schema, references, value) {
327
- if (!index_3.Custom.Has(schema[Types.Kind]))
374
+ function UserDefined(schema, value) {
375
+ if (!Types.TypeRegistry.Has(schema[Types.Kind]))
328
376
  return false;
329
- const func = index_3.Custom.Get(schema[Types.Kind]);
377
+ const func = Types.TypeRegistry.Get(schema[Types.Kind]);
330
378
  return func(schema, value);
331
379
  }
332
- function Visit(schema, references, value) {
333
- const anyReferences = schema.$id === undefined ? references : [schema, ...references];
380
+ function Visit(schema, value) {
334
381
  const anySchema = schema;
335
382
  switch (anySchema[Types.Kind]) {
336
383
  case 'Any':
337
- return Any(anySchema, anyReferences, value);
384
+ return Any(anySchema, value);
338
385
  case 'Array':
339
- return Array(anySchema, anyReferences, value);
386
+ return Array(anySchema, value);
387
+ case 'BigInt':
388
+ return BigInt(anySchema, value);
340
389
  case 'Boolean':
341
- return Boolean(anySchema, anyReferences, value);
390
+ return Boolean(anySchema, value);
342
391
  case 'Constructor':
343
- return Constructor(anySchema, anyReferences, value);
392
+ return Constructor(anySchema, value);
344
393
  case 'Date':
345
- return Date(anySchema, anyReferences, value);
394
+ return Date(anySchema, value);
346
395
  case 'Function':
347
- return Function(anySchema, anyReferences, value);
396
+ return Function(anySchema, value);
348
397
  case 'Integer':
349
- return Integer(anySchema, anyReferences, value);
398
+ return Integer(anySchema, value);
399
+ case 'Intersect':
400
+ return Intersect(anySchema, value);
350
401
  case 'Literal':
351
- return Literal(anySchema, anyReferences, value);
402
+ return Literal(anySchema, value);
352
403
  case 'Never':
353
- return Never(anySchema, anyReferences, value);
404
+ return Never(anySchema, value);
405
+ case 'Not':
406
+ return Not(anySchema, value);
354
407
  case 'Null':
355
- return Null(anySchema, anyReferences, value);
408
+ return Null(anySchema, value);
356
409
  case 'Number':
357
- return Number(anySchema, anyReferences, value);
410
+ return Number(anySchema, value);
358
411
  case 'Object':
359
- return Object(anySchema, anyReferences, value);
412
+ return Object(anySchema, value);
360
413
  case 'Promise':
361
- return Promise(anySchema, anyReferences, value);
414
+ return Promise(anySchema, value);
362
415
  case 'Record':
363
- return Record(anySchema, anyReferences, value);
416
+ return Record(anySchema, value);
364
417
  case 'Ref':
365
- return Ref(anySchema, anyReferences, value);
418
+ return Ref(anySchema, value);
366
419
  case 'Self':
367
- return Self(anySchema, anyReferences, value);
420
+ return Self(anySchema, value);
368
421
  case 'String':
369
- return String(anySchema, anyReferences, value);
422
+ return String(anySchema, value);
423
+ case 'Symbol':
424
+ return Symbol(anySchema, value);
370
425
  case 'Tuple':
371
- return Tuple(anySchema, anyReferences, value);
426
+ return Tuple(anySchema, value);
372
427
  case 'Undefined':
373
- return Undefined(anySchema, anyReferences, value);
428
+ return Undefined(anySchema, value);
374
429
  case 'Union':
375
- return Union(anySchema, anyReferences, value);
430
+ return Union(anySchema, value);
376
431
  case 'Uint8Array':
377
- return Uint8Array(anySchema, anyReferences, value);
432
+ return Uint8Array(anySchema, value);
378
433
  case 'Unknown':
379
- return Unknown(anySchema, anyReferences, value);
434
+ return Unknown(anySchema, value);
380
435
  case 'Void':
381
- return Void(anySchema, anyReferences, value);
436
+ return Void(anySchema, value);
382
437
  default:
383
- if (!index_3.Custom.Has(anySchema[Types.Kind]))
438
+ if (!Types.TypeRegistry.Has(anySchema[Types.Kind]))
384
439
  throw new ValueCheckUnknownTypeError(anySchema);
385
- return UserDefined(anySchema, anyReferences, value);
440
+ return UserDefined(anySchema, value);
386
441
  }
387
442
  }
388
443
  // -------------------------------------------------------------------------
389
444
  // Check
390
445
  // -------------------------------------------------------------------------
391
- function Check(schema, references, value) {
392
- return schema.$id === undefined ? Visit(schema, references, value) : Visit(schema, [schema, ...references], value);
446
+ function Check(schema, value) {
447
+ return Visit(schema, value);
393
448
  }
394
449
  ValueCheck.Check = Check;
395
450
  })(ValueCheck = exports.ValueCheck || (exports.ValueCheck = {}));
@@ -0,0 +1,13 @@
1
+ import * as Types from '../typebox';
2
+ export declare class ValueConvertReferenceTypeError extends Error {
3
+ readonly schema: Types.TRef | Types.TSelf;
4
+ constructor(schema: Types.TRef | Types.TSelf);
5
+ }
6
+ export declare class ValueConvertUnknownTypeError extends Error {
7
+ readonly schema: Types.TSchema;
8
+ constructor(schema: Types.TSchema);
9
+ }
10
+ export declare namespace ValueConvert {
11
+ function Visit(schema: Types.TSchema, value: any): unknown;
12
+ function Convert<T extends Types.TSchema>(schema: T, value: any): unknown;
13
+ }