@@ -31,7 +31,7 @@ yarn add @coderspirit/nominal-typebox
31
31
32
32
## Usage instructions
33
33
34
- ### Typebox ' Type.String -> brandedString
34
+ ### TypeBox ' Type.String -> brandedString
35
35
36
36
``` typescript
37
37
import type { FastBrand } from ' @coderspirit/nominal'
@@ -61,7 +61,42 @@ const username: Username = requestObject.username // OK
61
61
const corruptedUserame: Username = ' untagged string' // type error
62
62
```
63
63
64
- ### Typebox' Type.Number -> brandedNumber
64
+ ### TypeBox' Type.RegExp -> brandedRegExp
65
+
66
+ ``` typescript
67
+
68
+ import type { FastBrand } from ' @coderspirit/nominal'
69
+ import { brandedRegExp } from ' @coderspirit/nominal-typebox'
70
+
71
+ import { Object as TBObject } from ' @sinclair/typebox'
72
+ import { TypeCompiler } from ' @sinclair/typebox/compiler'
73
+
74
+ type UserId = FastBrand <string , ' UserId' >
75
+
76
+ // Use `brandedString` instead of Typebox' `Type.String`
77
+ const requestSchema = TBObject ({
78
+ // We can pass the same options Type.String has
79
+ userId: brandedRegExp <' UserId' >(
80
+ / ^ [0-9a-f ] {8} -[0-9a-f ] {4} -[0-9a-f ] {4} -[0-9a-f ] {4} -[0-9a-f ] {12} $ /
81
+ )
82
+ })
83
+ const requestValidator = TypeCompiler .Compile (requestSchema )
84
+
85
+ const requestObject = getRequestFromSomewhere () // unknown
86
+ if (! requestValidator .Check (requestObject )) {
87
+ throw new Error (' Invalid request!' )
88
+ }
89
+
90
+ // At this point, the type checker knows that requestObject.username is
91
+ // "branded" as 'Username'
92
+
93
+ const userId: UserId = requestObject .userId // OK
94
+ const corruptedUserId: UserId = ' untagged (and probably wrong) id' // type error
95
+ ```
96
+
97
+ ---
98
+
99
+ ### TypeBox' Type.Number -> brandedNumber
65
100
66
101
67
102
``` typescript
@@ -93,12 +128,14 @@ const corruptedLat: Latitude = 10 // type error
93
128
const corruptedLon: Longitude = 10 // type error
94
129
```
95
130
96
- ### Typebox ' Type.Integer -> brandedInteger
131
+ ### TypeBox ' Type.Integer -> brandedInteger
97
132
98
133
The same applies as for the two previous examples, you can use ` brandedInteger `
99
134
instead of Typebox' ` Type.Integer ` .
100
135
101
- ### Typebox' Type.Array -> brandedArray
136
+ ---
137
+
138
+ ### TypeBox' Type.Array -> brandedArray
102
139
103
140
` brandedArray ` has the same signature as Typebox' ` Type.Array ` , except that we
104
141
have to pass a "brand" string argument as its first parameter:
@@ -115,7 +152,7 @@ const arraySchema = brandedArray(
115
152
)
116
153
```
117
154
118
- ### Typebox ' Type.Object -> brandedObject
155
+ ### TypeBox ' Type.Object -> brandedObject
119
156
120
157
` brandedObject ` has the same signature as Typebox' ` Type.Object ` , except that we
121
158
have to pass a "brand" string argument as its first parameter:
@@ -134,7 +171,7 @@ const objectSchema = brandedObject(
134
171
)
135
172
```
136
173
137
- ### Typebox ' Type.Union -> brandedUnion
174
+ ### TypeBox ' Type.Union -> brandedUnion
138
175
139
176
` brandedUnion ` has the same signature as Typebox' ` Type.Union ` , except that we
140
177
have to pass a "brand" string argument as its first parameter:
@@ -149,6 +186,8 @@ const unionSchema = brandedUnion(
149
186
)
150
187
```
151
188
189
+ ---
190
+
152
191
### Fallback alternative
153
192
154
193
In case this library does not provide a specific schema factory for your type,
0 commit comments