@sinclair/typebox 0.26.7 → 0.27.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.
- package/compiler/compiler.js +13 -6
- package/errors/errors.d.ts +2 -2
- package/errors/errors.js +21 -10
- package/package.json +2 -2
- package/readme.md +178 -108
- package/typebox.d.ts +115 -39
- package/typebox.js +399 -51
- package/value/cast.d.ts +4 -4
- package/value/cast.js +14 -9
- package/value/check.d.ts +2 -2
- package/value/check.js +17 -9
- package/value/convert.d.ts +2 -2
- package/value/convert.js +14 -9
- package/value/create.d.ts +6 -2
- package/value/create.js +35 -16
package/compiler/compiler.js
CHANGED
|
@@ -363,10 +363,6 @@ var TypeCompiler;
|
|
|
363
363
|
return yield `${CreateFunctionName(schema.$ref)}(${value})`;
|
|
364
364
|
yield* Visit(target, references, value);
|
|
365
365
|
}
|
|
366
|
-
function* Self(schema, references, value) {
|
|
367
|
-
const func = CreateFunctionName(schema.$ref);
|
|
368
|
-
yield `${func}(${value})`;
|
|
369
|
-
}
|
|
370
366
|
function* String(schema, references, value) {
|
|
371
367
|
yield `(typeof ${value} === 'string')`;
|
|
372
368
|
if (IsNumber(schema.minLength))
|
|
@@ -384,6 +380,15 @@ var TypeCompiler;
|
|
|
384
380
|
function* Symbol(schema, references, value) {
|
|
385
381
|
yield `(typeof ${value} === 'symbol')`;
|
|
386
382
|
}
|
|
383
|
+
function* TemplateLiteral(schema, references, value) {
|
|
384
|
+
yield `(typeof ${value} === 'string')`;
|
|
385
|
+
const local = PushLocal(`${new RegExp(schema.pattern)};`);
|
|
386
|
+
yield `${local}.test(${value})`;
|
|
387
|
+
}
|
|
388
|
+
function* This(schema, references, value) {
|
|
389
|
+
const func = CreateFunctionName(schema.$ref);
|
|
390
|
+
yield `${func}(${value})`;
|
|
391
|
+
}
|
|
387
392
|
function* Tuple(schema, references, value) {
|
|
388
393
|
yield `(Array.isArray(${value}))`;
|
|
389
394
|
if (schema.items === undefined)
|
|
@@ -470,12 +475,14 @@ var TypeCompiler;
|
|
|
470
475
|
return yield* Record(schema_, references_, value);
|
|
471
476
|
case 'Ref':
|
|
472
477
|
return yield* Ref(schema_, references_, value);
|
|
473
|
-
case 'Self':
|
|
474
|
-
return yield* Self(schema_, references_, value);
|
|
475
478
|
case 'String':
|
|
476
479
|
return yield* String(schema_, references_, value);
|
|
477
480
|
case 'Symbol':
|
|
478
481
|
return yield* Symbol(schema_, references_, value);
|
|
482
|
+
case 'TemplateLiteral':
|
|
483
|
+
return yield* TemplateLiteral(schema_, references_, value);
|
|
484
|
+
case 'This':
|
|
485
|
+
return yield* This(schema_, references_, value);
|
|
479
486
|
case 'Tuple':
|
|
480
487
|
return yield* Tuple(schema_, references_, value);
|
|
481
488
|
case 'Undefined':
|
package/errors/errors.d.ts
CHANGED
|
@@ -79,8 +79,8 @@ export declare class ValueErrorsUnknownTypeError extends Error {
|
|
|
79
79
|
constructor(schema: Types.TSchema);
|
|
80
80
|
}
|
|
81
81
|
export declare class ValueErrorsDereferenceError extends Error {
|
|
82
|
-
readonly schema: Types.TRef | Types.
|
|
83
|
-
constructor(schema: Types.TRef | Types.
|
|
82
|
+
readonly schema: Types.TRef | Types.TThis;
|
|
83
|
+
constructor(schema: Types.TRef | Types.TThis);
|
|
84
84
|
}
|
|
85
85
|
/** Provides functionality to generate a sequence of errors against a TypeBox type. */
|
|
86
86
|
export declare namespace ValueErrors {
|
package/errors/errors.js
CHANGED
|
@@ -410,7 +410,7 @@ var ValueErrors;
|
|
|
410
410
|
const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
|
|
411
411
|
const regex = new RegExp(keyPattern);
|
|
412
412
|
if (!globalThis.Object.getOwnPropertyNames(value).every((key) => regex.test(key))) {
|
|
413
|
-
const numeric = keyPattern ===
|
|
413
|
+
const numeric = keyPattern === Types.PatternNumberExact;
|
|
414
414
|
const type = numeric ? ValueErrorType.RecordKeyNumeric : ValueErrorType.RecordKeyString;
|
|
415
415
|
const message = numeric ? 'Expected all object property keys to be numeric' : 'Expected all object property keys to be strings';
|
|
416
416
|
return yield { type, schema, path, value, message };
|
|
@@ -426,13 +426,6 @@ var ValueErrors;
|
|
|
426
426
|
const target = references[index];
|
|
427
427
|
yield* Visit(target, references, path, value);
|
|
428
428
|
}
|
|
429
|
-
function* Self(schema, references, path, value) {
|
|
430
|
-
const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
|
|
431
|
-
if (index === -1)
|
|
432
|
-
throw new ValueErrorsDereferenceError(schema);
|
|
433
|
-
const target = references[index];
|
|
434
|
-
yield* Visit(target, references, path, value);
|
|
435
|
-
}
|
|
436
429
|
function* String(schema, references, path, value) {
|
|
437
430
|
if (!IsString(value)) {
|
|
438
431
|
return yield { type: ValueErrorType.String, schema, path, value, message: 'Expected string' };
|
|
@@ -466,6 +459,22 @@ var ValueErrors;
|
|
|
466
459
|
return yield { type: ValueErrorType.Symbol, schema, path, value, message: 'Expected symbol' };
|
|
467
460
|
}
|
|
468
461
|
}
|
|
462
|
+
function* TemplateLiteral(schema, references, path, value) {
|
|
463
|
+
if (!IsString(value)) {
|
|
464
|
+
return yield { type: ValueErrorType.String, schema, path, value, message: 'Expected string' };
|
|
465
|
+
}
|
|
466
|
+
const regex = new RegExp(schema.pattern);
|
|
467
|
+
if (!regex.test(value)) {
|
|
468
|
+
yield { type: ValueErrorType.StringPattern, schema, path, value, message: `Expected string to match pattern ${schema.pattern}` };
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
function* This(schema, references, path, value) {
|
|
472
|
+
const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
|
|
473
|
+
if (index === -1)
|
|
474
|
+
throw new ValueErrorsDereferenceError(schema);
|
|
475
|
+
const target = references[index];
|
|
476
|
+
yield* Visit(target, references, path, value);
|
|
477
|
+
}
|
|
469
478
|
function* Tuple(schema, references, path, value) {
|
|
470
479
|
if (!globalThis.Array.isArray(value)) {
|
|
471
480
|
return yield { type: ValueErrorType.Array, schema, path, value, message: 'Expected Array' };
|
|
@@ -566,12 +575,14 @@ var ValueErrors;
|
|
|
566
575
|
return yield* Record(schema_, references_, path, value);
|
|
567
576
|
case 'Ref':
|
|
568
577
|
return yield* Ref(schema_, references_, path, value);
|
|
569
|
-
case 'Self':
|
|
570
|
-
return yield* Self(schema_, references_, path, value);
|
|
571
578
|
case 'String':
|
|
572
579
|
return yield* String(schema_, references_, path, value);
|
|
573
580
|
case 'Symbol':
|
|
574
581
|
return yield* Symbol(schema_, references_, path, value);
|
|
582
|
+
case 'TemplateLiteral':
|
|
583
|
+
return yield* TemplateLiteral(schema_, references_, path, value);
|
|
584
|
+
case 'This':
|
|
585
|
+
return yield* This(schema_, references_, path, value);
|
|
575
586
|
case 'Tuple':
|
|
576
587
|
return yield* Tuple(schema_, references_, path, value);
|
|
577
588
|
case 'Undefined':
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sinclair/typebox",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.27.0",
|
|
4
4
|
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"@types/chai": "^4.3.3",
|
|
38
38
|
"@types/mocha": "^9.1.1",
|
|
39
39
|
"@types/node": "^18.11.9",
|
|
40
|
-
"ajv": "^8.
|
|
40
|
+
"ajv": "^8.12.0",
|
|
41
41
|
"ajv-formats": "^2.1.1",
|
|
42
42
|
"chai": "^4.3.6",
|
|
43
43
|
"mocha": "^9.2.2",
|
package/readme.md
CHANGED
|
@@ -81,6 +81,7 @@ License MIT
|
|
|
81
81
|
- [References](#types-references)
|
|
82
82
|
- [Recursive](#types-recursive)
|
|
83
83
|
- [Conditional](#types-conditional)
|
|
84
|
+
- [Template Literal](#types-template-literal)
|
|
84
85
|
- [Guards](#types-guards)
|
|
85
86
|
- [Unsafe](#types-unsafe)
|
|
86
87
|
- [Strict](#types-strict)
|
|
@@ -264,12 +265,12 @@ The following table lists the Standard TypeBox types. These types are fully comp
|
|
|
264
265
|
│ Type.Number(), │ │ type: 'array', │
|
|
265
266
|
│ Type.Number() │ │ items: [{ │
|
|
266
267
|
│ ]) │ │ type: 'number' │
|
|
267
|
-
│ │ │
|
|
268
|
-
│ │ │
|
|
269
|
-
│ │ │
|
|
270
|
-
│ │ │
|
|
271
|
-
│ │ │
|
|
272
|
-
│ │ │
|
|
268
|
+
│ │ │ }, { │
|
|
269
|
+
│ │ │ type: 'number' │
|
|
270
|
+
│ │ │ }], │
|
|
271
|
+
│ │ │ additionalItems: false, │
|
|
272
|
+
│ │ │ minItems: 2, │
|
|
273
|
+
│ │ │ maxItems: 2 │
|
|
273
274
|
│ │ │ } │
|
|
274
275
|
│ │ │ │
|
|
275
276
|
│ │ │ │
|
|
@@ -321,6 +322,7 @@ The following table lists the Standard TypeBox types. These types are fully comp
|
|
|
321
322
|
│ │ │ y: { │
|
|
322
323
|
│ │ │ type: 'number' │
|
|
323
324
|
│ │ │ } │
|
|
325
|
+
│ │ │ } │
|
|
324
326
|
│ │ │ }] │
|
|
325
327
|
│ │ │ } │
|
|
326
328
|
│ │ │ │
|
|
@@ -385,6 +387,18 @@ The following table lists the Standard TypeBox types. These types are fully comp
|
|
|
385
387
|
│ ) │ │ │
|
|
386
388
|
│ │ │ │
|
|
387
389
|
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
390
|
+
│ const U = Type.Union([ │ type U = 'open' | 'close' │ const T = { │
|
|
391
|
+
│ Type.Literal('open'), │ │ type: 'string', │
|
|
392
|
+
│ Type.Literal('close') │ type T = `on${U}` │ pattern: '^on(open|close)$' │
|
|
393
|
+
│ ]) │ │ } │
|
|
394
|
+
│ │ │ │
|
|
395
|
+
│ const T = Type │ │ │
|
|
396
|
+
│ .TemplateLiteral([ │ │ │
|
|
397
|
+
│ Type.Literal('on'), │ │ │
|
|
398
|
+
│ U │ │ │
|
|
399
|
+
│ ]) │ │ │
|
|
400
|
+
│ │ │ │
|
|
401
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
388
402
|
│ const T = Type.Record( │ type T = Record< │ const T = { │
|
|
389
403
|
│ Type.String(), │ string, │ type: 'object', │
|
|
390
404
|
│ Type.Number() │ number, │ patternProperties: { │
|
|
@@ -424,32 +438,32 @@ The following table lists the Standard TypeBox types. These types are fully comp
|
|
|
424
438
|
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
425
439
|
│ const T = Type.Pick( │ type T = Pick<{ │ const T = { │
|
|
426
440
|
│ Type.Object({ │ x: number, │ type: 'object', │
|
|
427
|
-
│ x: Type.Number(), │ y: number │
|
|
428
|
-
│ y: Type.Number()
|
|
429
|
-
│ }), ['x']
|
|
430
|
-
│ ) │ │
|
|
431
|
-
│ │ │
|
|
432
|
-
│ │ │
|
|
441
|
+
│ x: Type.Number(), │ y: number │ required: ['x'], │
|
|
442
|
+
│ y: Type.Number() │ }, 'x'> │ properties: { │
|
|
443
|
+
│ }), ['x'] | │ x: { │
|
|
444
|
+
│ ) │ │ type: 'number' │
|
|
445
|
+
│ │ │ } │
|
|
446
|
+
│ │ │ } │
|
|
433
447
|
│ │ │ } │
|
|
434
448
|
│ │ │ │
|
|
435
449
|
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
436
450
|
│ const T = Type.Omit( │ type T = Omit<{ │ const T = { │
|
|
437
451
|
│ Type.Object({ │ x: number, │ type: 'object', │
|
|
438
|
-
│ x: Type.Number(), │ y: number │
|
|
439
|
-
│ y: Type.Number()
|
|
440
|
-
│ }), ['x']
|
|
441
|
-
│ ) │ │
|
|
442
|
-
│ │ │
|
|
443
|
-
│ │ │
|
|
452
|
+
│ x: Type.Number(), │ y: number │ required: ['y'], │
|
|
453
|
+
│ y: Type.Number() │ }, 'x'> │ properties: { │
|
|
454
|
+
│ }), ['x'] | │ y: { │
|
|
455
|
+
│ ) │ │ type: 'number' │
|
|
456
|
+
│ │ │ } │
|
|
457
|
+
│ │ │ } │
|
|
444
458
|
│ │ │ } │
|
|
445
459
|
│ │ │ │
|
|
446
460
|
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
447
|
-
│ const
|
|
448
|
-
│ x: Type.Number(), │ x: number, │ $ref: '
|
|
461
|
+
│ const T = Type.Object({ │ type T = { │ const R = { │
|
|
462
|
+
│ x: Type.Number(), │ x: number, │ $ref: 'T' │
|
|
449
463
|
│ y: Type.Number() │ y: number │ } │
|
|
450
464
|
│ }, { $id: 'T' }) | } │ │
|
|
451
465
|
│ │ │ │
|
|
452
|
-
│ const
|
|
466
|
+
│ const R = Type.Ref(T) │ type R = T │ │
|
|
453
467
|
│ │ │ │
|
|
454
468
|
│ │ │ │
|
|
455
469
|
│ │ │ │
|
|
@@ -687,7 +701,7 @@ type T = Static<typeof T> // type T = string | null
|
|
|
687
701
|
|
|
688
702
|
### Reference Types
|
|
689
703
|
|
|
690
|
-
Reference types are supported with `Type.Ref
|
|
704
|
+
Reference types are supported with `Type.Ref`. The target type must specify a valid `$id`.
|
|
691
705
|
|
|
692
706
|
```typescript
|
|
693
707
|
const T = Type.String({ $id: 'T' }) // const T = {
|
|
@@ -704,7 +718,7 @@ const R = Type.Ref(T) // const R = {
|
|
|
704
718
|
|
|
705
719
|
### Recursive Types
|
|
706
720
|
|
|
707
|
-
Recursive types are supported with `Type.Recursive
|
|
721
|
+
Recursive types are supported with `Type.Recursive`
|
|
708
722
|
|
|
709
723
|
```typescript
|
|
710
724
|
const Node = Type.Recursive(Node => Type.Object({ // const Node = {
|
|
@@ -741,33 +755,85 @@ function test(node: Node) {
|
|
|
741
755
|
|
|
742
756
|
### Conditional Types
|
|
743
757
|
|
|
744
|
-
Conditional types are supported with `Extends`, `Exclude` and `Extract
|
|
745
|
-
|
|
746
|
-
**TypeScript**
|
|
758
|
+
Conditional types are supported with `Type.Extends`, `Type.Exclude` and `Type.Extract`
|
|
747
759
|
|
|
748
760
|
```typescript
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
type
|
|
752
|
-
|
|
753
|
-
type
|
|
754
|
-
|
|
761
|
+
// TypeScript
|
|
762
|
+
|
|
763
|
+
type T0 = string extends number ? true : false // type T0 = false
|
|
764
|
+
|
|
765
|
+
type T1 = Extract<string | number, number> // type T1 = number
|
|
766
|
+
|
|
767
|
+
type T2 = Exclude<string | number, number> // type T2 = string
|
|
768
|
+
|
|
769
|
+
// TypeBox
|
|
770
|
+
|
|
771
|
+
const T0 = Type.Extends(Type.String(), Type.Number(), Type.Literal(true), Type.Literal(false))
|
|
772
|
+
|
|
773
|
+
const T1 = Type.Extract(Type.Union([Type.String(), Type.Number()]), Type.Number())
|
|
774
|
+
|
|
775
|
+
const T2 = Type.Exclude(Type.Union([Type.String(), Type.Number()]), Type.Number())
|
|
776
|
+
|
|
777
|
+
|
|
778
|
+
type T0 = Static<typeof T0> // type T0 = false
|
|
779
|
+
|
|
780
|
+
type T1 = Static<typeof T1> // type T1 = number
|
|
781
|
+
|
|
782
|
+
type T2 = Static<typeof T2> // type T2 = string
|
|
755
783
|
```
|
|
756
|
-
|
|
784
|
+
|
|
785
|
+
<a name='types-template-literal'></a>
|
|
786
|
+
|
|
787
|
+
### Template Literal Types
|
|
788
|
+
|
|
789
|
+
Template Literal types are supported with `Type.TemplateLiteral`
|
|
790
|
+
|
|
757
791
|
```typescript
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
//
|
|
792
|
+
// TypeScript
|
|
793
|
+
|
|
794
|
+
type T = `option${'A'|'B'}` // type T = 'optionA' | 'optionB'
|
|
795
|
+
|
|
796
|
+
type R = Record<T, string> // type R = {
|
|
797
|
+
// optionA: string
|
|
798
|
+
// optionB: string
|
|
799
|
+
// }
|
|
800
|
+
|
|
801
|
+
// TypeBox
|
|
802
|
+
|
|
803
|
+
const T = Type.TemplateLiteral([ // const T = {
|
|
804
|
+
Type.Literal('option'), // pattern: '^option(A|B)$',
|
|
805
|
+
Type.Union([ // type: 'string'
|
|
806
|
+
Type.Literal('A'), // }
|
|
807
|
+
Type.Literal('B')
|
|
808
|
+
])
|
|
809
|
+
])
|
|
810
|
+
|
|
811
|
+
const R = Type.Record(T, Type.String()) // const R = {
|
|
812
|
+
// type: 'object',
|
|
813
|
+
// required: ['optionA', 'optionB'],
|
|
814
|
+
// properties: {
|
|
815
|
+
// optionA: {
|
|
816
|
+
// type: 'string'
|
|
817
|
+
// },
|
|
818
|
+
// optionB: {
|
|
819
|
+
// type: 'string'
|
|
820
|
+
// }
|
|
821
|
+
// }
|
|
822
|
+
// }
|
|
823
|
+
|
|
824
|
+
type T = Static<typeof T> // type T = 'optionA' | 'optionB'
|
|
825
|
+
|
|
826
|
+
type R = Static<typeof R> // type R = {
|
|
827
|
+
// optionA: string
|
|
828
|
+
// optionB: string
|
|
829
|
+
// }
|
|
764
830
|
```
|
|
765
831
|
|
|
766
832
|
<a name='types-unsafe'></a>
|
|
767
833
|
|
|
768
834
|
### Unsafe
|
|
769
835
|
|
|
770
|
-
Use `Type.Unsafe
|
|
836
|
+
Use `Type.Unsafe` to create custom schematics with user defined inference rules.
|
|
771
837
|
|
|
772
838
|
```typescript
|
|
773
839
|
const T = Type.Unsafe<string>({ type: 'number' }) // const T = {
|
|
@@ -777,7 +843,7 @@ const T = Type.Unsafe<string>({ type: 'number' }) // const T = {
|
|
|
777
843
|
type T = Static<typeof T> // type T = string
|
|
778
844
|
```
|
|
779
845
|
|
|
780
|
-
The `Type.Unsafe
|
|
846
|
+
The `Type.Unsafe` type can be useful to express specific OpenAPI schema representations.
|
|
781
847
|
|
|
782
848
|
```typescript
|
|
783
849
|
import { Type, Static, TSchema } from '@sinclair/typebox'
|
|
@@ -829,7 +895,7 @@ if(TypeGuard.TString(T)) {
|
|
|
829
895
|
|
|
830
896
|
### Strict
|
|
831
897
|
|
|
832
|
-
TypeBox schemas contain the `Kind` and `Modifier` symbol properties. These properties are used for type composition and reflection. These properties are not strictly valid JSON schema; so in some cases it may be desirable to omit them. TypeBox provides a `Type.Strict
|
|
898
|
+
TypeBox schemas contain the `Kind` and `Modifier` symbol properties. These properties are used for type composition and reflection. These properties are not strictly valid JSON schema; so in some cases it may be desirable to omit them. TypeBox provides a `Type.Strict` function that will omit these properties if necessary.
|
|
833
899
|
|
|
834
900
|
```typescript
|
|
835
901
|
const T = Type.Object({ // const T = {
|
|
@@ -905,7 +971,7 @@ const R = Value.Check(T, { x: 1 }) // const R = true
|
|
|
905
971
|
Use the Convert function to convert a value into its target type if a reasonable conversion is possible.
|
|
906
972
|
|
|
907
973
|
```typescript
|
|
908
|
-
const T = Type.Object({ x: Type.Number()
|
|
974
|
+
const T = Type.Object({ x: Type.Number() })
|
|
909
975
|
|
|
910
976
|
const R1 = Value.Convert(T, { x: '3.14' }) // const R1 = { x: 3.14 }
|
|
911
977
|
|
|
@@ -1181,16 +1247,16 @@ const R = Value.Check(T, { x: 1, y: 2 }) // const R = true
|
|
|
1181
1247
|
|
|
1182
1248
|
### Formats
|
|
1183
1249
|
|
|
1184
|
-
Use the `Format(...)` function to create a custom string format. The following creates a
|
|
1250
|
+
Use the `Format(...)` function to create a custom string format. The following creates a format that checks for lowercase strings.
|
|
1185
1251
|
|
|
1186
1252
|
```typescript
|
|
1187
1253
|
TypeSystem.Format('lowercase', value => value === value.toLowerCase()) // format should be lowercase
|
|
1188
1254
|
|
|
1189
1255
|
const T = Type.String({ format: 'lowercase' })
|
|
1190
1256
|
|
|
1191
|
-
const A = Value.Check(T, '
|
|
1257
|
+
const A = Value.Check(T, 'Hello') // const A = false
|
|
1192
1258
|
|
|
1193
|
-
const B = Value.Check(T, '
|
|
1259
|
+
const B = Value.Check(T, 'hello') // const B = true
|
|
1194
1260
|
```
|
|
1195
1261
|
|
|
1196
1262
|
<a name='typesystem-policies'></a>
|
|
@@ -1217,7 +1283,7 @@ TypeSystem.AllowNaN = true
|
|
|
1217
1283
|
|
|
1218
1284
|
## Benchmark
|
|
1219
1285
|
|
|
1220
|
-
This project maintains a set of benchmarks that measure Ajv, Value and TypeCompiler compilation and validation performance. These benchmarks can be run locally by cloning this repository and running `npm run benchmark`. The results below show for Ajv version 8.
|
|
1286
|
+
This project maintains a set of benchmarks that measure Ajv, Value and TypeCompiler compilation and validation performance. These benchmarks can be run locally by cloning this repository and running `npm run benchmark`. The results below show for Ajv version 8.12.0.
|
|
1221
1287
|
|
|
1222
1288
|
For additional comparative benchmarks, please refer to [typescript-runtime-type-benchmarks](https://moltar.github.io/typescript-runtime-type-benchmarks/).
|
|
1223
1289
|
|
|
@@ -1231,33 +1297,35 @@ This benchmark measures compilation performance for varying types. You can revie
|
|
|
1231
1297
|
┌────────────────────────────┬────────────┬──────────────┬──────────────┬──────────────┐
|
|
1232
1298
|
│ (index) │ Iterations │ Ajv │ TypeCompiler │ Performance │
|
|
1233
1299
|
├────────────────────────────┼────────────┼──────────────┼──────────────┼──────────────┤
|
|
1234
|
-
│ Literal_String │ 1000 │ '
|
|
1235
|
-
│ Literal_Number │ 1000 │ '
|
|
1236
|
-
│ Literal_Boolean │ 1000 │ '
|
|
1237
|
-
│ Primitive_Number │ 1000 │ '
|
|
1238
|
-
│ Primitive_String │ 1000 │ '
|
|
1239
|
-
│ Primitive_String_Pattern │ 1000 │ '
|
|
1240
|
-
│ Primitive_Boolean │ 1000 │ '
|
|
1241
|
-
│ Primitive_Null │ 1000 │ '
|
|
1242
|
-
│ Object_Unconstrained │ 1000 │ '
|
|
1243
|
-
│ Object_Constrained │ 1000 │ '
|
|
1244
|
-
│
|
|
1245
|
-
│
|
|
1246
|
-
│
|
|
1247
|
-
│
|
|
1248
|
-
│
|
|
1249
|
-
│
|
|
1250
|
-
│
|
|
1251
|
-
│
|
|
1252
|
-
│
|
|
1253
|
-
│
|
|
1254
|
-
│
|
|
1255
|
-
│
|
|
1256
|
-
│
|
|
1257
|
-
│
|
|
1258
|
-
│
|
|
1259
|
-
│
|
|
1260
|
-
│
|
|
1300
|
+
│ Literal_String │ 1000 │ ' 257 ms' │ ' 8 ms' │ ' 32.13 x' │
|
|
1301
|
+
│ Literal_Number │ 1000 │ ' 203 ms' │ ' 4 ms' │ ' 50.75 x' │
|
|
1302
|
+
│ Literal_Boolean │ 1000 │ ' 183 ms' │ ' 4 ms' │ ' 45.75 x' │
|
|
1303
|
+
│ Primitive_Number │ 1000 │ ' 174 ms' │ ' 8 ms' │ ' 21.75 x' │
|
|
1304
|
+
│ Primitive_String │ 1000 │ ' 158 ms' │ ' 9 ms' │ ' 17.56 x' │
|
|
1305
|
+
│ Primitive_String_Pattern │ 1000 │ ' 213 ms' │ ' 13 ms' │ ' 16.38 x' │
|
|
1306
|
+
│ Primitive_Boolean │ 1000 │ ' 136 ms' │ ' 6 ms' │ ' 22.67 x' │
|
|
1307
|
+
│ Primitive_Null │ 1000 │ ' 144 ms' │ ' 6 ms' │ ' 24.00 x' │
|
|
1308
|
+
│ Object_Unconstrained │ 1000 │ ' 1176 ms' │ ' 38 ms' │ ' 30.95 x' │
|
|
1309
|
+
│ Object_Constrained │ 1000 │ ' 1181 ms' │ ' 31 ms' │ ' 38.10 x' │
|
|
1310
|
+
│ Object_Vector3 │ 1000 │ ' 387 ms' │ ' 8 ms' │ ' 48.38 x' │
|
|
1311
|
+
│ Object_Box3D │ 1000 │ ' 1693 ms' │ ' 25 ms' │ ' 67.72 x' │
|
|
1312
|
+
│ Tuple_Primitive │ 1000 │ ' 470 ms' │ ' 15 ms' │ ' 31.33 x' │
|
|
1313
|
+
│ Tuple_Object │ 1000 │ ' 1206 ms' │ ' 17 ms' │ ' 70.94 x' │
|
|
1314
|
+
│ Composite_Intersect │ 1000 │ ' 567 ms' │ ' 20 ms' │ ' 28.35 x' │
|
|
1315
|
+
│ Composite_Union │ 1000 │ ' 515 ms' │ ' 21 ms' │ ' 24.52 x' │
|
|
1316
|
+
│ Math_Vector4 │ 1000 │ ' 787 ms' │ ' 10 ms' │ ' 78.70 x' │
|
|
1317
|
+
│ Math_Matrix4 │ 1000 │ ' 386 ms' │ ' 8 ms' │ ' 48.25 x' │
|
|
1318
|
+
│ Array_Primitive_Number │ 1000 │ ' 349 ms' │ ' 7 ms' │ ' 49.86 x' │
|
|
1319
|
+
│ Array_Primitive_String │ 1000 │ ' 336 ms' │ ' 4 ms' │ ' 84.00 x' │
|
|
1320
|
+
│ Array_Primitive_Boolean │ 1000 │ ' 284 ms' │ ' 3 ms' │ ' 94.67 x' │
|
|
1321
|
+
│ Array_Object_Unconstrained │ 1000 │ ' 1704 ms' │ ' 19 ms' │ ' 89.68 x' │
|
|
1322
|
+
│ Array_Object_Constrained │ 1000 │ ' 1456 ms' │ ' 18 ms' │ ' 80.89 x' │
|
|
1323
|
+
│ Array_Tuple_Primitive │ 1000 │ ' 792 ms' │ ' 15 ms' │ ' 52.80 x' │
|
|
1324
|
+
│ Array_Tuple_Object │ 1000 │ ' 1552 ms' │ ' 17 ms' │ ' 91.29 x' │
|
|
1325
|
+
│ Array_Composite_Intersect │ 1000 │ ' 744 ms' │ ' 18 ms' │ ' 41.33 x' │
|
|
1326
|
+
│ Array_Composite_Union │ 1000 │ ' 783 ms' │ ' 15 ms' │ ' 52.20 x' │
|
|
1327
|
+
│ Array_Math_Vector4 │ 1000 │ ' 1093 ms' │ ' 14 ms' │ ' 78.07 x' │
|
|
1328
|
+
│ Array_Math_Matrix4 │ 1000 │ ' 684 ms' │ ' 6 ms' │ ' 114.00 x' │
|
|
1261
1329
|
└────────────────────────────┴────────────┴──────────────┴──────────────┴──────────────┘
|
|
1262
1330
|
```
|
|
1263
1331
|
|
|
@@ -1271,35 +1339,37 @@ This benchmark measures validation performance for varying types. You can review
|
|
|
1271
1339
|
┌────────────────────────────┬────────────┬──────────────┬──────────────┬──────────────┬──────────────┐
|
|
1272
1340
|
│ (index) │ Iterations │ ValueCheck │ Ajv │ TypeCompiler │ Performance │
|
|
1273
1341
|
├────────────────────────────┼────────────┼──────────────┼──────────────┼──────────────┼──────────────┤
|
|
1274
|
-
│ Literal_String │ 1000000 │ '
|
|
1275
|
-
│ Literal_Number │ 1000000 │ '
|
|
1276
|
-
│ Literal_Boolean │ 1000000 │ '
|
|
1277
|
-
│ Primitive_Number │ 1000000 │ '
|
|
1278
|
-
│ Primitive_String │ 1000000 │ '
|
|
1279
|
-
│ Primitive_String_Pattern │ 1000000 │ '
|
|
1280
|
-
│ Primitive_Boolean │ 1000000 │ ' 23 ms' │ '
|
|
1281
|
-
│ Primitive_Null │ 1000000 │ '
|
|
1282
|
-
│ Object_Unconstrained │ 1000000 │ '
|
|
1283
|
-
│ Object_Constrained │ 1000000 │ '
|
|
1284
|
-
│
|
|
1285
|
-
│
|
|
1286
|
-
│
|
|
1287
|
-
│
|
|
1288
|
-
│
|
|
1289
|
-
│
|
|
1290
|
-
│
|
|
1291
|
-
│
|
|
1292
|
-
│
|
|
1293
|
-
│
|
|
1294
|
-
│
|
|
1295
|
-
│
|
|
1296
|
-
│
|
|
1297
|
-
│
|
|
1298
|
-
│
|
|
1299
|
-
│
|
|
1300
|
-
│
|
|
1301
|
-
│
|
|
1302
|
-
│
|
|
1342
|
+
│ Literal_String │ 1000000 │ ' 27 ms' │ ' 6 ms' │ ' 5 ms' │ ' 1.20 x' │
|
|
1343
|
+
│ Literal_Number │ 1000000 │ ' 23 ms' │ ' 21 ms' │ ' 11 ms' │ ' 1.91 x' │
|
|
1344
|
+
│ Literal_Boolean │ 1000000 │ ' 21 ms' │ ' 20 ms' │ ' 10 ms' │ ' 2.00 x' │
|
|
1345
|
+
│ Primitive_Number │ 1000000 │ ' 26 ms' │ ' 19 ms' │ ' 11 ms' │ ' 1.73 x' │
|
|
1346
|
+
│ Primitive_String │ 1000000 │ ' 25 ms' │ ' 19 ms' │ ' 10 ms' │ ' 1.90 x' │
|
|
1347
|
+
│ Primitive_String_Pattern │ 1000000 │ ' 155 ms' │ ' 49 ms' │ ' 43 ms' │ ' 1.14 x' │
|
|
1348
|
+
│ Primitive_Boolean │ 1000000 │ ' 23 ms' │ ' 19 ms' │ ' 10 ms' │ ' 1.90 x' │
|
|
1349
|
+
│ Primitive_Null │ 1000000 │ ' 24 ms' │ ' 19 ms' │ ' 10 ms' │ ' 1.90 x' │
|
|
1350
|
+
│ Object_Unconstrained │ 1000000 │ ' 804 ms' │ ' 35 ms' │ ' 28 ms' │ ' 1.25 x' │
|
|
1351
|
+
│ Object_Constrained │ 1000000 │ ' 1041 ms' │ ' 55 ms' │ ' 41 ms' │ ' 1.34 x' │
|
|
1352
|
+
│ Object_Vector3 │ 1000000 │ ' 380 ms' │ ' 26 ms' │ ' 20 ms' │ ' 1.30 x' │
|
|
1353
|
+
│ Object_Box3D │ 1000000 │ ' 1785 ms' │ ' 65 ms' │ ' 52 ms' │ ' 1.25 x' │
|
|
1354
|
+
│ Object_Recursive │ 1000000 │ ' 4984 ms' │ ' 396 ms' │ ' 114 ms' │ ' 3.47 x' │
|
|
1355
|
+
│ Tuple_Primitive │ 1000000 │ ' 168 ms' │ ' 24 ms' │ ' 16 ms' │ ' 1.50 x' │
|
|
1356
|
+
│ Tuple_Object │ 1000000 │ ' 673 ms' │ ' 30 ms' │ ' 26 ms' │ ' 1.15 x' │
|
|
1357
|
+
│ Composite_Intersect │ 1000000 │ ' 751 ms' │ ' 28 ms' │ ' 20 ms' │ ' 1.40 x' │
|
|
1358
|
+
│ Composite_Union │ 1000000 │ ' 489 ms' │ ' 24 ms' │ ' 16 ms' │ ' 1.50 x' │
|
|
1359
|
+
│ Math_Vector4 │ 1000000 │ ' 259 ms' │ ' 23 ms' │ ' 13 ms' │ ' 1.77 x' │
|
|
1360
|
+
│ Math_Matrix4 │ 1000000 │ ' 1002 ms' │ ' 40 ms' │ ' 30 ms' │ ' 1.33 x' │
|
|
1361
|
+
│ Array_Primitive_Number │ 1000000 │ ' 252 ms' │ ' 22 ms' │ ' 15 ms' │ ' 1.47 x' │
|
|
1362
|
+
│ Array_Primitive_String │ 1000000 │ ' 227 ms' │ ' 22 ms' │ ' 18 ms' │ ' 1.22 x' │
|
|
1363
|
+
│ Array_Primitive_Boolean │ 1000000 │ ' 150 ms' │ ' 23 ms' │ ' 22 ms' │ ' 1.05 x' │
|
|
1364
|
+
│ Array_Object_Unconstrained │ 1000000 │ ' 4754 ms' │ ' 71 ms' │ ' 64 ms' │ ' 1.11 x' │
|
|
1365
|
+
│ Array_Object_Constrained │ 1000000 │ ' 4787 ms' │ ' 142 ms' │ ' 123 ms' │ ' 1.15 x' │
|
|
1366
|
+
│ Array_Object_Recursive │ 1000000 │ ' 19088 ms' │ ' 1735 ms' │ ' 314 ms' │ ' 5.53 x' │
|
|
1367
|
+
│ Array_Tuple_Primitive │ 1000000 │ ' 650 ms' │ ' 41 ms' │ ' 31 ms' │ ' 1.32 x' │
|
|
1368
|
+
│ Array_Tuple_Object │ 1000000 │ ' 2770 ms' │ ' 67 ms' │ ' 55 ms' │ ' 1.22 x' │
|
|
1369
|
+
│ Array_Composite_Intersect │ 1000000 │ ' 2693 ms' │ ' 50 ms' │ ' 39 ms' │ ' 1.28 x' │
|
|
1370
|
+
│ Array_Composite_Union │ 1000000 │ ' 1982 ms' │ ' 72 ms' │ ' 33 ms' │ ' 2.18 x' │
|
|
1371
|
+
│ Array_Math_Vector4 │ 1000000 │ ' 1068 ms' │ ' 40 ms' │ ' 26 ms' │ ' 1.54 x' │
|
|
1372
|
+
│ Array_Math_Matrix4 │ 1000000 │ ' 4609 ms' │ ' 115 ms' │ ' 88 ms' │ ' 1.31 x' │
|
|
1303
1373
|
└────────────────────────────┴────────────┴──────────────┴──────────────┴──────────────┴──────────────┘
|
|
1304
1374
|
```
|
|
1305
1375
|
|
|
@@ -1313,11 +1383,11 @@ The following table lists esbuild compiled and minified sizes for each TypeBox m
|
|
|
1313
1383
|
┌──────────────────────┬────────────┬────────────┬─────────────┐
|
|
1314
1384
|
│ (index) │ Compiled │ Minified │ Compression │
|
|
1315
1385
|
├──────────────────────┼────────────┼────────────┼─────────────┤
|
|
1316
|
-
│ typebox/compiler │ '
|
|
1317
|
-
│ typebox/errors │ '
|
|
1318
|
-
│ typebox/system │ '
|
|
1319
|
-
│ typebox/value │ '
|
|
1320
|
-
│ typebox │ '
|
|
1386
|
+
│ typebox/compiler │ '124.3 kb' │ ' 55.7 kb' │ '2.23 x' │
|
|
1387
|
+
│ typebox/errors │ '107.8 kb' │ ' 47.9 kb' │ '2.25 x' │
|
|
1388
|
+
│ typebox/system │ ' 73.3 kb' │ ' 30.2 kb' │ '2.43 x' │
|
|
1389
|
+
│ typebox/value │ '170.7 kb' │ ' 74.2 kb' │ '2.30 x' │
|
|
1390
|
+
│ typebox │ ' 72.0 kb' │ ' 29.7 kb' │ '2.43 x' │
|
|
1321
1391
|
└──────────────────────┴────────────┴────────────┴─────────────┘
|
|
1322
1392
|
```
|
|
1323
1393
|
|