@sinclair/typebox 0.32.0-dev-18 → 0.32.0-dev-19
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/import/compiler/compiler.mjs +14 -8
- package/build/import/errors/errors.mjs +15 -11
- package/build/import/type/awaited/awaited.mjs +4 -4
- package/build/import/type/deref/deref.mjs +12 -12
- package/build/import/type/exclude/exclude.mjs +8 -5
- package/build/import/type/extends/extends-check.mjs +167 -167
- package/build/import/type/extends/extends.mjs +3 -3
- package/build/import/type/extract/extract.mjs +5 -5
- package/build/import/type/guard/type.d.mts +50 -50
- package/build/import/type/guard/type.mjs +136 -136
- package/build/import/type/indexed/indexed-property-keys.mjs +7 -7
- package/build/import/type/indexed/indexed.mjs +9 -9
- package/build/import/type/intersect/intersect-create.mjs +4 -4
- package/build/import/type/intersect/intersect-evaluated.mjs +1 -1
- package/build/import/type/intersect/intersect.mjs +2 -2
- package/build/import/type/intrinsic/intrinsic.mjs +5 -5
- package/build/import/type/keyof/keyof-property-keys.mjs +7 -7
- package/build/import/type/keyof/keyof.mjs +2 -2
- package/build/import/type/mapped/mapped.mjs +16 -16
- package/build/import/type/modifiers/modifiers.mjs +3 -3
- package/build/import/type/object/object.mjs +3 -3
- package/build/import/type/omit/omit.mjs +6 -6
- package/build/import/type/partial/partial.mjs +5 -5
- package/build/import/type/pick/pick.mjs +6 -6
- package/build/import/type/record/record.mjs +7 -7
- package/build/import/type/required/required.mjs +5 -5
- package/build/import/type/rest/rest.mjs +6 -2
- package/build/import/type/template-literal/pattern.mjs +9 -9
- package/build/import/type/transform/transform.mjs +1 -1
- package/build/import/type/union/union-evaluated.mjs +2 -2
- package/build/import/value/check/check.mjs +18 -11
- package/build/import/value/clean/clean.mjs +9 -7
- package/build/import/value/clone/clone.mjs +7 -4
- package/build/import/value/convert/convert.mjs +6 -2
- package/build/import/value/default/default.mjs +12 -4
- package/build/import/value/transform/decode.mjs +12 -6
- package/build/import/value/transform/encode.mjs +12 -6
- package/build/import/value/transform/has.mjs +24 -18
- package/build/require/compiler/compiler.js +77 -71
- package/build/require/errors/errors.js +58 -54
- package/build/require/type/awaited/awaited.js +3 -3
- package/build/require/type/deref/deref.js +11 -11
- package/build/require/type/exclude/exclude.js +7 -4
- package/build/require/type/extends/extends-check.js +167 -167
- package/build/require/type/extends/extends.js +2 -2
- package/build/require/type/extract/extract.js +4 -4
- package/build/require/type/guard/type.d.ts +50 -50
- package/build/require/type/guard/type.js +184 -184
- package/build/require/type/indexed/indexed-property-keys.js +6 -6
- package/build/require/type/indexed/indexed.js +9 -9
- package/build/require/type/intersect/intersect-create.js +3 -3
- package/build/require/type/intersect/intersect-evaluated.js +2 -2
- package/build/require/type/intersect/intersect.js +1 -1
- package/build/require/type/intrinsic/intrinsic.js +4 -4
- package/build/require/type/keyof/keyof-property-keys.js +6 -6
- package/build/require/type/keyof/keyof.js +1 -1
- package/build/require/type/mapped/mapped.js +15 -15
- package/build/require/type/modifiers/modifiers.js +2 -2
- package/build/require/type/object/object.js +2 -2
- package/build/require/type/omit/omit.js +5 -5
- package/build/require/type/partial/partial.js +4 -4
- package/build/require/type/pick/pick.js +5 -5
- package/build/require/type/record/record.js +6 -6
- package/build/require/type/required/required.js +4 -4
- package/build/require/type/rest/rest.js +5 -1
- package/build/require/type/template-literal/pattern.js +8 -8
- package/build/require/type/transform/transform.js +1 -1
- package/build/require/type/union/union-evaluated.js +1 -1
- package/build/require/value/check/check.js +63 -56
- package/build/require/value/clean/clean.js +31 -29
- package/build/require/value/clone/clone.js +7 -4
- package/build/require/value/convert/convert.js +42 -38
- package/build/require/value/default/default.js +24 -16
- package/build/require/value/transform/decode.js +27 -21
- package/build/require/value/transform/encode.js +28 -22
- package/build/require/value/transform/has.js +29 -23
- package/package.json +1 -1
- package/readme.md +5 -5
|
@@ -1,16 +1,22 @@
|
|
|
1
1
|
import { TransformEncode, TransformDecode, HasTransform, TransformDecodeCheckError, TransformEncodeCheckError } from '../value/transform/index.mjs';
|
|
2
|
-
import { IsArray, IsString, IsNumber, IsBigInt } from '../value/guard/index.mjs';
|
|
3
2
|
import { Errors } from '../errors/index.mjs';
|
|
4
3
|
import { TypeSystemPolicy } from '../system/index.mjs';
|
|
5
4
|
import { Deref } from '../value/deref/index.mjs';
|
|
6
5
|
import { Hash } from '../value/hash/index.mjs';
|
|
7
6
|
import { Kind } from '../type/symbols/index.mjs';
|
|
8
|
-
import { TSchema as IsSchemaType } from '../type/guard/type.mjs';
|
|
9
7
|
import { TypeRegistry, FormatRegistry } from '../type/registry/index.mjs';
|
|
10
8
|
import { KeyOfPattern } from '../type/keyof/index.mjs';
|
|
11
9
|
import { ExtendsUndefinedCheck } from '../type/extends/extends-undefined.mjs';
|
|
12
10
|
import { Never } from '../type/never/index.mjs';
|
|
13
11
|
// ------------------------------------------------------------------
|
|
12
|
+
// ValueGuard
|
|
13
|
+
// ------------------------------------------------------------------
|
|
14
|
+
import { IsArray, IsString, IsNumber, IsBigInt } from '../value/guard/index.mjs';
|
|
15
|
+
// ------------------------------------------------------------------
|
|
16
|
+
// TypeGuard
|
|
17
|
+
// ------------------------------------------------------------------
|
|
18
|
+
import { IsSchema } from '../type/guard/type.mjs';
|
|
19
|
+
// ------------------------------------------------------------------
|
|
14
20
|
// TypeCheck
|
|
15
21
|
// ------------------------------------------------------------------
|
|
16
22
|
export class TypeCheck {
|
|
@@ -202,8 +208,8 @@ export var TypeCompiler;
|
|
|
202
208
|
yield `${value}.length >= ${schema.minItems}`;
|
|
203
209
|
const elementExpression = CreateExpression(schema.items, references, 'value');
|
|
204
210
|
yield `${value}.every((${parameter}) => ${elementExpression})`;
|
|
205
|
-
if (
|
|
206
|
-
const containsSchema =
|
|
211
|
+
if (IsSchema(schema.contains) || IsNumber(schema.minContains) || IsNumber(schema.maxContains)) {
|
|
212
|
+
const containsSchema = IsSchema(schema.contains) ? schema.contains : Never();
|
|
207
213
|
const checkExpression = CreateExpression(containsSchema, references, 'value');
|
|
208
214
|
const checkMinContains = IsNumber(schema.minContains) ? [`(count >= ${schema.minContains})`] : [];
|
|
209
215
|
const checkMaxContains = IsNumber(schema.maxContains) ? [`(count <= ${schema.maxContains})`] : [];
|
|
@@ -275,7 +281,7 @@ export var TypeCompiler;
|
|
|
275
281
|
const check2 = `Object.getOwnPropertyNames(${value}).every(key => ${keyCheck}.test(key))`;
|
|
276
282
|
yield `(${check1} && ${check2})`;
|
|
277
283
|
}
|
|
278
|
-
else if (
|
|
284
|
+
else if (IsSchema(schema.unevaluatedProperties)) {
|
|
279
285
|
const keyCheck = CreateVariable(`${new RegExp(KeyOfPattern(schema))};`);
|
|
280
286
|
const check2 = `Object.getOwnPropertyNames(${value}).every(key => ${keyCheck}.test(key) || ${CreateExpression(schema.unevaluatedProperties, references, `${value}[key]`)})`;
|
|
281
287
|
yield `(${check1} && ${check2})`;
|
|
@@ -365,7 +371,7 @@ export var TypeCompiler;
|
|
|
365
371
|
const [patternKey, patternSchema] = Object.entries(schema.patternProperties)[0];
|
|
366
372
|
const variable = CreateVariable(`${new RegExp(patternKey)}`);
|
|
367
373
|
const check1 = CreateExpression(patternSchema, references, 'value');
|
|
368
|
-
const check2 =
|
|
374
|
+
const check2 = IsSchema(schema.additionalProperties) ? CreateExpression(schema.additionalProperties, references, value) : schema.additionalProperties === false ? 'false' : 'true';
|
|
369
375
|
const expression = `(${variable}.test(key) ? ${check1} : ${check2})`;
|
|
370
376
|
yield `(Object.entries(${value}).every(([key, value]) => ${expression}))`;
|
|
371
377
|
}
|
|
@@ -589,10 +595,10 @@ export var TypeCompiler;
|
|
|
589
595
|
state.variables.clear();
|
|
590
596
|
state.functions.clear();
|
|
591
597
|
state.instances.clear();
|
|
592
|
-
if (!
|
|
598
|
+
if (!IsSchema(schema))
|
|
593
599
|
throw new TypeCompilerTypeGuardError(schema);
|
|
594
600
|
for (const schema of references)
|
|
595
|
-
if (!
|
|
601
|
+
if (!IsSchema(schema))
|
|
596
602
|
throw new TypeCompilerTypeGuardError(schema);
|
|
597
603
|
return Build(schema, references, options);
|
|
598
604
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { IsArray, IsUint8Array, IsDate, IsPromise, IsFunction, IsAsyncIterator, IsIterator, IsBoolean, IsNumber, IsBigInt, IsString, IsSymbol, IsInteger, IsNull, IsUndefined } from '../value/guard/index.mjs';
|
|
2
1
|
import { TypeSystemPolicy } from '../system/index.mjs';
|
|
3
2
|
import { KeyOfPattern } from '../type/keyof/index.mjs';
|
|
4
3
|
import { TypeRegistry, FormatRegistry } from '../type/registry/index.mjs';
|
|
@@ -8,9 +7,14 @@ import { Deref } from '../value/deref/index.mjs';
|
|
|
8
7
|
import { Hash } from '../value/hash/index.mjs';
|
|
9
8
|
import { Kind } from '../type/symbols/index.mjs';
|
|
10
9
|
import { Never } from '../type/never/index.mjs';
|
|
11
|
-
//
|
|
10
|
+
// ------------------------------------------------------------------
|
|
11
|
+
// ValueGuard
|
|
12
|
+
// ------------------------------------------------------------------
|
|
13
|
+
// prettier-ignore
|
|
14
|
+
import { IsArray, IsUint8Array, IsDate, IsPromise, IsFunction, IsAsyncIterator, IsIterator, IsBoolean, IsNumber, IsBigInt, IsString, IsSymbol, IsInteger, IsNull, IsUndefined } from '../value/guard/index.mjs';
|
|
15
|
+
// ------------------------------------------------------------------
|
|
12
16
|
// ValueErrorType
|
|
13
|
-
//
|
|
17
|
+
// ------------------------------------------------------------------
|
|
14
18
|
export var ValueErrorType;
|
|
15
19
|
(function (ValueErrorType) {
|
|
16
20
|
ValueErrorType[ValueErrorType["ArrayContains"] = 0] = "ArrayContains";
|
|
@@ -77,9 +81,9 @@ export var ValueErrorType;
|
|
|
77
81
|
ValueErrorType[ValueErrorType["Union"] = 61] = "Union";
|
|
78
82
|
ValueErrorType[ValueErrorType["Void"] = 62] = "Void";
|
|
79
83
|
})(ValueErrorType || (ValueErrorType = {}));
|
|
80
|
-
//
|
|
84
|
+
// ------------------------------------------------------------------
|
|
81
85
|
// ValueErrors
|
|
82
|
-
//
|
|
86
|
+
// ------------------------------------------------------------------
|
|
83
87
|
export class ValueErrorsUnknownTypeError extends Error {
|
|
84
88
|
schema;
|
|
85
89
|
constructor(schema) {
|
|
@@ -87,21 +91,21 @@ export class ValueErrorsUnknownTypeError extends Error {
|
|
|
87
91
|
this.schema = schema;
|
|
88
92
|
}
|
|
89
93
|
}
|
|
90
|
-
//
|
|
94
|
+
// ------------------------------------------------------------------
|
|
91
95
|
// EscapeKey
|
|
92
|
-
//
|
|
96
|
+
// ------------------------------------------------------------------
|
|
93
97
|
function EscapeKey(key) {
|
|
94
98
|
return key.replace(/~/g, '~0').replace(/\//g, '~1'); // RFC6901 Path
|
|
95
99
|
}
|
|
96
|
-
//
|
|
100
|
+
// ------------------------------------------------------------------
|
|
97
101
|
// Guards
|
|
98
|
-
//
|
|
102
|
+
// ------------------------------------------------------------------
|
|
99
103
|
function IsDefined(value) {
|
|
100
104
|
return value !== undefined;
|
|
101
105
|
}
|
|
102
|
-
//
|
|
106
|
+
// ------------------------------------------------------------------
|
|
103
107
|
// ValueErrorIterator
|
|
104
|
-
//
|
|
108
|
+
// ------------------------------------------------------------------
|
|
105
109
|
export class ValueErrorIterator {
|
|
106
110
|
iterator;
|
|
107
111
|
constructor(iterator) {
|
|
@@ -5,7 +5,7 @@ import { CloneType } from '../clone/type.mjs';
|
|
|
5
5
|
// TypeGuard
|
|
6
6
|
// ------------------------------------------------------------------
|
|
7
7
|
// prettier-ignore
|
|
8
|
-
import {
|
|
8
|
+
import { IsIntersect, IsUnion, IsPromise } from '../guard/type.mjs';
|
|
9
9
|
// prettier-ignore
|
|
10
10
|
function FromRest(T) {
|
|
11
11
|
const [L, ...R] = T;
|
|
@@ -27,9 +27,9 @@ function FromPromise(T) {
|
|
|
27
27
|
}
|
|
28
28
|
// prettier-ignore
|
|
29
29
|
function FromSchema(T) {
|
|
30
|
-
return (
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
return (IsIntersect(T) ? FromIntersect(T.allOf) :
|
|
31
|
+
IsUnion(T) ? FromUnion(T.anyOf) :
|
|
32
|
+
IsPromise(T) ? FromPromise(T.item) :
|
|
33
33
|
T);
|
|
34
34
|
}
|
|
35
35
|
/** `[JavaScript]` Constructs a type by recursively unwrapping Promise types */
|
|
@@ -5,7 +5,7 @@ import { IsUndefined } from '../guard/value.mjs';
|
|
|
5
5
|
// TypeGuard
|
|
6
6
|
// ------------------------------------------------------------------
|
|
7
7
|
// prettier-ignore
|
|
8
|
-
import {
|
|
8
|
+
import { IsConstructor, IsFunction, IsIntersect, IsUnion, IsTuple, IsArray, IsObject, IsPromise, IsAsyncIterator, IsIterator, IsRef, } from '../guard/type.mjs';
|
|
9
9
|
function FromRest(schema, references) {
|
|
10
10
|
return schema.map((schema) => Deref(schema, references));
|
|
11
11
|
}
|
|
@@ -89,17 +89,17 @@ function FromRef(schema, references) {
|
|
|
89
89
|
}
|
|
90
90
|
// prettier-ignore
|
|
91
91
|
export function DerefResolve(schema, references) {
|
|
92
|
-
return (
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
92
|
+
return (IsConstructor(schema) ? FromConstructor(schema, references) :
|
|
93
|
+
IsFunction(schema) ? FromFunction(schema, references) :
|
|
94
|
+
IsIntersect(schema) ? FromIntersect(schema, references) :
|
|
95
|
+
IsUnion(schema) ? FromUnion(schema, references) :
|
|
96
|
+
IsTuple(schema) ? FromTuple(schema, references) :
|
|
97
|
+
IsArray(schema) ? FromArray(schema, references) :
|
|
98
|
+
IsObject(schema) ? FromObject(schema, references) :
|
|
99
|
+
IsPromise(schema) ? FromPromise(schema, references) :
|
|
100
|
+
IsAsyncIterator(schema) ? FromAsyncIterator(schema, references) :
|
|
101
|
+
IsIterator(schema) ? FromIterator(schema, references) :
|
|
102
|
+
IsRef(schema) ? FromRef(schema, references) :
|
|
103
103
|
schema);
|
|
104
104
|
}
|
|
105
105
|
/** `[Json]` Creates a dereferenced type */
|
|
@@ -4,13 +4,16 @@ import { Never } from '../never/index.mjs';
|
|
|
4
4
|
import { ExtendsCheck, ExtendsResult } from '../extends/index.mjs';
|
|
5
5
|
import { CloneType } from '../clone/type.mjs';
|
|
6
6
|
import { ExcludeFromMappedResult } from './exclude-from-mapped-result.mjs';
|
|
7
|
+
// ------------------------------------------------------------------
|
|
8
|
+
// TypeGuard
|
|
9
|
+
// ------------------------------------------------------------------
|
|
7
10
|
// prettier-ignore
|
|
8
|
-
import {
|
|
11
|
+
import { IsMappedResult, IsTemplateLiteral, IsUnion } from '../guard/type.mjs';
|
|
9
12
|
// prettier-ignore
|
|
10
13
|
function ExcludeResolve(L, R) {
|
|
11
|
-
return (
|
|
12
|
-
|
|
13
|
-
|
|
14
|
+
return (IsTemplateLiteral(L) ? ExcludeResolve(TemplateLiteralToUnion(L), R) :
|
|
15
|
+
IsTemplateLiteral(R) ? ExcludeResolve(L, TemplateLiteralToUnion(R)) :
|
|
16
|
+
IsUnion(L) ? (() => {
|
|
14
17
|
const narrowed = L.anyOf.filter((inner) => ExtendsCheck(inner, R) === ExtendsResult.False);
|
|
15
18
|
return (narrowed.length === 1 ? narrowed[0] : Union(narrowed));
|
|
16
19
|
})() :
|
|
@@ -19,7 +22,7 @@ function ExcludeResolve(L, R) {
|
|
|
19
22
|
}
|
|
20
23
|
/** `[Json]` Constructs a type by excluding from unionType all union members that are assignable to excludedMembers */
|
|
21
24
|
export function Exclude(unionType, excludedMembers, options = {}) {
|
|
22
|
-
if (
|
|
25
|
+
if (IsMappedResult(unionType)) {
|
|
23
26
|
return ExcludeFromMappedResult(unionType, excludedMembers, options);
|
|
24
27
|
}
|
|
25
28
|
else {
|