@sinclair/typebox 0.28.9 → 0.28.11
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 +2 -2
- package/package.json +1 -1
- package/readme.md +30 -11
- package/typebox.js +8 -0
- package/value/check.js +2 -2
package/compiler/compiler.js
CHANGED
|
@@ -257,7 +257,7 @@ var TypeCompiler;
|
|
|
257
257
|
}
|
|
258
258
|
else if (Types.TypeGuard.TSchema(schema.unevaluatedProperties)) {
|
|
259
259
|
const keyCheck = PushLocal(`${new RegExp(Types.KeyResolver.ResolvePattern(schema))};`);
|
|
260
|
-
const check2 = `Object.getOwnPropertyNames(${value}).every(key => ${keyCheck}.test(key) || ${CreateExpression(schema.unevaluatedProperties, references,
|
|
260
|
+
const check2 = `Object.getOwnPropertyNames(${value}).every(key => ${keyCheck}.test(key) || ${CreateExpression(schema.unevaluatedProperties, references, `${value}[key]`)})`;
|
|
261
261
|
yield `${check1} && ${check2}`;
|
|
262
262
|
}
|
|
263
263
|
else {
|
|
@@ -326,7 +326,7 @@ var TypeCompiler;
|
|
|
326
326
|
}
|
|
327
327
|
}
|
|
328
328
|
if (typeof schema.additionalProperties === 'object') {
|
|
329
|
-
const expression = CreateExpression(schema.additionalProperties, references,
|
|
329
|
+
const expression = CreateExpression(schema.additionalProperties, references, `${value}[key]`);
|
|
330
330
|
const keys = `[${knownKeys.map((key) => `'${key}'`).join(', ')}]`;
|
|
331
331
|
yield `(Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key) || ${expression}))`;
|
|
332
332
|
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -11,9 +11,8 @@
|
|
|
11
11
|
|
|
12
12
|
[](https://badge.fury.io/js/%40sinclair%2Ftypebox)
|
|
13
13
|
[](https://www.npmjs.com/package/%40sinclair%2Ftypebox)
|
|
14
|
-
[](https://packagephobia.com/result?p=@sinclair/typebox)
|
|
15
|
-
[](https://opensource.org/licenses/MIT)
|
|
16
14
|
[](https://github.com/sinclairzx81/typebox/actions)
|
|
15
|
+
[](https://opensource.org/licenses/MIT)
|
|
17
16
|
|
|
18
17
|
</div>
|
|
19
18
|
|
|
@@ -109,6 +108,7 @@ License MIT
|
|
|
109
108
|
- [Types](#typesystem-types)
|
|
110
109
|
- [Formats](#typesystem-formats)
|
|
111
110
|
- [Policies](#typesystem-policies)
|
|
111
|
+
- [Workbench](#workbench)
|
|
112
112
|
- [Benchmark](#benchmark)
|
|
113
113
|
- [Compile](#benchmark-compile)
|
|
114
114
|
- [Validate](#benchmark-validate)
|
|
@@ -1346,26 +1346,31 @@ import { TypeSystem } from '@sinclair/typebox/system'
|
|
|
1346
1346
|
|
|
1347
1347
|
### Types
|
|
1348
1348
|
|
|
1349
|
-
Use the `Type(...)` function to create
|
|
1349
|
+
Use the `Type(...)` function to create custom types. This function lets you specify custom value assertion logic and will return a type factory function which is used to instance the type. This function accepts two generic arguments, the first is the inference type, the second is options used to constrain the type. The following creates a Vector type.
|
|
1350
1350
|
|
|
1351
1351
|
```typescript
|
|
1352
|
-
type
|
|
1352
|
+
type VectorOptions = { abs: boolean }
|
|
1353
1353
|
|
|
1354
|
-
type
|
|
1354
|
+
type Vector = { x: number, y: number }
|
|
1355
1355
|
|
|
1356
|
-
const
|
|
1356
|
+
const Vector = TypeSystem.Type<Vector, VectorOptions>('Vector', (options, value) => {
|
|
1357
1357
|
return (
|
|
1358
1358
|
typeof value === 'object' && value !== null &&
|
|
1359
|
-
typeof value.x === 'number' &&
|
|
1360
|
-
typeof value.y === 'number'
|
|
1359
|
+
'x' in value && typeof value.x === 'number' &&
|
|
1360
|
+
'y' in value && typeof value.y === 'number' &&
|
|
1361
|
+
(options.abs ? (value.x === Math.abs(value.x) && value.y === Math.abs(value.y)) : true)
|
|
1361
1362
|
)
|
|
1362
1363
|
})
|
|
1363
1364
|
|
|
1364
|
-
const T =
|
|
1365
|
+
const T = Vector({ abs: true })
|
|
1366
|
+
|
|
1367
|
+
type T = Static<typeof T> // type T = Vector
|
|
1368
|
+
|
|
1369
|
+
const R1 = Value.Check(T, { x: 1, y: 1 }) // const R1 = true
|
|
1365
1370
|
|
|
1366
|
-
|
|
1371
|
+
const R2 = Value.Check(T, { x: 1, y: '1' }) // const R2 = false
|
|
1367
1372
|
|
|
1368
|
-
const
|
|
1373
|
+
const R3 = Value.Check(T, { x: 1, y: -1 }) // const R3 = false
|
|
1369
1374
|
```
|
|
1370
1375
|
|
|
1371
1376
|
<a name='typesystem-formats'></a>
|
|
@@ -1410,6 +1415,20 @@ TypeSystem.AllowArrayObjects = true
|
|
|
1410
1415
|
TypeSystem.AllowNaN = true
|
|
1411
1416
|
```
|
|
1412
1417
|
|
|
1418
|
+
<a name='workbench'></a>
|
|
1419
|
+
|
|
1420
|
+
## Workbench
|
|
1421
|
+
|
|
1422
|
+
TypeBox offers a web based code generation tool that can be used to create TypeBox types from TypeScript type definitions. This tool is written to prototype new TypeBox features, but can be used to rapidly convert TypeScript type definitions in to TypeBox types as well as raw JSON Schema.
|
|
1423
|
+
|
|
1424
|
+
[Workbench Link Here](https://sinclairzx81.github.io/typebox-workbench/)
|
|
1425
|
+
|
|
1426
|
+
<div align='center'>
|
|
1427
|
+
|
|
1428
|
+
<a href="https://sinclairzx81.github.io/typebox-workbench/"><img src="https://github.com/sinclairzx81/typebox/blob/master/workbench.png?raw=true" /></a>
|
|
1429
|
+
|
|
1430
|
+
</div>
|
|
1431
|
+
|
|
1413
1432
|
<a name='benchmark'></a>
|
|
1414
1433
|
|
|
1415
1434
|
## Benchmark
|
package/typebox.js
CHANGED
|
@@ -621,6 +621,14 @@ var ExtendsUndefined;
|
|
|
621
621
|
function Check(schema) {
|
|
622
622
|
if (schema[exports.Kind] === 'Undefined')
|
|
623
623
|
return true;
|
|
624
|
+
if (schema[exports.Kind] === 'Not') {
|
|
625
|
+
const not = schema;
|
|
626
|
+
return Check(not.allOf[1]);
|
|
627
|
+
}
|
|
628
|
+
if (schema[exports.Kind] === 'Intersect') {
|
|
629
|
+
const intersect = schema;
|
|
630
|
+
return intersect.allOf.every((schema) => Check(schema));
|
|
631
|
+
}
|
|
624
632
|
if (schema[exports.Kind] === 'Union') {
|
|
625
633
|
const union = schema;
|
|
626
634
|
return union.anyOf.some((schema) => Check(schema));
|
package/value/check.js
CHANGED
|
@@ -254,8 +254,8 @@ var ValueCheck;
|
|
|
254
254
|
if (!Visit(property, references, value[knownKey])) {
|
|
255
255
|
return false;
|
|
256
256
|
}
|
|
257
|
-
if (Types.ExtendsUndefined.Check(property)) {
|
|
258
|
-
return
|
|
257
|
+
if (Types.ExtendsUndefined.Check(property) && !(knownKey in value)) {
|
|
258
|
+
return false;
|
|
259
259
|
}
|
|
260
260
|
}
|
|
261
261
|
else {
|