typedriver 0.8.1 → 0.8.2
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/locale.d.mts +6 -0
- package/build/locale.mjs +9 -0
- package/package.json +1 -1
- package/readme.md +81 -30
|
@@ -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
|
@@ -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,9 @@
|
|
|
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
|
|
6
|
+
? System.Locale[locale]
|
|
7
|
+
: System.Locale.en_US;
|
|
8
|
+
System.Locale.Set(F);
|
|
9
|
+
}
|
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,27 +23,38 @@ $ npm install typedriver
|
|
|
23
23
|
|
|
24
24
|
## Example
|
|
25
25
|
|
|
26
|
+
Runtime Compile System 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) | [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) | [TypeBox](https://www.typescriptlang.org/play/?target=99&module=7#code/PTAEFpK6dv4YpAoEoBSBnA9gO1AMoDGAFgKYC2AhgFygAqAngA5kBC2AHqmEn-wMHhkASwrNsAJwAuoAN6gi2cSIA2ZUAF9QAM0nLQAcmksyAE0kiAbmUmHR4qbKatd+ikZOsARl0OgA5GQlXExZADUyImkpAGZQAF5FZWY1MgAKFzIAOgB5bwArKOl0uWQAzjos7IA5AFcKb1t0gEoAGnLQRirTWoamyVaOgIAvHtY+xuaW5E0WmeQzKNUqSQ0QsNArKlU6sjo63ABrXGwAd1wgjdkJTBFpETxE0EjouOzmVcwM7d2yFoCgKBgLQ11At3uj1wdDKwLh8IRiKRQLQFTouH6tmGyJxuJxqK66MxkmxeLJ5ICBLGoAxU0knQpjPxYG0SWy7KAA)
|
|
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 validation middleware that enables frameworks to incorporate Json Schema and Standard Schema based libraries on framework interfaces (for example HTTP route handlers, RPC endpoints, etc). It offers a unified validation and type inference system to make integrating runtime types simple, and provides a default TypeScript DSL inference system as standard.
|
|
52
|
+
|
|
53
|
+
> TypeDriver is designed to normalize heterogeneous runtime schema systems into a single, framework-consumable contract that preserves static type inference, runtime validation, and schema reflection without forcing a commitment to a single schema ecosystem.
|
|
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.
|
|
56
|
+
|
|
45
57
|
|
|
46
|
-
TypeDriver supports Json Schema drafts 3 through 2020-12. It provides advanced type inference support for both Json Schema and TypeScript DSL definitions as standard. It uses TypeBox as a foundation for high performance runtime validation, and Standard Schema for cross-library compatibility.
|
|
47
58
|
|
|
48
59
|
License MIT
|
|
49
60
|
|
|
@@ -52,16 +63,17 @@ License MIT
|
|
|
52
63
|
- [Overview](#Overview)
|
|
53
64
|
- [Framework](#Framework)
|
|
54
65
|
- [Compile](#Compile)
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
66
|
+
- [Check](#Check)
|
|
67
|
+
- [Parse](#Parse)
|
|
68
|
+
- [Errors](#Errors)
|
|
58
69
|
- [Static](#Static)
|
|
59
70
|
- [Reflect](#Schema)
|
|
71
|
+
- [Locale](#Locale)
|
|
60
72
|
- [Contribute](#Contribute)
|
|
61
73
|
|
|
62
74
|
## Framework
|
|
63
75
|
|
|
64
|
-
TypeDriver is designed
|
|
76
|
+
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
77
|
|
|
66
78
|
[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
79
|
|
|
@@ -77,7 +89,7 @@ post('/', {
|
|
|
77
89
|
})
|
|
78
90
|
```
|
|
79
91
|
|
|
80
|
-
|
|
92
|
+
Where the above design is achieved with the following ...
|
|
81
93
|
|
|
82
94
|
```typescript
|
|
83
95
|
import { type Static, compile } from 'typedriver'
|
|
@@ -96,7 +108,6 @@ export function post<Path extends string, const Options extends RouteOptions,
|
|
|
96
108
|
}
|
|
97
109
|
```
|
|
98
110
|
|
|
99
|
-
|
|
100
111
|
## Compile
|
|
101
112
|
|
|
102
113
|
TypeDriver consists of a singular compile(...) function that accepts either a TypeScript, Json Schema and Standard Schema definition and returns an optimized Validator.
|
|
@@ -105,20 +116,20 @@ TypeDriver consists of a singular compile(...) function that accepts either a Ty
|
|
|
105
116
|
import { compile } from 'typedriver'
|
|
106
117
|
```
|
|
107
118
|
|
|
108
|
-
Pass TypeScript definitions via strings
|
|
119
|
+
Pass TypeScript definitions via strings [Example](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYwuYAbApnAvnAMyjTgHIYBPMLAEymADcspSAoV1AOwGd4A1LMhjQAzHAC8KNGExYAFAAMErOHAAeALjicAriABGzFXApbdBo6oBeZvYaitcCgJSq37j56-ef7gPR+Ujz8gsJQIlp8AIYYwDRRYQA8AHSpADSIxr7ZObluAW6a2nbMaVl5FZX5gaqmxRZQZVXNlQXWtg1NLd3ZBbgAfOxcvHCQ3MAwwBCcEnACQqLJYFFQ3PJIxkUADF11AIxdNnBbjs5AA)
|
|
109
120
|
|
|
110
121
|
```typescript
|
|
111
|
-
const
|
|
122
|
+
const Vector3 = compile(`{
|
|
112
123
|
x: number
|
|
113
124
|
y: number
|
|
114
125
|
z: number
|
|
115
126
|
}`)
|
|
116
127
|
```
|
|
117
128
|
|
|
118
|
-
... or Json Schema
|
|
129
|
+
... or Json Schema [Example](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYwuYAbApnAvnAMyjTgHIYBPMLAEymADcspSAoV1AOwGd4A1LMhjQAzHAC8KNGExYAFEjhLlK1WtUB6DVJ79BwqCIBccPgEMMwGmYMAeAHSOANIlZLK1E6QgAjAFb6pC7qIaFhWsoAHiacAK4gPsxObnBQWACOscBpNCYA2qSRQWQUxaQAXqQAusFhdXARShQx8YlQyUpgxNSwwFjcJor1w+HaSuUtCcwpStGIcB5YXnFTLHi1I6ERuAB8M3DN84vLrcyk6-sTR1RLZCtt57gpT7gAlOxcvHCQ3MAwwBBOBJTPpRPYwGYoNx5EgUnMAAwdA4mACMSKu8NYbyAA)
|
|
119
130
|
|
|
120
131
|
```typescript
|
|
121
|
-
const
|
|
132
|
+
const Vector3 = compile({
|
|
122
133
|
type: 'object',
|
|
123
134
|
required: ['x', 'y', 'z'],
|
|
124
135
|
properties: {
|
|
@@ -129,12 +140,12 @@ const Vec3 = compile({
|
|
|
129
140
|
})
|
|
130
141
|
```
|
|
131
142
|
|
|
132
|
-
... or libraries that implement Standard Schema
|
|
143
|
+
... or libraries that implement Standard Schema [Example](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYwuYAbApnAvnAMyjTgHIYBPMLAEymADcspSAoV0SWOAKjgEMAznABehYiDIiINNq1QA7QfABqWZDGgBmOAF4UaMJiwAKEQDoIAIwBW6mCaRxnL12+cB6DwaWr72gC44FX4MYBp+TSgAHnM4gBpEVmcADyCLBQBXECtmEwBKRPdiktK3Lxc0uCyc5njkuAp08xrcqAKisq7uuArnJurstvrnEWbWvMKe6dK+0SCJqFZcfPyZ9Y3e71wAPnZFZThIQWAYYAgFPWD-KC1zMH4oQVMkBqqABhHGoIBGL7G4O9lvkgA)
|
|
133
144
|
|
|
134
145
|
```typescript
|
|
135
146
|
import * as z from 'zod'
|
|
136
147
|
|
|
137
|
-
const
|
|
148
|
+
const Vector3 = compile(z.object({
|
|
138
149
|
x: z.number(),
|
|
139
150
|
y: z.number(),
|
|
140
151
|
z: z.number(),
|
|
@@ -146,37 +157,37 @@ const Vec3 = compile(z.object({
|
|
|
146
157
|
|
|
147
158
|
The compile(...) function returns Validator instances to Check, Parse and report Errors for JavaScript values.
|
|
148
159
|
|
|
149
|
-
|
|
160
|
+
## Check
|
|
150
161
|
|
|
151
162
|
The check(...) returns a boolean result.
|
|
152
163
|
|
|
153
164
|
```typescript
|
|
154
|
-
//
|
|
165
|
+
// Vector3.check(value: unknown): value is Vector3
|
|
155
166
|
|
|
156
|
-
if(
|
|
167
|
+
if(Vector3.check(value)) {
|
|
157
168
|
|
|
158
169
|
const { x, y, z } = value // safe
|
|
159
170
|
}
|
|
160
171
|
```
|
|
161
172
|
|
|
162
|
-
|
|
173
|
+
## Parse
|
|
163
174
|
|
|
164
175
|
The parse(...) function returns if valid, otherwise throws.
|
|
165
176
|
|
|
166
177
|
```typescript
|
|
167
|
-
//
|
|
178
|
+
// Vector3.parse(value: unknown): Vector3
|
|
168
179
|
|
|
169
|
-
const { x, y, z } =
|
|
180
|
+
const { x, y, z } = Vector3.parse(value)
|
|
170
181
|
```
|
|
171
182
|
|
|
172
|
-
|
|
183
|
+
## Errors
|
|
173
184
|
|
|
174
185
|
The errors(...) function returns diagnostics (use only after failed check)
|
|
175
186
|
|
|
176
187
|
```typescript
|
|
177
|
-
//
|
|
188
|
+
// Vector3.errors(value: unknown): object[]
|
|
178
189
|
|
|
179
|
-
const errors =
|
|
190
|
+
const errors = Vector3.errors(value)
|
|
180
191
|
```
|
|
181
192
|
|
|
182
193
|
## Static
|
|
@@ -241,6 +252,46 @@ The source type used for compilation can also be returned via
|
|
|
241
252
|
validator.schema() // will return the schematic used for compile.
|
|
242
253
|
```
|
|
243
254
|
|
|
255
|
+
## Locale
|
|
256
|
+
|
|
257
|
+
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.
|
|
258
|
+
|
|
259
|
+
```typescript
|
|
260
|
+
import { compile, locale } from 'typedriver'
|
|
261
|
+
|
|
262
|
+
// Supported Locales
|
|
263
|
+
|
|
264
|
+
type LocaleString =
|
|
265
|
+
| "ar_001" | "bn_BD" | "cs_CZ" | "de_DE" | "el_GR" | "en_US" | "es_419"
|
|
266
|
+
| "es_AR" | "es_ES" | "es_MX" | "fa_IR" | "fil_PH" | "fr_CA" | "fr_FR"
|
|
267
|
+
| "ha_NG" | "hi_IN" | "hu_HU" | "id_ID" | "it_IT" | "ja_JP" | "ko_KR"
|
|
268
|
+
| "ms_MY" | "nl_NL" | "pl_PL" | "pt_BR" | "pt_PT" | "ro_RO" | "ru_RU"
|
|
269
|
+
| "sv_SE" | "sw_TZ" | "th_TH" | "tr_TR" | "uk_UA" | "ur_PK" | "vi_VN"
|
|
270
|
+
| "yo_NG" | "zh_Hans" | "zh_Hant"
|
|
271
|
+
|
|
272
|
+
locale('en_US') // Set: English | US (Default)
|
|
273
|
+
|
|
274
|
+
console.log(compile('string').errors(42)) // [{
|
|
275
|
+
// keyword: "type",
|
|
276
|
+
// schemaPath: "#",
|
|
277
|
+
// instancePath: "",
|
|
278
|
+
// params: { type: "string" },
|
|
279
|
+
// message: "must be string"
|
|
280
|
+
// }]
|
|
281
|
+
|
|
282
|
+
locale('ko_KR') // Set: Korean | South Korea
|
|
283
|
+
|
|
284
|
+
console.log(compile('string').errors(42)) // [{
|
|
285
|
+
// keyword: "type",
|
|
286
|
+
// schemaPath: "#",
|
|
287
|
+
// instancePath: "",
|
|
288
|
+
// params: { type: "string" },
|
|
289
|
+
// message: "string이어야 합니다"
|
|
290
|
+
// }]
|
|
291
|
+
```
|
|
292
|
+
Localization support is only available for Json Schema and supporting libraries.
|
|
293
|
+
|
|
294
|
+
|
|
244
295
|
## Contribute
|
|
245
296
|
|
|
246
297
|
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.
|