typedriver 0.8.1 → 0.8.3
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/build/guard/standard-json-schema.mjs +1 -1
- package/build/guard/standard-schema.mjs +1 -1
- package/build/index.d.mts +1 -0
- package/build/index.mjs +1 -0
- package/build/json-schema/validator.d.mts +1 -0
- package/build/json-schema/validator.mjs +6 -0
- package/build/locale.d.mts +6 -0
- package/build/locale.mjs +7 -0
- package/build/standard-json-schema/validator.d.mts +1 -0
- package/build/standard-json-schema/validator.mjs +6 -0
- package/build/standard-schema/validator.d.mts +1 -0
- package/build/standard-schema/validator.mjs +6 -0
- package/build/typescript/validator.d.mts +1 -0
- package/build/typescript/validator.mjs +6 -0
- package/build/validator.d.mts +2 -0
- package/package.json +1 -1
- package/readme.md +146 -38
|
@@ -18,7 +18,7 @@ function IsStandardJsonSchemaV1Props(value) {
|
|
|
18
18
|
}
|
|
19
19
|
function IsTypicalStandardJsonSchemaV1(value) {
|
|
20
20
|
return Guard.IsObject(value) &&
|
|
21
|
-
Guard.
|
|
21
|
+
!Guard.IsUndefined(value['~standard']) &&
|
|
22
22
|
IsStandardJsonSchemaV1Props(value['~standard']);
|
|
23
23
|
}
|
|
24
24
|
// ArkType (Obviously)
|
|
@@ -13,7 +13,7 @@ function IsStandardSchemaV1Props(value) {
|
|
|
13
13
|
}
|
|
14
14
|
function IsTypicalStandardSchemaV1(value) {
|
|
15
15
|
return Guard.IsObject(value) &&
|
|
16
|
-
Guard.
|
|
16
|
+
!Guard.IsUndefined(value['~standard']) &&
|
|
17
17
|
IsStandardSchemaV1Props(value['~standard']);
|
|
18
18
|
}
|
|
19
19
|
// ArkType (Obviously)
|
package/build/index.d.mts
CHANGED
package/build/index.mjs
CHANGED
|
@@ -21,6 +21,7 @@ export declare class JsonSchemaValidator<Input extends Type.TSchema, Output exte
|
|
|
21
21
|
private readonly input;
|
|
22
22
|
private readonly validator;
|
|
23
23
|
constructor(input: Input);
|
|
24
|
+
accelerated(): boolean;
|
|
24
25
|
schema(): Input;
|
|
25
26
|
isJsonSchema(): boolean;
|
|
26
27
|
toJsonSchema(): unknown;
|
|
@@ -26,6 +26,12 @@ export class JsonSchemaValidator extends Validator {
|
|
|
26
26
|
this.validator = new TBValidator({}, input);
|
|
27
27
|
}
|
|
28
28
|
// ----------------------------------------------------------------
|
|
29
|
+
// Accelerated
|
|
30
|
+
// ----------------------------------------------------------------
|
|
31
|
+
accelerated() {
|
|
32
|
+
return this.validator.IsEvaluated();
|
|
33
|
+
}
|
|
34
|
+
// ----------------------------------------------------------------
|
|
29
35
|
// Schema
|
|
30
36
|
// ----------------------------------------------------------------
|
|
31
37
|
schema() {
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { System } from 'typebox/system';
|
|
2
|
+
declare const Locale: typeof System.Locale;
|
|
3
|
+
type LocaleString = Exclude<keyof typeof Locale, 'Get' | 'Set' | 'Reset'> & ({} & string);
|
|
4
|
+
/** Sets the locale for which errors are generated. */
|
|
5
|
+
export declare function locale(locale: LocaleString): void;
|
|
6
|
+
export {};
|
package/build/locale.mjs
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { System } from 'typebox/system';
|
|
2
|
+
const Locale = System.Locale;
|
|
3
|
+
/** Sets the locale for which errors are generated. */
|
|
4
|
+
export function locale(locale) {
|
|
5
|
+
const F = locale in System.Locale ? System.Locale[locale] : System.Locale.en_US;
|
|
6
|
+
System.Locale.Set(F);
|
|
7
|
+
}
|
|
@@ -16,6 +16,7 @@ export declare class StandardJsonSchemaValidator<Input extends StandardJSONSchem
|
|
|
16
16
|
private readonly input;
|
|
17
17
|
private readonly validator;
|
|
18
18
|
constructor(input: Input);
|
|
19
|
+
accelerated(): boolean;
|
|
19
20
|
schema(): Input;
|
|
20
21
|
isJsonSchema(): boolean;
|
|
21
22
|
toJsonSchema(): unknown;
|
|
@@ -23,6 +23,12 @@ export class StandardJsonSchemaValidator extends Validator {
|
|
|
23
23
|
this.validator = new TBValidator({}, schema);
|
|
24
24
|
}
|
|
25
25
|
// ----------------------------------------------------------------
|
|
26
|
+
// Accelerated
|
|
27
|
+
// ----------------------------------------------------------------
|
|
28
|
+
accelerated() {
|
|
29
|
+
return this.validator.IsEvaluated();
|
|
30
|
+
}
|
|
31
|
+
// ----------------------------------------------------------------
|
|
26
32
|
// Schema
|
|
27
33
|
// ----------------------------------------------------------------
|
|
28
34
|
schema() {
|
|
@@ -3,6 +3,7 @@ import { Validator } from '../validator.mjs';
|
|
|
3
3
|
export declare class StandardSchemaValidator<Input extends StandardSchemaV1, Output extends unknown = StandardSchemaV1.InferOutput<Input>> extends Validator<Input, Output> {
|
|
4
4
|
private readonly input;
|
|
5
5
|
constructor(input: Input);
|
|
6
|
+
accelerated(): boolean;
|
|
6
7
|
schema(): Input;
|
|
7
8
|
isJsonSchema(): boolean;
|
|
8
9
|
toJsonSchema(): unknown;
|
|
@@ -7,6 +7,12 @@ export class StandardSchemaValidator extends Validator {
|
|
|
7
7
|
this.input = input;
|
|
8
8
|
}
|
|
9
9
|
// ----------------------------------------------------------------
|
|
10
|
+
// Accelerated
|
|
11
|
+
// ----------------------------------------------------------------
|
|
12
|
+
accelerated() {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
// ----------------------------------------------------------------
|
|
10
16
|
// Schema
|
|
11
17
|
// ----------------------------------------------------------------
|
|
12
18
|
schema() {
|
|
@@ -5,6 +5,7 @@ export declare class TypeScriptValidator<Input extends string, Schema extends Ty
|
|
|
5
5
|
private readonly script;
|
|
6
6
|
private readonly jsonschema;
|
|
7
7
|
constructor(script: Input);
|
|
8
|
+
accelerated(): boolean;
|
|
8
9
|
schema(): Input;
|
|
9
10
|
isJsonSchema(): boolean;
|
|
10
11
|
toJsonSchema(): unknown;
|
|
@@ -11,6 +11,12 @@ export class TypeScriptValidator extends Validator {
|
|
|
11
11
|
this.validator = new TBValidator({}, this.jsonschema);
|
|
12
12
|
}
|
|
13
13
|
// ----------------------------------------------------------------
|
|
14
|
+
// Accelerated
|
|
15
|
+
// ----------------------------------------------------------------
|
|
16
|
+
accelerated() {
|
|
17
|
+
return this.validator.IsEvaluated();
|
|
18
|
+
}
|
|
19
|
+
// ----------------------------------------------------------------
|
|
14
20
|
// Schema
|
|
15
21
|
// ----------------------------------------------------------------
|
|
16
22
|
schema() {
|
package/build/validator.d.mts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/** Abstract Base for all Validator types. */
|
|
2
2
|
export declare abstract class Validator<Input extends unknown = unknown, Output extends unknown = unknown> {
|
|
3
|
+
/** Returns true if this validator is accelerated */
|
|
4
|
+
abstract accelerated(): boolean;
|
|
3
5
|
/** Returns the schema used to construct this validator */
|
|
4
6
|
abstract schema(): Input;
|
|
5
7
|
/** Checks a value matches the given schema */
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
<h1>TypeDriver</h1>
|
|
4
4
|
|
|
5
|
-
<p>Integration
|
|
5
|
+
<p>Integration Middlware for High Performance Runtime Validation</p>
|
|
6
6
|
|
|
7
7
|
<img src="typedriver.png" />
|
|
8
8
|
|
|
@@ -23,47 +23,70 @@ $ npm install typedriver
|
|
|
23
23
|
|
|
24
24
|
## Example
|
|
25
25
|
|
|
26
|
+
Framework Compiler for JSON Schema, Standard Schema and TypeScript
|
|
27
|
+
|
|
28
|
+
Ref: [TypeScript](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoAqBPADgUwMoDGATgJbYAuqYStd9D4ypAttgPbEWgDeoh7NqQA2uUAF9QAM2KDQAcgo5cAEzIA3XMXnJkAgHYBnbgDVchCpwDMoALz9B2EbgAUAAx7JQoAB4AuUH0AVxYAIy0AGi9QTADgsMjogC84kPDiZHE3AEpdFXNhAENiMQNjUHVC4SDcAKD9AGt9dgB3fV0y7g5DUgpSdn07UDMLawA6bGLDV0rq3GzvRaXFtE7Qbt7+-QDPZb39g8OjpbRvf0C0xOPrm5vTmNSE4ijb17eTsG8Ui6fo9--rmhJPYxqCgA) | [JSON Schema](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoBSBnA9gO1AMoDGAFgKYC2AhqmEvQ40+MgJYUAO2ATgC6gBvUEWydWAGzKgAvqABm3UaADkvAJ4cyAE26sAbmW7LkyEbkz8AamSK8eAZlABeYaI4SyACgHJQodZoAXCrYAEYAVja8ygA0vqDcZACOAK6siVrBANrKAB6xKmoFygBeygC6cX4cipp8rGSYwT5+frnN-hpkwcq4KRShhsoyVa1qHQHdKn0DQyPxfiUTXT0zg0Yy8dLI0gCUJlo24lSJrub8elTiKVMpuADWuNgA7rgmZhagXJisvKx4zlA1lsDgAdBwTpgvJdrmRdq0EYi0B9+N9fv9cM0Foicbi8fiCaA0G1gmtDKNCZSqYTiaBxqAydwKdSWayiWBFqT+utsWy+QS0LIXKCRUA) | [TypeBox](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoBSBnA9gO1AMoDGAFgKYC2AhgFygAqAngA5kBC2AHqmEn-wMHhkASwrNsAJwAuoAN6gi2cSIA2ZUAF9QAM0nLQAcmksyAE0kiAbmUmHR4qbKatd+ikZOsARl0OgA5GQlXExZADUyImkpAGZQAF5FZWY1MgAKFzIAOgB5bwArKOl0uWQAzjos7IA5AFcKb1t0gEoAGnLQRirTWoamyVaOgIAvHtY+xuaW5E0WmeQzKNUqSQ0QsNArKlU6sjo63ABrXGwAd1wgjdkJTBFpETxE0EjouOzmVcwM7d2yFoCgKBgLQ11At3uj1wdDKwLh8IRiKRQLQFTouH6tmGyJxuJxqK66MxkmxeLJ5ICBLGoAxU0knQpjPxYG0SWy7KAA) | [Zod](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoDKAXAhgOwCbYBO+GAxgBYCmAttgFygBaA9vqmEl9z7+MgEsaABxZFMoAN6gyLEQIA2VUAF9QAMyJzQAckwBPYVXxEBANypEdyQSLESAVKGwBnUAC8NWmrvdtryLK4LhIAalRkmGIAzKAAvDJywopUABTuAHQsAEYAVhGYqZLIoKAAHoyZuACuNNmWqQCUADQloPqVGTV1DS1t7p3d9URNyCqNjTb4EQrEykEhoGbYCtVUjNW4ANa4LADuuDYLEqIuApgCLLjxoOGRMRnCxC5py6tUjaVf319ox6Cnc6XXCMYo-cEQyFQ6HfNClCqgIaWVow1FotFw9qMJFEFHo-EE35gUoDRG1YZtQlU1FoNQJDIMoA) | [Valibot](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoDKAXAhgOwCbYBO+GAxgBYCmAttgFygBq2ANgJYBGA9pqmEkFDhI8MnY0ADtyKZQAb1BluU9qyqgAvqABmRFaADkmAJ6Sq+IuwBuVIoeTipMuQCpQ2AM6hru-TSNrNi5eB2RlXE85JioyTBkAZlAAXiUVSTUqAAprADpuTgArWMws+WRQUAAPRjzcAFcaTjssgEoAGgrQE1rchqaWjq6AL17+5qI25E1W1sd8WNZiDQionzZ6qkZ63ABrXG4Ad1xHVblpT3ZMdm5cFOYSxNzJYk9soNZN1sqf35+0M6gC5XG64RjlP6QqHQmGw35oSo1UDjOydOHojEYhHdRgoohozGEon-MCVUbIxoTLrEmnotDaVK5JlAA) | [ArkType](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoDKAXAhgOwCbYBO+GAxgBYCmAttgFygCCRA1gCoCeADlamEkFDhI8MgCWNbgHsimUAG9QZaVPEAbKqAC+oAGZFVoAOSYeVfEXEA3KkWPIJU2fKVneO-YZonird1QOyCq4AM7yAGpUZJiyAMygALzKqtwaVAAUARkKyKCgAB6MxrgArjQARnbGADR5oJzFZZXVdfkAXk3lVfbI2gCU-Y740erEWiHhoNbY6qVUjKW4rLjSAO64jpPyMqHimOLSuEmgUTHxAHTcxKGZM3NU-fnPL89o26C7+4e4jLmvAMBQOBIJeaHyRVAzR6bVBcPhcPBDUY0LssIRGMx+SRnSh3Ts9SxRMRYF0yQuFKAA) | [Effect](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoDKAXAhgOwCbYBO+GAxgBYCmAttgFygCiAZi1WZqmEr3-wPDIAljQAOAeyKZQAb1BkJ44QBsqoAL6gWRJaADkmAJ5iq+IsIBuVIvpHipM+ekq1sm7bpoGqbDpjtkRVwAZxkANX8pAGZQAF4FJTFVKgAKF2o6ADowvEISDLdwgEZ012ysIgBXTlTZZFBQAA9GQuyAOSqaACMbABoG0CNW8uwszp7+wYAvEcyxid6iZA0ASnXkZHwOFWJ1YLDQS2wVKqpGKtwAa1wJAHdcTYOZSRDhTGEJXHjQSM4YrJiYghNLHU5UVaNKHQqFoZ6gV7vT64Rj1GHojGYrHY6FoRpEKjYfBfFRGZqMXBdJYDHG0ul0vGgAlEklk4agSmTIg0+m8vmwsD4wnE3Ck0CzDlUmyDfmy2loLQJLLKoA) | [Arri](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoDKAXAhgOwCbYBO+GAxgBYCmAttgFygCCRRAlqmEtz73+MjY0ADgHsimUAG9QZUSLYAbKqAC+oAGZF5oAOSYAnsKr52ANypFdgkeMkzsazdpp6AAsXZFhZYAGdKWmxrZDlcP0kANSoyTHEAZlAAXll5YSUqAApsADpRACMAKxjMTKlkUFAAD0Zc3ABXGnzLTIBKABoK0ANanIamlo6ugC9e-uaiNuRVVtbkZHwYxWIVMIjQM2xFeqpGetwAa1xRAHdcebXJMT82TDZRXGTQaNiEnOFiPyzN7apWyoBgIBaEuoGut3uuEY5SBsLh8IRiMBaEqNVA40snSR2JxOJR3UYGKIWNxpLJwLAlVG6MaEy65IZ2LQ6hSOTZQA) | [Sury](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoDKAXAhgOwCbYBO+GAxgBYCmAttgFwYCuRAnqmEl9z7+MgEsaABwD2RTKADeoMqJECANlVABfUADMi80AHJMrYVXxEBANypFdgkeMkAqUNgDOGTdpp7nLVrtD+AwKDg-zQAFgA6AGZQAGsAIVAAChoBXFAAalAAcwAvAWEASmRkOVxnSQA1KjJMcRiAXll5YSUqJPQI50pabCSpZH8AD0ZO3CYaACNLABpB0FZRiPGp2fncpZXpojnVQuLkfBrFYhUyitAzbEUmKkYmXFjcUQB3XBLzyTFnAUwBUXSTWqtXqEWExGc7SuNyohRCQTQn1A31+-1wjAG8Kx2JxOLQw0YWzWuJJpOx+IWhIm2zmZLp9NAFI2oCJRHmDI55LA6iaET5QA)
|
|
29
|
+
|
|
30
|
+
|
|
26
31
|
```typescript
|
|
27
32
|
import { compile } from 'typedriver'
|
|
28
33
|
|
|
29
|
-
const
|
|
34
|
+
const Vector3 = compile(`{
|
|
30
35
|
x: number,
|
|
31
36
|
y: number,
|
|
32
37
|
z: number
|
|
33
38
|
}`)
|
|
34
39
|
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
+
declare const value: unknown
|
|
41
|
+
|
|
42
|
+
const position = Vector3.parse(value) // const position: {
|
|
43
|
+
// x: number,
|
|
44
|
+
// y: number,
|
|
45
|
+
// z: number
|
|
46
|
+
// } = ...
|
|
40
47
|
```
|
|
41
48
|
|
|
42
49
|
## Overview
|
|
43
50
|
|
|
44
|
-
TypeDriver is a
|
|
51
|
+
TypeDriver is a high-performance validation middleware that enables frameworks to incorporate both JSON Schema and Standard Schema specifications into framework interfaces (for example, HTTP route handlers, RPC endpoints, etc.). It provides a unified validation and type inference system to simplify integrating runtime types, and offers a TypeScript DSL compiler and inference system as standard.
|
|
45
52
|
|
|
46
|
-
TypeDriver
|
|
53
|
+
> TypeDriver unifies heterogeneous runtime schema systems based on JSON Schema and Standard Schema into a single system that preserves static type inference, runtime validation, and schema reflection, while remaining compatible with multiple schema ecosystems.
|
|
54
|
+
|
|
55
|
+
TypeDriver provides high-performance validation for JSON Schema Drafts 3 through 2020-12. Internally, it uses TypeBox for schema validation and type inference, and Standard Schema for remote library integration. The TypeScript DSL provides runtime and type-level TypeScript emulation in support of TS 7 native compiler.
|
|
47
56
|
|
|
48
57
|
License MIT
|
|
49
58
|
|
|
59
|
+
## Features
|
|
60
|
+
|
|
61
|
+
- Designed Specifically for Framework Integration
|
|
62
|
+
- Integrated TypeScript DSL for TS7 Native (supported in TS5 and above)
|
|
63
|
+
- Unified Validation for JSON Schema and Standard Schema
|
|
64
|
+
- Unified Inference for JSON Schema and Standard Schema
|
|
65
|
+
- High Performance Validation Compiler (performance approx 2x Ajv)
|
|
66
|
+
- Acceleration support for Standard JSON Schema
|
|
67
|
+
- JSON Schema Reflect for OpenAPI, MCP and IDL Based Systems
|
|
68
|
+
- Localization Support for 40+ languages
|
|
69
|
+
|
|
50
70
|
## Contents
|
|
51
71
|
|
|
52
72
|
- [Overview](#Overview)
|
|
73
|
+
- [Features](#Features)
|
|
53
74
|
- [Framework](#Framework)
|
|
54
75
|
- [Compile](#Compile)
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
76
|
+
- [Check](#Check)
|
|
77
|
+
- [Parse](#Parse)
|
|
78
|
+
- [Errors](#Errors)
|
|
58
79
|
- [Static](#Static)
|
|
59
80
|
- [Reflect](#Schema)
|
|
81
|
+
- [Locale](#Locale)
|
|
82
|
+
- [Accelerate](#Accelerate)
|
|
60
83
|
- [Contribute](#Contribute)
|
|
61
84
|
|
|
62
85
|
## Framework
|
|
63
86
|
|
|
64
|
-
TypeDriver is designed
|
|
87
|
+
TypeDriver is designed for framework integration to allow multiple type safe libraries to be hosted on framework interfaces. It provides a simple infrastructure to connect type inference and validation to the framework. The following shows a simple design for a route function ...
|
|
65
88
|
|
|
66
|
-
[
|
|
89
|
+
Ref: [TypeScript](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YyAoEoAiBTAzgSwOYB2qYSZ5FcyADgPbYAuAFAOTAsA0oA3sqKACNaAEwCeALlAADXv34APSYQCuAWwGYAThz5yJoFeq065oAF5K1GzboC+U5La5NNmAI7KcDAJSgAvAB8PLoAxrSEjDyg8lyiXGagtv6grh5eAHRCYqBoDKLUmODYAIYAZpgAhI7eyCQQlA2N8HUAYprFqpgA7rSaANZ1TUPDyLiqdJoMUXkFoADKDMUMuCFcYeO4ADaYiaClmrSqoCwzmMKauABuWiy1mPITU7iEDFqlxSE7AEq0yq8A8tRluFsAAeABCIlEoHur0IwmwoGUhD6hFoXUIyWRqPRhCCskEUMkkLEjmQ90eoFOoB+f0wAGFiptNgIPn1QQAFJYACxh8jhCNAjAuhHwXEBwIifIFiNpAKBuBBQT8oCYuhc7k8jEk3CRmk2ki5DG5XCy+gWSxWoIlioiAG0WGaWABdIK2XyBUCXWi4YTIGoU3pTUrIkKS0B0RicnnSzDwxHC55i0BhCJTG0g2Pxmm-eWS7BcXSM5mskJ9ZJyhlMlls6PG8UKpXIAJMag8w08ri0RsRSQZiJraulvqSYs1su+AmpyJmgD6lyZvqWvWS62oW0wTG7+YdTudNX4uREtEk6TPoDG1G2nReKVzO02tHwKzJQA) | [JSON Schema](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YyAoEoAiBTAzgSwOYB2qYSZ5FcyADgPbYAuAFAOTAsA0oA3sqKACNaAEwCeALh59+oBqOqZJLWgIBWmAMYNO0-gCdMARwCuuA8MkBtFgA9OoFqPssAXiwC6HXaGp7aCvQZcHEleGRkbUNl5RQdCYwBbAUw9FlAAXy9w-gkeaIUleKSUtMzvfhcouQK4xOTUjKyZdOkWzNAmAxMcBgBKUABeAD4pfg1aQkY8my5RLhcMwdAu4x6AOiExUDRqzHBsAEMAM0wAQmR03uQSCEo7+-gbgDE9A4TMAHdaPQBrG4eAYDkLgEnRAnldqAAMoMA5BDRccag3AAG0wiyOfgSDl2wj0uAAbiVrpgbGCGKBcIQGCkjgcNOiAEq0Yw0gDy1CCE2wAB4AEIiUSgUk0wjCbCgYyEH6EWgfQhLKUyuWEEZhQSCyQCsQXZCk8n5JksmkAYQOKJRAnpPx5AAU4QALYU2UXi0CMfGEfBcDlcybO10S5mszC+3DckYDDrSTpGVaMKLGPQoyT2hgOribXIwuG4DQ8sPc6xZjwjS6DEYE2i4YTIK7674Uo5SrThhV0Rh2x0BzBiiUeqne0DjSYUwv+kW9t3B9mctvYLjSM0Wq0aH5LGeYZeW61d9M+ucR5BDJjUR2px1cfx+7CSccL4fmndryTb1c-frqkdTLMAfQJ5o1nC3xLEi1CopgTDXvOxaCh4Vz8DsIi0JIaxoZSoJou81LLMa6IorQ+B5rqQA) | [Standard Schema](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YyAoEoAiBTAzgSwOYB2qYSZ5FcyuAtgA4D2ATgC6gBUoAhtqAF6gAZkwY1QAcj4MAJuOTJG2FgApxwcQBpQAb2ShQAIxkBPAFz8AdAwMArTAGMVu-foAe5vhcIBXGgcxMygCUGnouZpY+fgHBoS78Hl6+-oEhYQC+QcjpWspMmACO3jgsQaAAvAB8OmH2DIRKOqCuWsZaAukVoPlFJRZG0sagaCzGdJjg2FyCmACE2VkkEJQrq-BLAGJMXDSYAO7MANZLa6dn1PTMbNqgo+OgAMosXCy49lp19LgANpigncJRBI7phpExcAA3AJyZCYVyMVigXCEFgBQRcex-ABKDG8qIA8nRXvVsAAeABCJlAcNRhGkvG8hEOhAYe0IXUZzNZhGqzkMJnMlMG2Vh8KutzG2NxqIAwlxvt8DBjDqSAAovAAW1NctPpoCU4MI+C0hOJDW1ut4OLxmFNuBJ1XKoGUYTyhWKSnMN28TG+5nVLA1WgGESeLzepLtJIA2uIQ+IALrVTIVaoQhi4aTILJwhFsQSMxz29mKFhqzUWzB03gG5HG0B1BpsKPmmlVvXWglE4vYLRhOUKpX2Q5dTuYAeK5XlwMm7sO5CVZR0TX+zVaBhzhrmFu9hvyyfD8wToeHMp8xuNEMAfQh8szL2YXU+dB+mGUG7N2Fj8YTWX0IxkBhzAsECkXoX5dhRbppT+b4GHwN4RSAA)
|
|
67
90
|
|
|
68
91
|
```typescript
|
|
69
92
|
post('/', {
|
|
@@ -77,7 +100,12 @@ post('/', {
|
|
|
77
100
|
})
|
|
78
101
|
```
|
|
79
102
|
|
|
80
|
-
|
|
103
|
+
Where the above design is achieved with the following TS definitions (Expand to View)
|
|
104
|
+
|
|
105
|
+
<details>
|
|
106
|
+
|
|
107
|
+
<summary>TypeDriver Route Definition</summary>
|
|
108
|
+
|
|
81
109
|
|
|
82
110
|
```typescript
|
|
83
111
|
import { type Static, compile } from 'typedriver'
|
|
@@ -96,29 +124,30 @@ export function post<Path extends string, const Options extends RouteOptions,
|
|
|
96
124
|
}
|
|
97
125
|
```
|
|
98
126
|
|
|
127
|
+
</details>
|
|
99
128
|
|
|
100
129
|
## Compile
|
|
101
130
|
|
|
102
|
-
TypeDriver consists of a
|
|
131
|
+
TypeDriver consists of a single compile(...) function that accepts JSON Schema, Standard Schema or TypeScript definition and returns a Validator instance.
|
|
103
132
|
|
|
104
133
|
```typescript
|
|
105
134
|
import { compile } from 'typedriver'
|
|
106
135
|
```
|
|
107
136
|
|
|
108
|
-
|
|
137
|
+
TypeScript [Example](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYwuYAbApnAvnAMyjTgHIYBPMLAEymADcspSAoV1AOwGd4A1LMhjQAzHAC8KNGExYAFAAMErOHAAeALjicAriABGzFXApbdBo6oBeZvYaitcCgJSq37j56-ef7gPR+Ujz8gsJQIlp8AIYYwDRRYQA8AHSpADSIxr7ZObluAW6a2nbMaVl5FZX5gaqmxRZQZVXNlQXWtg1NLd3ZBbgAfOxcvHCQ3MAwwBCcEnACQqLJYFFQ3PJIxkUADF11AIxdNnBbjs5AA)
|
|
109
138
|
|
|
110
139
|
```typescript
|
|
111
|
-
const
|
|
140
|
+
const Vector3 = compile(`{
|
|
112
141
|
x: number
|
|
113
142
|
y: number
|
|
114
143
|
z: number
|
|
115
144
|
}`)
|
|
116
145
|
```
|
|
117
146
|
|
|
118
|
-
|
|
147
|
+
JSON Schema [Example](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYwuYAbApnAvnAMyjTgHIYBPMLAEymADcspSAoV1AOwGd4A1LMhjQAzHAC8KNGExYAFEjhLlK1WtUB6DVJ79BwqCIBccPgEMMwGmYMAeAHSOANIlZLK1E6QgAjAFb6pC7qIaFhWsoAHiacAK4gPsxObnBQWACOscBpNCYA2qSRQWQUxaQAXqQAusFhdXARShQx8YlQyUpgxNSwwFjcJor1w+HaSuUtCcwpStGIcB5YXnFTLHi1I6ERuAB8M3DN84vLrcyk6-sTR1RLZCtt57gpT7gAlOxcvHCQ3MAwwBBOBJTPpRPYwGYoNx5EgUnMAAwdA4mACMSKu8NYbyAA)
|
|
119
148
|
|
|
120
149
|
```typescript
|
|
121
|
-
const
|
|
150
|
+
const Vector3 = compile({
|
|
122
151
|
type: 'object',
|
|
123
152
|
required: ['x', 'y', 'z'],
|
|
124
153
|
properties: {
|
|
@@ -129,12 +158,12 @@ const Vec3 = compile({
|
|
|
129
158
|
})
|
|
130
159
|
```
|
|
131
160
|
|
|
132
|
-
|
|
161
|
+
Standard Schema [Example](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYwuYAbApnAvnAMyjTgHIYBPMLAEymADcspSAoV0SWOAKjgEMAznABehYiDIiINNq1QA7QfABqWZDGgBmOAF4UaMJiwAKEQDoIAIwBW6mCaRxnL12+cB6DwaWr72gC44FX4MYBp+TSgAHnM4gBpEVmcADyCLBQBXECtmEwBKRPdiktK3Lxc0uCyc5njkuAp08xrcqAKisq7uuArnJurstvrnEWbWvMKe6dK+0SCJqFZcfPyZ9Y3e71wAPnZFZThIQWAYYAgFPWD-KC1zMH4oQVMkBqqABhHGoIBGL7G4O9lvkgA)
|
|
133
162
|
|
|
134
163
|
```typescript
|
|
135
164
|
import * as z from 'zod'
|
|
136
165
|
|
|
137
|
-
const
|
|
166
|
+
const Vector3 = compile(z.object({
|
|
138
167
|
x: z.number(),
|
|
139
168
|
y: z.number(),
|
|
140
169
|
z: z.number(),
|
|
@@ -146,47 +175,47 @@ const Vec3 = compile(z.object({
|
|
|
146
175
|
|
|
147
176
|
The compile(...) function returns Validator instances to Check, Parse and report Errors for JavaScript values.
|
|
148
177
|
|
|
149
|
-
|
|
178
|
+
## Check
|
|
150
179
|
|
|
151
180
|
The check(...) returns a boolean result.
|
|
152
181
|
|
|
153
182
|
```typescript
|
|
154
|
-
//
|
|
183
|
+
// Vector3.check(value: unknown): value is Vector3
|
|
155
184
|
|
|
156
|
-
if(
|
|
185
|
+
if(Vector3.check(value)) {
|
|
157
186
|
|
|
158
187
|
const { x, y, z } = value // safe
|
|
159
188
|
}
|
|
160
189
|
```
|
|
161
190
|
|
|
162
|
-
|
|
191
|
+
## Parse
|
|
163
192
|
|
|
164
193
|
The parse(...) function returns if valid, otherwise throws.
|
|
165
194
|
|
|
166
195
|
```typescript
|
|
167
|
-
//
|
|
196
|
+
// Vector3.parse(value: unknown): Vector3
|
|
168
197
|
|
|
169
|
-
const { x, y, z } =
|
|
198
|
+
const { x, y, z } = Vector3.parse(value)
|
|
170
199
|
```
|
|
171
200
|
|
|
172
|
-
|
|
201
|
+
## Errors
|
|
173
202
|
|
|
174
203
|
The errors(...) function returns diagnostics (use only after failed check)
|
|
175
204
|
|
|
176
205
|
```typescript
|
|
177
|
-
//
|
|
206
|
+
// Vector3.errors(value: unknown): object[]
|
|
178
207
|
|
|
179
|
-
const errors =
|
|
208
|
+
const errors = Vector3.errors(value)
|
|
180
209
|
```
|
|
181
210
|
|
|
182
211
|
## Static
|
|
183
212
|
|
|
184
|
-
|
|
213
|
+
TypeDriver provides type infernece for JSON Schema, Standard Schema or TypeScript | [Example](https://www.typescriptlang.org/play/?target=99&module=7#code/JYWwDg9gTgLgBAbzjAnmApnAyjAhjYAYzgF84AzKCEOAclQwBMpgA3dKWgKC4Hpe4ASQB25DsjToAzhSo1cAGwXIAFsGEBzKQEIeDTAEE4AXmx4ChADwADJFzhwAHgC44wgK4gARhwA09uBRXD28-AIAvYM8fKC4SawA+PUk4ACETM3wiSwQA-VdaCC8AK3RCGFp-Byh0AEd3YBrGVwBtWkdKuhRO2nDaAF0quDAqDFhgaVdchwcXRAkMApCY2lIhhyD5-LpljlWSdbhIrckl6L21gJI4pK5QSFg4ACo4XBlw2Wo6cIhGbi5CBBhFJ4ABhDLhAB0RVK5QAFNMnK4obsoHCAJRDTYo85ozERZGQ1EY-wkdFcfRwcGmHBZKz6CDkKm3IA)
|
|
185
214
|
|
|
186
215
|
```typescript
|
|
187
216
|
import { type Static } from 'typedriver'
|
|
188
217
|
|
|
189
|
-
//
|
|
218
|
+
// TypeScript
|
|
190
219
|
|
|
191
220
|
type A = Static<`{
|
|
192
221
|
x: number,
|
|
@@ -194,6 +223,8 @@ type A = Static<`{
|
|
|
194
223
|
z: number
|
|
195
224
|
}`>
|
|
196
225
|
|
|
226
|
+
// JSON Schema
|
|
227
|
+
|
|
197
228
|
type B = Static<{
|
|
198
229
|
type: 'object',
|
|
199
230
|
required: ['x', 'y', 'z'],
|
|
@@ -204,6 +235,8 @@ type B = Static<{
|
|
|
204
235
|
}
|
|
205
236
|
}>
|
|
206
237
|
|
|
238
|
+
// Standard Schema
|
|
239
|
+
|
|
207
240
|
import * as z from 'zod'
|
|
208
241
|
|
|
209
242
|
const C = z.object({
|
|
@@ -218,19 +251,19 @@ type C = Static<typeof C>
|
|
|
218
251
|
|
|
219
252
|
## Reflect
|
|
220
253
|
|
|
221
|
-
Validators can reflect back a
|
|
254
|
+
Validators can reflect back a JSON Schema representation if the underlying type supports it. This is true for all TypeScript and JSON Schema source types. Reflect can be used for OpenAPI metadata publishing, or RPC systems that need to publish JSON based IDL (interface definition language) to remote callers. Validators provide two functions for this.
|
|
222
255
|
|
|
223
256
|
```typescript
|
|
224
257
|
import { compile, type Static } from 'typedriver'
|
|
225
258
|
|
|
226
259
|
const validator = compile(...)
|
|
227
260
|
|
|
228
|
-
validator.
|
|
229
|
-
//
|
|
230
|
-
// compiled with
|
|
261
|
+
validator.isJSONSchema() // Returns true if the validator can be converted to
|
|
262
|
+
// JSON Schema. This is true when the validator was
|
|
263
|
+
// compiled with JSON Schema or TypeScript, but false
|
|
231
264
|
// if it was compiled with Standard Schema.
|
|
232
265
|
|
|
233
|
-
validator.
|
|
266
|
+
validator.toJSONSchema() // Returns the JSON Schema for the validator. If the
|
|
234
267
|
// validator was compiled with Standard Schema, an
|
|
235
268
|
// empty {} is returned to indicate an unknown
|
|
236
269
|
// runtime schema.
|
|
@@ -241,6 +274,81 @@ The source type used for compilation can also be returned via
|
|
|
241
274
|
validator.schema() // will return the schematic used for compile.
|
|
242
275
|
```
|
|
243
276
|
|
|
277
|
+
## Locale
|
|
278
|
+
|
|
279
|
+
TypeDriver provides (i18n) error message translations for many languages and locales. Locales are defined as [IETF BCP 47](https://www.rfc-editor.org/rfc/bcp/bcp47.txt) localization codes. Additional localizations can be submitted to the TypeBox project.
|
|
280
|
+
|
|
281
|
+
```typescript
|
|
282
|
+
import { compile, locale } from 'typedriver'
|
|
283
|
+
|
|
284
|
+
// Supported Locales
|
|
285
|
+
|
|
286
|
+
type LocaleString = (
|
|
287
|
+
| "ar_001" | "bn_BD" | "cs_CZ" | "de_DE" | "el_GR" | "en_US" | "es_419"
|
|
288
|
+
| "es_AR" | "es_ES" | "es_MX" | "fa_IR" | "fil_PH" | "fr_CA" | "fr_FR"
|
|
289
|
+
| "ha_NG" | "hi_IN" | "hu_HU" | "id_ID" | "it_IT" | "ja_JP" | "ko_KR"
|
|
290
|
+
| "ms_MY" | "nl_NL" | "pl_PL" | "pt_BR" | "pt_PT" | "ro_RO" | "ru_RU"
|
|
291
|
+
| "sv_SE" | "sw_TZ" | "th_TH" | "tr_TR" | "uk_UA" | "ur_PK" | "vi_VN"
|
|
292
|
+
| "yo_NG" | "zh_Hans" | "zh_Hant"
|
|
293
|
+
)
|
|
294
|
+
|
|
295
|
+
locale('en_US') // Set: English | US (Default)
|
|
296
|
+
|
|
297
|
+
console.log(compile('string').errors(42)) // [{
|
|
298
|
+
// keyword: "type",
|
|
299
|
+
// schemaPath: "#",
|
|
300
|
+
// instancePath: "",
|
|
301
|
+
// params: { type: "string" },
|
|
302
|
+
// message: "must be string"
|
|
303
|
+
// }]
|
|
304
|
+
|
|
305
|
+
locale('ko_KR') // Set: Korean | South Korea
|
|
306
|
+
|
|
307
|
+
console.log(compile('string').errors(42)) // [{
|
|
308
|
+
// keyword: "type",
|
|
309
|
+
// schemaPath: "#",
|
|
310
|
+
// instancePath: "",
|
|
311
|
+
// params: { type: "string" },
|
|
312
|
+
// message: "string이어야 합니다"
|
|
313
|
+
// }]
|
|
314
|
+
```
|
|
315
|
+
Localization support is only available for JSON Schema.
|
|
316
|
+
|
|
317
|
+
## Accelerate
|
|
318
|
+
|
|
319
|
+
TypeDriver provides acceleration support for libraries that implement the Standard JSON Schema specification. This is a new specification that enables runtime type libraries to be integrated into TypeBox validation infrastructure. This project tracks upstream implementations of this specification and maintains a benchmark measuring compariative performance with and without compile(...).
|
|
320
|
+
|
|
321
|
+
```bash
|
|
322
|
+
$ deno task bench
|
|
323
|
+
```
|
|
324
|
+
Benchmark 16M Parse Operations of this Structure
|
|
325
|
+
```typescript
|
|
326
|
+
const Vector3 = compile(`{
|
|
327
|
+
x: number,
|
|
328
|
+
y: number,
|
|
329
|
+
z: number
|
|
330
|
+
}`)
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
Accelerated Indicates Support for Standard JSON Schema
|
|
334
|
+
|
|
335
|
+
```bash
|
|
336
|
+
┌────────────┬────────────┬─────────────┬──────────────┬──────────────┬─────────────────┐
|
|
337
|
+
│ (idx) │ iterations │ accelerated │ default(...) │ compile(...) │ result │
|
|
338
|
+
├────────────┼────────────┼─────────────┼──────────────┼──────────────┼─────────────────┤
|
|
339
|
+
│ typescript │ 16000000 │ true │ " ------" │ " 30 ms" │ " ------" │
|
|
340
|
+
│ jsonschema │ 16000000 │ true │ " ------" │ " 29 ms" │ " ------" │
|
|
341
|
+
│ arktype │ 16000000 │ true │ " 537 ms" │ " 30 ms" │ "94.41% faster" │
|
|
342
|
+
│ arri │ 16000000 │ false │ " 3086 ms" │ " 3049 ms" │ "1.20% faster" │
|
|
343
|
+
│ effect │ 16000000 │ false │ "24183 ms" │ "23886 ms" │ "1.23% faster" │
|
|
344
|
+
│ sury │ 16000000 │ false │ " 153 ms" │ " 166 ms" │ "8.33% slower" │
|
|
345
|
+
│ valibot │ 16000000 │ false │ " 3632 ms" │ " 3515 ms" │ "3.21% faster" │
|
|
346
|
+
│ zod │ 16000000 │ false │ " 575 ms" │ " 603 ms" │ "4.93% slower" │
|
|
347
|
+
└────────────┴────────────┴─────────────┴──────────────┴──────────────┴─────────────────┘
|
|
348
|
+
|
|
349
|
+
Last Run: Thu Dec 04 2025
|
|
350
|
+
```
|
|
351
|
+
|
|
244
352
|
## Contribute
|
|
245
353
|
|
|
246
354
|
TypeDriver is open to community contribution. Please ensure you submit an issue before submitting a pull request. This project prefers open community discussion before accepting new features.
|