@sinclair/typebox 0.28.15 → 0.28.16
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 +14 -10
- package/package.json +1 -1
- package/readme.md +6 -6
package/compiler/compiler.js
CHANGED
|
@@ -199,14 +199,15 @@ var TypeCompiler;
|
|
|
199
199
|
yield 'true';
|
|
200
200
|
}
|
|
201
201
|
function* Array(schema, references, value) {
|
|
202
|
-
|
|
202
|
+
yield `Array.isArray(${value})`;
|
|
203
203
|
if (IsNumber(schema.minItems))
|
|
204
204
|
yield `${value}.length >= ${schema.minItems}`;
|
|
205
205
|
if (IsNumber(schema.maxItems))
|
|
206
206
|
yield `${value}.length <= ${schema.maxItems}`;
|
|
207
207
|
if (schema.uniqueItems === true)
|
|
208
208
|
yield `((function() { const set = new Set(); for(const element of ${value}) { const hashed = hash(element); if(set.has(hashed)) { return false } else { set.add(hashed) } } return true })())`;
|
|
209
|
-
|
|
209
|
+
const expression = CreateExpression(schema.items, references, 'value');
|
|
210
|
+
yield `${value}.every(value => ${expression})`;
|
|
210
211
|
}
|
|
211
212
|
function* BigInt(schema, references, value) {
|
|
212
213
|
yield `(typeof ${value} === 'bigint')`;
|
|
@@ -358,9 +359,9 @@ var TypeCompiler;
|
|
|
358
359
|
if (index === -1)
|
|
359
360
|
throw new TypeCompilerDereferenceError(schema);
|
|
360
361
|
const target = references[index];
|
|
361
|
-
// Reference: If we have seen this reference before we can just yield and
|
|
362
|
-
// the function call. If this isn't the case we defer to visit to
|
|
363
|
-
// set the function for subsequent passes.
|
|
362
|
+
// Reference: If we have seen this reference before we can just yield and
|
|
363
|
+
// return the function call. If this isn't the case we defer to visit to
|
|
364
|
+
// generate and set the function for subsequent passes.
|
|
364
365
|
if (state_local_function_names.has(schema.$ref))
|
|
365
366
|
return yield `${CreateFunctionName(schema.$ref)}(${value})`;
|
|
366
367
|
yield* Visit(target, references, value);
|
|
@@ -392,7 +393,7 @@ var TypeCompiler;
|
|
|
392
393
|
yield `${func}(${value})`;
|
|
393
394
|
}
|
|
394
395
|
function* Tuple(schema, references, value) {
|
|
395
|
-
yield `
|
|
396
|
+
yield `Array.isArray(${value})`;
|
|
396
397
|
if (schema.items === undefined)
|
|
397
398
|
return yield `${value}.length === 0`;
|
|
398
399
|
yield `(${value}.length === ${schema.maxItems})`;
|
|
@@ -429,10 +430,13 @@ var TypeCompiler;
|
|
|
429
430
|
function* Visit(schema, references, value, root = false) {
|
|
430
431
|
const references_ = IsString(schema.$id) ? [...references, schema] : references;
|
|
431
432
|
const schema_ = schema;
|
|
432
|
-
// Rule: Types with identifiers are hoisted into their own functions.
|
|
433
|
-
//
|
|
434
|
-
//
|
|
435
|
-
//
|
|
433
|
+
// Rule: Types with identifiers are hoisted into their own functions.
|
|
434
|
+
// The following will generate a function for the schema and yield the
|
|
435
|
+
// call to that function. This call is only made if NOT the root type
|
|
436
|
+
// which allows the generated function to yield its expression. The
|
|
437
|
+
// root argument is only true when making calls via CreateFunction().
|
|
438
|
+
// Note there is potential to omit the root argument and conditional
|
|
439
|
+
// by refactoring the logic below. Consider for review.
|
|
436
440
|
if (IsString(schema.$id)) {
|
|
437
441
|
const name = CreateFunctionName(schema.$id);
|
|
438
442
|
if (!state_local_function_names.has(schema.$id)) {
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -483,7 +483,7 @@ The following table lists the Standard TypeBox types. These types are fully comp
|
|
|
483
483
|
│ ]) │ │ ], │
|
|
484
484
|
│ const T = Type.Tuple([ │ │ additionalItems: false, │
|
|
485
485
|
| ...Type.Rest(A), │ │ minItems: 4, │
|
|
486
|
-
| ...Type.
|
|
486
|
+
| ...Type.Rest(B) │ │ maxItems: 4 │
|
|
487
487
|
│ ]) │ │ } │
|
|
488
488
|
│ │ │ │
|
|
489
489
|
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
@@ -870,7 +870,7 @@ const T = Type.Object({ // const T = {
|
|
|
870
870
|
z: Type.Boolean() // properties: {
|
|
871
871
|
}) // x: { type: 'number' },
|
|
872
872
|
// y: { type: 'string' },
|
|
873
|
-
// z: { type: 'string' }
|
|
873
|
+
// z: { type: 'string' }
|
|
874
874
|
// }
|
|
875
875
|
// }
|
|
876
876
|
|
|
@@ -896,7 +896,7 @@ const C = Type.Index(T, Type.KeyOf(T)) // const C = {
|
|
|
896
896
|
|
|
897
897
|
### Rest Types
|
|
898
898
|
|
|
899
|
-
Rest parameters are supported with `Type.Rest`. This function is used to extract interior type elements from tuples which enables them to compose with the JavaScript spread operator `...`. This type can be used for tuple
|
|
899
|
+
Rest parameters are supported with `Type.Rest`. This function is used to extract interior type elements from tuples which enables them to compose with the JavaScript spread operator `...`. This type can be used for tuple concatenation as well as for variadic functions.
|
|
900
900
|
|
|
901
901
|
```typescript
|
|
902
902
|
// TypeScript
|
|
@@ -908,14 +908,14 @@ type C = [...T, number] // type C = [number, number
|
|
|
908
908
|
type F = (...param: C) => void // type F = (
|
|
909
909
|
// param0: number,
|
|
910
910
|
// param1: number,
|
|
911
|
-
// param2: number
|
|
911
|
+
// param2: number
|
|
912
912
|
// ) => void
|
|
913
913
|
|
|
914
914
|
// TypeBox
|
|
915
915
|
|
|
916
916
|
const T = Type.Tuple([ // const T: TTuple<[
|
|
917
917
|
Type.Number(), // TNumber,
|
|
918
|
-
Type.Number() // TNumber
|
|
918
|
+
Type.Number() // TNumber
|
|
919
919
|
]) // ]>
|
|
920
920
|
|
|
921
921
|
const C = Type.Tuple([ // const C: TTuple<[
|
|
@@ -1335,7 +1335,7 @@ console.log(C.Code()) // return function check(va
|
|
|
1335
1335
|
|
|
1336
1336
|
## TypeSystem
|
|
1337
1337
|
|
|
1338
|
-
The TypeBox TypeSystem module provides functionality to define types above and beyond the Standard and Extended type sets as well as control various assertion
|
|
1338
|
+
The TypeBox TypeSystem module provides functionality to define types above and beyond the Standard and Extended type sets as well as control various assertion policies. Configurations made to the TypeSystem module are observed by both `TypeCompiler` and `Value` modules.
|
|
1339
1339
|
|
|
1340
1340
|
The TypeSystem module is provided as an optional import.
|
|
1341
1341
|
|