@sinclair/typebox 0.25.13 → 0.25.14
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/package.json +1 -1
- package/readme.md +17 -17
- package/value/create.js +24 -4
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -1143,27 +1143,28 @@ The custom module is an optional import.
|
|
|
1143
1143
|
import { Custom } from '@sinclair/typebox/custom'
|
|
1144
1144
|
```
|
|
1145
1145
|
|
|
1146
|
-
The following creates a `
|
|
1146
|
+
The following creates a `bigint` type.
|
|
1147
1147
|
|
|
1148
1148
|
```typescript
|
|
1149
1149
|
import { Type, Kind } from '@sinclair/typebox'
|
|
1150
1150
|
|
|
1151
|
-
Custom.Set('
|
|
1151
|
+
Custom.Set('bigint', (schema, value) => typeof value === 'bigint')
|
|
1152
|
+
// ▲
|
|
1152
1153
|
// │
|
|
1153
|
-
// └────────────────────────────┐
|
|
1154
|
+
// └────────────────────────────┐
|
|
1154
1155
|
// │
|
|
1155
|
-
const T = Type.Unsafe<bigint>({ [Kind]: '
|
|
1156
|
+
const T = Type.Unsafe<bigint>({ [Kind]: 'bigint' }) // const T = { [Kind]: 'BigInt' }
|
|
1156
1157
|
|
|
1157
|
-
const A = TypeCompiler.Compile(T).Check(
|
|
1158
|
+
const A = TypeCompiler.Compile(T).Check(1n) // const A = true
|
|
1158
1159
|
|
|
1159
|
-
const B = Value.Check(T,
|
|
1160
|
+
const B = Value.Check(T, 1) // const B = false
|
|
1160
1161
|
```
|
|
1161
1162
|
|
|
1162
1163
|
<a name='typecheck-custom-formats'></a>
|
|
1163
1164
|
|
|
1164
1165
|
### Custom Formats
|
|
1165
1166
|
|
|
1166
|
-
Use the format module to create user defined string formats. If using Ajv, please refer to the official Ajv format documentation located [here](https://ajv.js.org/guide/formats.html).
|
|
1167
|
+
Use the format module to create user defined string formats. The format module is specific to TypeBox can only be used with the TypeCompiler and Value modules. If using Ajv, please refer to the official Ajv format documentation located [here](https://ajv.js.org/guide/formats.html).
|
|
1167
1168
|
|
|
1168
1169
|
The format module is an optional import.
|
|
1169
1170
|
|
|
@@ -1171,20 +1172,19 @@ The format module is an optional import.
|
|
|
1171
1172
|
import { Format } from '@sinclair/typebox/format'
|
|
1172
1173
|
```
|
|
1173
1174
|
|
|
1174
|
-
The following creates a
|
|
1175
|
+
The following creates a custom string format that checks for lowercase.
|
|
1175
1176
|
|
|
1176
1177
|
```typescript
|
|
1177
|
-
Format.Set('
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
const T = Type.String({ format: 'palindrome' })
|
|
1178
|
+
Format.Set('lowercase', value => value === value.toLowerCase())
|
|
1179
|
+
// ▲
|
|
1180
|
+
// │
|
|
1181
|
+
// └────────────────────┐
|
|
1182
|
+
// │
|
|
1183
|
+
const T = Type.String({ format: 'lowercase' })
|
|
1184
1184
|
|
|
1185
|
-
const A = TypeCompiler.Compile(T).Check('
|
|
1185
|
+
const A = TypeCompiler.Compile(T).Check('action') // const A = true
|
|
1186
1186
|
|
|
1187
|
-
const B = Value.Check(T, '
|
|
1187
|
+
const B = Value.Check(T, 'Action') // const B = false
|
|
1188
1188
|
```
|
|
1189
1189
|
|
|
1190
1190
|
<a name='benchmark'></a>
|
package/value/create.js
CHANGED
|
@@ -131,13 +131,23 @@ var ValueCreate;
|
|
|
131
131
|
}
|
|
132
132
|
}
|
|
133
133
|
function Literal(schema, references) {
|
|
134
|
-
|
|
134
|
+
if (schema.default !== undefined) {
|
|
135
|
+
return schema.default;
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
return schema.const;
|
|
139
|
+
}
|
|
135
140
|
}
|
|
136
141
|
function Never(schema, references) {
|
|
137
142
|
throw new ValueCreateNeverTypeError(schema);
|
|
138
143
|
}
|
|
139
144
|
function Null(schema, references) {
|
|
140
|
-
|
|
145
|
+
if (schema.default !== undefined) {
|
|
146
|
+
return schema.default;
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
141
151
|
}
|
|
142
152
|
function Number(schema, references) {
|
|
143
153
|
if (schema.default !== undefined) {
|
|
@@ -258,7 +268,12 @@ var ValueCreate;
|
|
|
258
268
|
}
|
|
259
269
|
}
|
|
260
270
|
function Undefined(schema, references) {
|
|
261
|
-
|
|
271
|
+
if (schema.default !== undefined) {
|
|
272
|
+
return schema.default;
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
return undefined;
|
|
276
|
+
}
|
|
262
277
|
}
|
|
263
278
|
function Union(schema, references) {
|
|
264
279
|
if (schema.default !== undefined) {
|
|
@@ -291,7 +306,12 @@ var ValueCreate;
|
|
|
291
306
|
}
|
|
292
307
|
}
|
|
293
308
|
function Void(schema, references) {
|
|
294
|
-
|
|
309
|
+
if (schema.default !== undefined) {
|
|
310
|
+
return schema.default;
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
return null;
|
|
314
|
+
}
|
|
295
315
|
}
|
|
296
316
|
function UserDefined(schema, references) {
|
|
297
317
|
if (schema.default !== undefined) {
|