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