@sinclair/typebox 0.33.7 → 0.33.9
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/cjs/compiler/compiler.d.ts +3 -3
- package/build/cjs/errors/errors.js +7 -4
- package/build/cjs/type/omit/omit.d.ts +4 -3
- package/build/cjs/type/omit/omit.js +11 -4
- package/build/cjs/type/partial/partial.d.ts +3 -2
- package/build/cjs/type/partial/partial.js +13 -5
- package/build/cjs/type/pick/pick.d.ts +4 -4
- package/build/cjs/type/pick/pick.js +11 -4
- package/build/cjs/type/required/required.d.ts +3 -2
- package/build/cjs/type/required/required.js +9 -4
- package/build/cjs/type/strict/strict.d.ts +11 -2
- package/build/cjs/type/strict/strict.js +9 -1
- package/build/cjs/type/type/json.d.ts +18 -9
- package/build/cjs/type/type/json.js +9 -1
- package/build/cjs/type/type/type.d.ts +1 -1
- package/build/cjs/type/type/type.js +6 -6
- package/build/cjs/value/create/create.js +2 -1
- package/build/cjs/value/default/default.js +2 -1
- package/build/cjs/value/transform/encode.js +1 -1
- package/build/cjs/value/value/value.d.ts +4 -4
- package/build/esm/compiler/compiler.d.mts +3 -3
- package/build/esm/errors/errors.mjs +7 -4
- package/build/esm/type/omit/omit.d.mts +4 -3
- package/build/esm/type/omit/omit.mjs +11 -4
- package/build/esm/type/partial/partial.d.mts +3 -2
- package/build/esm/type/partial/partial.mjs +13 -5
- package/build/esm/type/pick/pick.d.mts +4 -4
- package/build/esm/type/pick/pick.mjs +11 -4
- package/build/esm/type/required/required.d.mts +3 -2
- package/build/esm/type/required/required.mjs +9 -4
- package/build/esm/type/strict/strict.d.mts +11 -2
- package/build/esm/type/strict/strict.mjs +9 -1
- package/build/esm/type/type/json.d.mts +18 -9
- package/build/esm/type/type/json.mjs +9 -1
- package/build/esm/type/type/type.d.mts +1 -1
- package/build/esm/type/type/type.mjs +1 -1
- package/build/esm/value/create/create.mjs +2 -1
- package/build/esm/value/default/default.mjs +3 -2
- package/build/esm/value/transform/encode.mjs +1 -1
- package/build/esm/value/value/value.d.mts +4 -4
- package/package.json +1 -1
- package/readme.md +0 -30
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ValueErrorIterator } from '../errors/index';
|
|
2
2
|
import { TypeBoxError } from '../type/error/index';
|
|
3
3
|
import type { TSchema } from '../type/schema/index';
|
|
4
|
-
import type { Static, StaticDecode
|
|
4
|
+
import type { Static, StaticDecode } from '../type/static/index';
|
|
5
5
|
export type CheckFunction = (value: unknown) => boolean;
|
|
6
6
|
export declare class TypeCheck<T extends TSchema> {
|
|
7
7
|
private readonly schema;
|
|
@@ -17,9 +17,9 @@ export declare class TypeCheck<T extends TSchema> {
|
|
|
17
17
|
/** Returns true if the value matches the compiled type. */
|
|
18
18
|
Check(value: unknown): value is Static<T>;
|
|
19
19
|
/** Decodes a value or throws if error */
|
|
20
|
-
Decode<
|
|
20
|
+
Decode<Static = StaticDecode<T>, Result extends Static = Static>(value: unknown): Result;
|
|
21
21
|
/** Encodes a value or throws if error */
|
|
22
|
-
Encode<
|
|
22
|
+
Encode<Static = StaticDecode<T>, Result extends Static = Static>(value: unknown): Result;
|
|
23
23
|
}
|
|
24
24
|
export declare class TypeCompilerUnknownTypeError extends TypeBoxError {
|
|
25
25
|
readonly schema: TSchema;
|
|
@@ -251,13 +251,16 @@ function* FromInteger(schema, references, path, value) {
|
|
|
251
251
|
}
|
|
252
252
|
}
|
|
253
253
|
function* FromIntersect(schema, references, path, value) {
|
|
254
|
+
let hasError = false;
|
|
254
255
|
for (const inner of schema.allOf) {
|
|
255
|
-
const
|
|
256
|
-
|
|
257
|
-
yield
|
|
258
|
-
yield next.value;
|
|
256
|
+
for (const error of Visit(inner, references, path, value)) {
|
|
257
|
+
hasError = true;
|
|
258
|
+
yield error;
|
|
259
259
|
}
|
|
260
260
|
}
|
|
261
|
+
if (hasError) {
|
|
262
|
+
return yield Create(ValueErrorType.Intersect, schema, path, value);
|
|
263
|
+
}
|
|
261
264
|
if (schema.unevaluatedProperties === false) {
|
|
262
265
|
const keyCheck = new RegExp((0, index_2.KeyOfPattern)(schema));
|
|
263
266
|
for (const valueKey of Object.getOwnPropertyNames(value)) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { TupleToUnion, Evaluate } from '../helpers/index';
|
|
1
|
+
import type { SchemaOptions, TSchema } from '../schema/index';
|
|
2
|
+
import type { TupleToUnion, Evaluate, Ensure } from '../helpers/index';
|
|
3
3
|
import { type TRecursive } from '../recursive/index';
|
|
4
4
|
import type { TMappedKey, TMappedResult } from '../mapped/index';
|
|
5
5
|
import { type TIntersect } from '../intersect/index';
|
|
@@ -11,7 +11,8 @@ import { type TOmitFromMappedResult } from './omit-from-mapped-result';
|
|
|
11
11
|
type TFromIntersect<T extends TSchema[], K extends PropertyKey[], Acc extends TSchema[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromIntersect<R, K, [...Acc, TOmit<L, K>]> : Acc);
|
|
12
12
|
type TFromUnion<T extends TSchema[], K extends PropertyKey[], Acc extends TSchema[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromUnion<R, K, [...Acc, TOmit<L, K>]> : Acc);
|
|
13
13
|
type TFromProperties<T extends TProperties, K extends PropertyKey[], I extends PropertyKey = TupleToUnion<K>> = Evaluate<Omit<T, I>>;
|
|
14
|
-
|
|
14
|
+
type TFromObject<T extends TObject, K extends PropertyKey[], Properties extends TProperties = T['properties']> = Ensure<TObject<(TFromProperties<Properties, K>)>>;
|
|
15
|
+
export type TOmit<T extends TProperties, K extends PropertyKey[]> = (T extends TRecursive<infer S extends TSchema> ? TRecursive<TOmit<S, K>> : T extends TIntersect<infer S extends TSchema[]> ? TIntersect<TFromIntersect<S, K>> : T extends TUnion<infer S extends TSchema[]> ? TUnion<TFromUnion<S, K>> : T extends TObject<infer S extends TProperties> ? TFromObject<TObject<S>, K> : TObject<{}>);
|
|
15
16
|
/** `[Json]` Constructs a type whose keys are omitted from the given type */
|
|
16
17
|
export declare function Omit<T extends TMappedResult, K extends PropertyKey[]>(T: T, K: [...K], options?: SchemaOptions): TOmitFromMappedResult<T, K>;
|
|
17
18
|
/** `[Json]` Constructs a type whose keys are omitted from the given type */
|
|
@@ -3,14 +3,14 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.Omit = Omit;
|
|
5
5
|
const type_1 = require("../create/type");
|
|
6
|
+
const discard_1 = require("../discard/discard");
|
|
6
7
|
const index_1 = require("../intersect/index");
|
|
7
8
|
const index_2 = require("../union/index");
|
|
8
9
|
const index_3 = require("../object/index");
|
|
9
10
|
const index_4 = require("../indexed/index");
|
|
10
|
-
const index_5 = require("../discard/index");
|
|
11
|
-
const index_6 = require("../symbols/index");
|
|
12
11
|
const omit_from_mapped_key_1 = require("./omit-from-mapped-key");
|
|
13
12
|
const omit_from_mapped_result_1 = require("./omit-from-mapped-result");
|
|
13
|
+
const symbols_1 = require("../symbols/symbols");
|
|
14
14
|
// ------------------------------------------------------------------
|
|
15
15
|
// TypeGuard
|
|
16
16
|
// ------------------------------------------------------------------
|
|
@@ -35,6 +35,12 @@ function FromProperty(T, K) {
|
|
|
35
35
|
function FromProperties(T, K) {
|
|
36
36
|
return K.reduce((T, K2) => FromProperty(T, K2), T);
|
|
37
37
|
}
|
|
38
|
+
// prettier-ignore
|
|
39
|
+
function FromObject(T, K) {
|
|
40
|
+
const options = (0, discard_1.Discard)(T, [symbols_1.TransformKind, '$id', 'required', 'properties']);
|
|
41
|
+
const properties = FromProperties(T['properties'], K);
|
|
42
|
+
return (0, index_3.Object)(properties, options);
|
|
43
|
+
}
|
|
38
44
|
// ------------------------------------------------------------------
|
|
39
45
|
// OmitResolve
|
|
40
46
|
// ------------------------------------------------------------------
|
|
@@ -42,7 +48,7 @@ function FromProperties(T, K) {
|
|
|
42
48
|
function OmitResolve(T, K) {
|
|
43
49
|
return ((0, kind_1.IsIntersect)(T) ? (0, index_1.Intersect)(FromIntersect(T.allOf, K)) :
|
|
44
50
|
(0, kind_1.IsUnion)(T) ? (0, index_2.Union)(FromUnion(T.anyOf, K)) :
|
|
45
|
-
(0, kind_1.IsObject)(T) ? (
|
|
51
|
+
(0, kind_1.IsObject)(T) ? FromObject(T, K) :
|
|
46
52
|
(0, index_3.Object)({}));
|
|
47
53
|
}
|
|
48
54
|
function Omit(T, K, options) {
|
|
@@ -53,5 +59,6 @@ function Omit(T, K, options) {
|
|
|
53
59
|
return (0, omit_from_mapped_result_1.OmitFromMappedResult)(T, K, options);
|
|
54
60
|
// non-mapped
|
|
55
61
|
const I = (0, kind_1.IsSchema)(K) ? (0, index_4.IndexPropertyKeys)(K) : K;
|
|
56
|
-
|
|
62
|
+
// special: mapping types require overridable options
|
|
63
|
+
return (0, type_1.CreateType)({ ...OmitResolve(T, I), ...options });
|
|
57
64
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { TSchema, SchemaOptions } from '../schema/index';
|
|
2
|
-
import type { Evaluate } from '../helpers/index';
|
|
2
|
+
import type { Evaluate, Ensure } from '../helpers/index';
|
|
3
3
|
import type { TMappedResult } from '../mapped/index';
|
|
4
4
|
import { type TReadonlyOptional } from '../readonly-optional/index';
|
|
5
5
|
import { type TOptional } from '../optional/index';
|
|
@@ -13,7 +13,8 @@ type TFromRest<T extends TSchema[], Acc extends TSchema[] = []> = (T extends [in
|
|
|
13
13
|
type TFromProperties<T extends TProperties> = Evaluate<{
|
|
14
14
|
[K in keyof T]: T[K] extends (TReadonlyOptional<infer S>) ? TReadonlyOptional<S> : T[K] extends (TReadonly<infer S>) ? TReadonlyOptional<S> : T[K] extends (TOptional<infer S>) ? TOptional<S> : TOptional<T[K]>;
|
|
15
15
|
}>;
|
|
16
|
-
|
|
16
|
+
type TFromObject<T extends TObject, Properties extends TProperties = T['properties']> = Ensure<TObject<(TFromProperties<Properties>)>>;
|
|
17
|
+
export type TPartial<T extends TSchema> = (T extends TRecursive<infer S extends TSchema> ? TRecursive<TPartial<S>> : T extends TIntersect<infer S extends TSchema[]> ? TIntersect<TFromRest<S>> : T extends TUnion<infer S extends TSchema[]> ? TUnion<TFromRest<S>> : T extends TObject<infer S extends TProperties> ? TFromObject<TObject<S>> : TObject<{}>);
|
|
17
18
|
/** `[Json]` Constructs a type where all properties are optional */
|
|
18
19
|
export declare function Partial<T extends TMappedResult>(T: T, options?: SchemaOptions): TPartialFromMappedResult<T>;
|
|
19
20
|
/** `[Json]` Constructs a type where all properties are optional */
|
|
@@ -25,6 +25,12 @@ function FromProperties(T) {
|
|
|
25
25
|
Acc[K] = (0, index_1.Optional)(T[K]);
|
|
26
26
|
return Acc;
|
|
27
27
|
}
|
|
28
|
+
// prettier-ignore
|
|
29
|
+
function FromObject(T) {
|
|
30
|
+
const options = (0, index_5.Discard)(T, [index_6.TransformKind, '$id', 'required', 'properties']);
|
|
31
|
+
const properties = FromProperties(T['properties']);
|
|
32
|
+
return (0, index_2.Object)(properties, options);
|
|
33
|
+
}
|
|
28
34
|
// ------------------------------------------------------------------
|
|
29
35
|
// PartialResolve
|
|
30
36
|
// ------------------------------------------------------------------
|
|
@@ -32,14 +38,16 @@ function FromProperties(T) {
|
|
|
32
38
|
function PartialResolve(T) {
|
|
33
39
|
return ((0, kind_1.IsIntersect)(T) ? (0, index_3.Intersect)(FromRest(T.allOf)) :
|
|
34
40
|
(0, kind_1.IsUnion)(T) ? (0, index_4.Union)(FromRest(T.anyOf)) :
|
|
35
|
-
(0, kind_1.IsObject)(T) ? (
|
|
41
|
+
(0, kind_1.IsObject)(T) ? FromObject(T) :
|
|
36
42
|
(0, index_2.Object)({}));
|
|
37
43
|
}
|
|
38
44
|
/** `[Json]` Constructs a type where all properties are optional */
|
|
39
45
|
function Partial(T, options) {
|
|
40
|
-
if ((0, kind_1.IsMappedResult)(T))
|
|
46
|
+
if ((0, kind_1.IsMappedResult)(T)) {
|
|
41
47
|
return (0, partial_from_mapped_result_1.PartialFromMappedResult)(T, options);
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
// special: mapping types require overridable options
|
|
51
|
+
return (0, type_1.CreateType)({ ...PartialResolve(T), ...options });
|
|
52
|
+
}
|
|
45
53
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { TSchema, SchemaOptions } from '../schema/index';
|
|
2
|
-
import type { TupleToUnion, Evaluate } from '../helpers/index';
|
|
2
|
+
import type { TupleToUnion, Evaluate, Ensure } from '../helpers/index';
|
|
3
3
|
import { type TRecursive } from '../recursive/index';
|
|
4
4
|
import { type TIntersect } from '../intersect/index';
|
|
5
5
|
import { type TUnion } from '../union/index';
|
|
@@ -12,9 +12,9 @@ type FromIntersect<T extends TSchema[], K extends PropertyKey[], Acc extends TSc
|
|
|
12
12
|
declare function FromIntersect<T extends TSchema[], K extends PropertyKey[]>(T: T, K: K): FromIntersect<T, K>;
|
|
13
13
|
type FromUnion<T extends TSchema[], K extends PropertyKey[], Acc extends TSchema[] = []> = T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? FromUnion<R, K, [...Acc, TPick<L, K>]> : Acc;
|
|
14
14
|
declare function FromUnion<T extends TSchema[], K extends PropertyKey[]>(T: T, K: K): FromUnion<T, K>;
|
|
15
|
-
type
|
|
16
|
-
|
|
17
|
-
export type TPick<T extends TProperties, K extends PropertyKey[]> = T extends TRecursive<infer S> ? TRecursive<TPick<S, K>> : T extends TIntersect<infer S> ? TIntersect<FromIntersect<S, K>> : T extends TUnion<infer S> ? TUnion<FromUnion<S, K>> : T extends TObject<infer S> ? TObject<
|
|
15
|
+
type TFromProperties<T extends TProperties, K extends PropertyKey[], I extends PropertyKey = TupleToUnion<K>> = Evaluate<Pick<T, I & keyof T>>;
|
|
16
|
+
type TFromObject<T extends TObject, K extends PropertyKey[], Properties extends TProperties = T['properties']> = Ensure<TObject<(TFromProperties<Properties, K>)>>;
|
|
17
|
+
export type TPick<T extends TProperties, K extends PropertyKey[]> = T extends TRecursive<infer S extends TSchema> ? TRecursive<TPick<S, K>> : T extends TIntersect<infer S extends TSchema[]> ? TIntersect<FromIntersect<S, K>> : T extends TUnion<infer S extends TSchema[]> ? TUnion<FromUnion<S, K>> : T extends TObject<infer S extends TProperties> ? TFromObject<TObject<S>, K> : TObject<{}>;
|
|
18
18
|
/** `[Json]` Constructs a type whose keys are picked from the given type */
|
|
19
19
|
export declare function Pick<T extends TMappedResult, K extends PropertyKey[]>(T: T, K: [...K], options?: SchemaOptions): TPickFromMappedResult<T, K>;
|
|
20
20
|
/** `[Json]` Constructs a type whose keys are picked from the given type */
|
|
@@ -3,14 +3,14 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.Pick = Pick;
|
|
5
5
|
const type_1 = require("../create/type");
|
|
6
|
+
const discard_1 = require("../discard/discard");
|
|
6
7
|
const index_1 = require("../intersect/index");
|
|
7
8
|
const index_2 = require("../union/index");
|
|
8
9
|
const index_3 = require("../object/index");
|
|
9
10
|
const index_4 = require("../indexed/index");
|
|
10
|
-
const index_5 = require("../discard/index");
|
|
11
|
-
const index_6 = require("../symbols/index");
|
|
12
11
|
const pick_from_mapped_key_1 = require("./pick-from-mapped-key");
|
|
13
12
|
const pick_from_mapped_result_1 = require("./pick-from-mapped-result");
|
|
13
|
+
const symbols_1 = require("../symbols/symbols");
|
|
14
14
|
// ------------------------------------------------------------------
|
|
15
15
|
// TypeGuard
|
|
16
16
|
// ------------------------------------------------------------------
|
|
@@ -30,6 +30,12 @@ function FromProperties(T, K) {
|
|
|
30
30
|
Acc[K2] = T[K2];
|
|
31
31
|
return Acc;
|
|
32
32
|
}
|
|
33
|
+
// prettier-ignore
|
|
34
|
+
function FromObject(T, K) {
|
|
35
|
+
const options = (0, discard_1.Discard)(T, [symbols_1.TransformKind, '$id', 'required', 'properties']);
|
|
36
|
+
const properties = FromProperties(T['properties'], K);
|
|
37
|
+
return (0, index_3.Object)(properties, options);
|
|
38
|
+
}
|
|
33
39
|
// ------------------------------------------------------------------
|
|
34
40
|
// PickResolve
|
|
35
41
|
// ------------------------------------------------------------------
|
|
@@ -37,7 +43,7 @@ function FromProperties(T, K) {
|
|
|
37
43
|
function PickResolve(T, K) {
|
|
38
44
|
return ((0, kind_1.IsIntersect)(T) ? (0, index_1.Intersect)(FromIntersect(T.allOf, K)) :
|
|
39
45
|
(0, kind_1.IsUnion)(T) ? (0, index_2.Union)(FromUnion(T.anyOf, K)) :
|
|
40
|
-
(0, kind_1.IsObject)(T) ? (
|
|
46
|
+
(0, kind_1.IsObject)(T) ? FromObject(T, K) :
|
|
41
47
|
(0, index_3.Object)({}));
|
|
42
48
|
}
|
|
43
49
|
function Pick(T, K, options) {
|
|
@@ -48,5 +54,6 @@ function Pick(T, K, options) {
|
|
|
48
54
|
return (0, pick_from_mapped_result_1.PickFromMappedResult)(T, K, options);
|
|
49
55
|
// non-mapped
|
|
50
56
|
const I = (0, kind_1.IsSchema)(K) ? (0, index_4.IndexPropertyKeys)(K) : K;
|
|
51
|
-
|
|
57
|
+
// special: mapping types require overridable options
|
|
58
|
+
return (0, type_1.CreateType)({ ...PickResolve(T, I), ...options });
|
|
52
59
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { TSchema, SchemaOptions } from '../schema/index';
|
|
2
|
-
import type { Evaluate } from '../helpers/index';
|
|
2
|
+
import type { Evaluate, Ensure } from '../helpers/index';
|
|
3
3
|
import type { TMappedResult } from '../mapped/index';
|
|
4
4
|
import { type TReadonlyOptional } from '../readonly-optional/index';
|
|
5
5
|
import { type TOptional } from '../optional/index';
|
|
@@ -13,7 +13,8 @@ type TFromRest<T extends TSchema[], Acc extends TSchema[] = []> = (T extends [in
|
|
|
13
13
|
type TFromProperties<T extends TProperties> = Evaluate<{
|
|
14
14
|
[K in keyof T]: T[K] extends (TReadonlyOptional<infer S>) ? TReadonly<S> : T[K] extends (TReadonly<infer S>) ? TReadonly<S> : T[K] extends (TOptional<infer S>) ? S : T[K];
|
|
15
15
|
}>;
|
|
16
|
-
|
|
16
|
+
type TFromObject<T extends TObject, Properties extends TProperties = T['properties']> = Ensure<TObject<(TFromProperties<Properties>)>>;
|
|
17
|
+
export type TRequired<T extends TSchema> = (T extends TRecursive<infer S extends TSchema> ? TRecursive<TRequired<S>> : T extends TIntersect<infer S extends TSchema[]> ? TIntersect<TFromRest<S>> : T extends TUnion<infer S extends TSchema[]> ? TUnion<TFromRest<S>> : T extends TObject<infer S extends TProperties> ? TFromObject<TObject<S>> : TObject<{}>);
|
|
17
18
|
/** `[Json]` Constructs a type where all properties are required */
|
|
18
19
|
export declare function Required<T extends TMappedResult>(T: T, options?: SchemaOptions): TRequiredFromMappedResult<T>;
|
|
19
20
|
/** `[Json]` Constructs a type where all properties are required */
|
|
@@ -24,6 +24,12 @@ function FromProperties(T) {
|
|
|
24
24
|
Acc[K] = (0, index_5.Discard)(T[K], [index_4.OptionalKind]);
|
|
25
25
|
return Acc;
|
|
26
26
|
}
|
|
27
|
+
// prettier-ignore
|
|
28
|
+
function FromObject(T) {
|
|
29
|
+
const options = (0, index_5.Discard)(T, [index_4.TransformKind, '$id', 'required', 'properties']);
|
|
30
|
+
const properties = FromProperties(T['properties']);
|
|
31
|
+
return (0, index_3.Object)(properties, options);
|
|
32
|
+
}
|
|
27
33
|
// ------------------------------------------------------------------
|
|
28
34
|
// RequiredResolve
|
|
29
35
|
// ------------------------------------------------------------------
|
|
@@ -31,7 +37,7 @@ function FromProperties(T) {
|
|
|
31
37
|
function RequiredResolve(T) {
|
|
32
38
|
return ((0, kind_1.IsIntersect)(T) ? (0, index_1.Intersect)(FromRest(T.allOf)) :
|
|
33
39
|
(0, kind_1.IsUnion)(T) ? (0, index_2.Union)(FromRest(T.anyOf)) :
|
|
34
|
-
(0, kind_1.IsObject)(T) ? (
|
|
40
|
+
(0, kind_1.IsObject)(T) ? FromObject(T) :
|
|
35
41
|
(0, index_3.Object)({}));
|
|
36
42
|
}
|
|
37
43
|
/** `[Json]` Constructs a type where all properties are required */
|
|
@@ -40,8 +46,7 @@ function Required(T, options) {
|
|
|
40
46
|
return (0, required_from_mapped_result_1.RequiredFromMappedResult)(T, options);
|
|
41
47
|
}
|
|
42
48
|
else {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
return (0, type_1.CreateType)({ ...D, ...R }, options);
|
|
49
|
+
// special: mapping types require overridable options
|
|
50
|
+
return (0, type_1.CreateType)({ ...RequiredResolve(T), ...options });
|
|
46
51
|
}
|
|
47
52
|
}
|
|
@@ -1,3 +1,12 @@
|
|
|
1
1
|
import type { TSchema } from '../schema/index';
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
export type TStrict<T extends TSchema> = T;
|
|
3
|
+
/**
|
|
4
|
+
* @deprecated `[Json]` Omits compositing symbols from this schema. It is recommended
|
|
5
|
+
* to use the JSON parse/stringify to remove compositing symbols if needed. This
|
|
6
|
+
* is how Strict works internally.
|
|
7
|
+
*
|
|
8
|
+
* ```typescript
|
|
9
|
+
* JSON.parse(JSON.stringify(Type.String()))
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export declare function Strict<T extends TSchema>(schema: T): TStrict<T>;
|
|
@@ -2,7 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.Strict = Strict;
|
|
5
|
-
/**
|
|
5
|
+
/**
|
|
6
|
+
* @deprecated `[Json]` Omits compositing symbols from this schema. It is recommended
|
|
7
|
+
* to use the JSON parse/stringify to remove compositing symbols if needed. This
|
|
8
|
+
* is how Strict works internally.
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* JSON.parse(JSON.stringify(Type.String()))
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
6
14
|
function Strict(schema) {
|
|
7
15
|
return JSON.parse(JSON.stringify(schema));
|
|
8
16
|
}
|
|
@@ -33,6 +33,7 @@ import { type TRef } from '../ref/index';
|
|
|
33
33
|
import { type TRequired, type TRequiredFromMappedResult } from '../required/index';
|
|
34
34
|
import { type TRest } from '../rest/index';
|
|
35
35
|
import { type TSchema, type SchemaOptions } from '../schema/index';
|
|
36
|
+
import { type TStrict } from '../strict/index';
|
|
36
37
|
import { type TString, type StringOptions } from '../string/index';
|
|
37
38
|
import { type TTemplateLiteral, type TTemplateLiteralKind, type TTemplateLiteralSyntax } from '../template-literal/index';
|
|
38
39
|
import { TransformDecodeBuilder } from '../transform/index';
|
|
@@ -42,8 +43,16 @@ import { type TUnknown } from '../unknown/index';
|
|
|
42
43
|
import { type TUnsafe, type UnsafeOptions } from '../unsafe/index';
|
|
43
44
|
/** Json Type Builder with Static Resolution for TypeScript */
|
|
44
45
|
export declare class JsonTypeBuilder {
|
|
45
|
-
/**
|
|
46
|
-
|
|
46
|
+
/**
|
|
47
|
+
* @deprecated `[Json]` Omits compositing symbols from this schema. It is recommended
|
|
48
|
+
* to use the JSON parse/stringify to remove compositing symbols if needed. This
|
|
49
|
+
* is how Strict works internally.
|
|
50
|
+
*
|
|
51
|
+
* ```typescript
|
|
52
|
+
* JSON.parse(JSON.stringify(Type.String()))
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
Strict<T extends TSchema>(schema: T): TStrict<T>;
|
|
47
56
|
/** `[Json]` Creates a Readonly and Optional property */
|
|
48
57
|
ReadonlyOptional<T extends TSchema>(schema: T): TReadonlyOptional<T>;
|
|
49
58
|
/** `[Json]` Creates a Readonly property */
|
|
@@ -133,19 +142,19 @@ export declare class JsonTypeBuilder {
|
|
|
133
142
|
/** `[Json]` Constructs a type whose keys are omitted from the given type */
|
|
134
143
|
Omit<T extends TMappedResult, K extends PropertyKey[]>(T: T, K: [...K], options?: SchemaOptions): TOmitFromMappedResult<T, K>;
|
|
135
144
|
/** `[Json]` Constructs a type whose keys are omitted from the given type */
|
|
136
|
-
Omit<T extends TSchema, K extends TMappedKey>(T: T, K: K): TOmitFromMappedKey<T, K>;
|
|
145
|
+
Omit<T extends TSchema, K extends TMappedKey>(T: T, K: K, options?: SchemaOptions): TOmitFromMappedKey<T, K>;
|
|
137
146
|
/** `[Json]` Constructs a type whose keys are omitted from the given type */
|
|
138
147
|
Omit<T extends TSchema, K extends TSchema, I extends PropertyKey[] = TIndexPropertyKeys<K>>(T: T, K: K, options?: SchemaOptions): TOmit<T, I>;
|
|
139
148
|
/** `[Json]` Constructs a type whose keys are omitted from the given type */
|
|
140
149
|
Omit<T extends TSchema, K extends PropertyKey[]>(T: T, K: readonly [...K], options?: SchemaOptions): TOmit<T, K>;
|
|
141
150
|
/** `[Json]` Constructs a type where all properties are optional */
|
|
142
|
-
Partial<T extends TMappedResult>(T: T, options?:
|
|
151
|
+
Partial<T extends TMappedResult>(T: T, options?: SchemaOptions): TPartialFromMappedResult<T>;
|
|
143
152
|
/** `[Json]` Constructs a type where all properties are optional */
|
|
144
|
-
Partial<T extends TSchema>(schema: T, options?:
|
|
153
|
+
Partial<T extends TSchema>(schema: T, options?: SchemaOptions): TPartial<T>;
|
|
145
154
|
/** `[Json]` Constructs a type whose keys are picked from the given type */
|
|
146
|
-
Pick<T extends TMappedResult, K extends PropertyKey[]>(T: T, K: [...K]): TPickFromMappedResult<T, K>;
|
|
155
|
+
Pick<T extends TMappedResult, K extends PropertyKey[]>(T: T, K: [...K], options?: SchemaOptions): TPickFromMappedResult<T, K>;
|
|
147
156
|
/** `[Json]` Constructs a type whose keys are picked from the given type */
|
|
148
|
-
Pick<T extends TSchema, K extends TMappedKey>(T: T, K: K): TPickFromMappedKey<T, K>;
|
|
157
|
+
Pick<T extends TSchema, K extends TMappedKey>(T: T, K: K, options?: SchemaOptions): TPickFromMappedKey<T, K>;
|
|
149
158
|
/** `[Json]` Constructs a type whose keys are picked from the given type */
|
|
150
159
|
Pick<T extends TSchema, K extends TSchema, I extends PropertyKey[] = TIndexPropertyKeys<K>>(T: T, K: K, options?: SchemaOptions): TPick<T, I>;
|
|
151
160
|
/** `[Json]` Constructs a type whose keys are picked from the given type */
|
|
@@ -159,9 +168,9 @@ export declare class JsonTypeBuilder {
|
|
|
159
168
|
/** `[Json]` Creates a Ref type. */
|
|
160
169
|
Ref<T extends TSchema>($ref: string, options?: SchemaOptions): TRef<T>;
|
|
161
170
|
/** `[Json]` Constructs a type where all properties are required */
|
|
162
|
-
Required<T extends TMappedResult>(T: T, options?:
|
|
171
|
+
Required<T extends TMappedResult>(T: T, options?: SchemaOptions): TRequiredFromMappedResult<T>;
|
|
163
172
|
/** `[Json]` Constructs a type where all properties are required */
|
|
164
|
-
Required<T extends TSchema>(schema: T, options?:
|
|
173
|
+
Required<T extends TSchema>(schema: T, options?: SchemaOptions): TRequired<T>;
|
|
165
174
|
/** `[Json]` Extracts interior Rest elements from Tuple, Intersect and Union types */
|
|
166
175
|
Rest<T extends TSchema>(schema: T): TRest<T>;
|
|
167
176
|
/** `[Json]` Creates a String type */
|
|
@@ -48,7 +48,15 @@ class JsonTypeBuilder {
|
|
|
48
48
|
// ------------------------------------------------------------------------
|
|
49
49
|
// Strict
|
|
50
50
|
// ------------------------------------------------------------------------
|
|
51
|
-
/**
|
|
51
|
+
/**
|
|
52
|
+
* @deprecated `[Json]` Omits compositing symbols from this schema. It is recommended
|
|
53
|
+
* to use the JSON parse/stringify to remove compositing symbols if needed. This
|
|
54
|
+
* is how Strict works internally.
|
|
55
|
+
*
|
|
56
|
+
* ```typescript
|
|
57
|
+
* JSON.parse(JSON.stringify(Type.String()))
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
52
60
|
Strict(schema) {
|
|
53
61
|
return (0, index_34.Strict)(schema);
|
|
54
62
|
}
|
|
@@ -44,8 +44,8 @@ export { RegExp } from '../regexp/index';
|
|
|
44
44
|
export { Required } from '../required/index';
|
|
45
45
|
export { Rest } from '../rest/index';
|
|
46
46
|
export { ReturnType } from '../return-type/index';
|
|
47
|
-
export { Strict } from '../strict/index';
|
|
48
47
|
export { String } from '../string/index';
|
|
48
|
+
export { Strict } from '../strict/index';
|
|
49
49
|
export { Symbol } from '../symbol/index';
|
|
50
50
|
export { TemplateLiteral } from '../template-literal/index';
|
|
51
51
|
export { Transform } from '../transform/index';
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.
|
|
5
|
-
exports.Void = exports.Unsafe = exports.Unknown = exports.Union = exports.Undefined = exports.Uint8Array = exports.Tuple = exports.Transform = exports.TemplateLiteral = exports.Symbol = exports.
|
|
4
|
+
exports.String = exports.ReturnType = exports.Rest = exports.Required = exports.RegExp = exports.Ref = exports.Recursive = exports.Record = exports.ReadonlyOptional = exports.Readonly = exports.Promise = exports.Pick = exports.Partial = exports.Parameters = exports.Optional = exports.Omit = exports.Object = exports.Number = exports.Null = exports.Not = exports.Never = exports.Mapped = exports.Literal = exports.KeyOf = exports.Iterator = exports.Uppercase = exports.Lowercase = exports.Uncapitalize = exports.Capitalize = exports.Intersect = exports.Integer = exports.InstanceType = exports.Index = exports.Function = exports.Extract = exports.Extends = exports.Exclude = exports.Enum = exports.Deref = exports.Date = exports.ConstructorParameters = exports.Constructor = exports.Const = exports.Composite = exports.Boolean = exports.BigInt = exports.Awaited = exports.AsyncIterator = exports.Array = exports.Any = void 0;
|
|
5
|
+
exports.Void = exports.Unsafe = exports.Unknown = exports.Union = exports.Undefined = exports.Uint8Array = exports.Tuple = exports.Transform = exports.TemplateLiteral = exports.Symbol = exports.Strict = void 0;
|
|
6
6
|
// ------------------------------------------------------------------
|
|
7
7
|
// Type: Module
|
|
8
8
|
// ------------------------------------------------------------------
|
|
@@ -101,10 +101,10 @@ var index_45 = require("../rest/index");
|
|
|
101
101
|
Object.defineProperty(exports, "Rest", { enumerable: true, get: function () { return index_45.Rest; } });
|
|
102
102
|
var index_46 = require("../return-type/index");
|
|
103
103
|
Object.defineProperty(exports, "ReturnType", { enumerable: true, get: function () { return index_46.ReturnType; } });
|
|
104
|
-
var index_47 = require("../
|
|
105
|
-
Object.defineProperty(exports, "
|
|
106
|
-
var index_48 = require("../
|
|
107
|
-
Object.defineProperty(exports, "
|
|
104
|
+
var index_47 = require("../string/index");
|
|
105
|
+
Object.defineProperty(exports, "String", { enumerable: true, get: function () { return index_47.String; } });
|
|
106
|
+
var index_48 = require("../strict/index");
|
|
107
|
+
Object.defineProperty(exports, "Strict", { enumerable: true, get: function () { return index_48.Strict; } });
|
|
108
108
|
var index_49 = require("../symbol/index");
|
|
109
109
|
Object.defineProperty(exports, "Symbol", { enumerable: true, get: function () { return index_49.Symbol; } });
|
|
110
110
|
var index_50 = require("../template-literal/index");
|
|
@@ -12,6 +12,7 @@ const index_6 = require("../../type/patterns/index");
|
|
|
12
12
|
const index_7 = require("../../type/registry/index");
|
|
13
13
|
const index_8 = require("../../type/symbols/index");
|
|
14
14
|
const index_9 = require("../../type/error/index");
|
|
15
|
+
const guard_1 = require("../guard/guard");
|
|
15
16
|
// ------------------------------------------------------------------
|
|
16
17
|
// Errors
|
|
17
18
|
// ------------------------------------------------------------------
|
|
@@ -26,7 +27,7 @@ exports.ValueCreateError = ValueCreateError;
|
|
|
26
27
|
// Default
|
|
27
28
|
// ------------------------------------------------------------------
|
|
28
29
|
function FromDefault(value) {
|
|
29
|
-
return
|
|
30
|
+
return (0, guard_1.IsFunction)(value) ? value() : (0, index_3.Clone)(value);
|
|
30
31
|
}
|
|
31
32
|
// ------------------------------------------------------------------
|
|
32
33
|
// Create
|
|
@@ -18,7 +18,8 @@ const kind_1 = require("../../type/guard/kind");
|
|
|
18
18
|
// ValueOrDefault
|
|
19
19
|
// ------------------------------------------------------------------
|
|
20
20
|
function ValueOrDefault(schema, value) {
|
|
21
|
-
const
|
|
21
|
+
const defaultValue = (0, index_5.HasPropertyKey)(schema, 'default') ? schema.default : undefined;
|
|
22
|
+
const clone = (0, index_5.IsFunction)(defaultValue) ? defaultValue() : (0, index_2.Clone)(defaultValue);
|
|
22
23
|
return (0, index_5.IsUndefined)(value) ? clone : (0, index_5.IsObject)(value) && (0, index_5.IsObject)(clone) ? Object.assign(clone, value) : value;
|
|
23
24
|
}
|
|
24
25
|
// ------------------------------------------------------------------
|
|
@@ -73,7 +73,7 @@ function FromIntersect(schema, references, path, value) {
|
|
|
73
73
|
knownProperties[knownKey] = Visit(knownSchema, references, `${path}/${knownKey}`, knownProperties[knownKey]);
|
|
74
74
|
}
|
|
75
75
|
if (!(0, type_1.IsTransform)(schema.unevaluatedProperties)) {
|
|
76
|
-
return
|
|
76
|
+
return knownProperties;
|
|
77
77
|
}
|
|
78
78
|
const unknownKeys = Object.getOwnPropertyNames(knownProperties);
|
|
79
79
|
const unevaluatedProperties = schema.unevaluatedProperties;
|
|
@@ -30,17 +30,17 @@ export declare function Convert(schema: TSchema, value: unknown): unknown;
|
|
|
30
30
|
/** Returns a structural clone of the given value */
|
|
31
31
|
export declare function Clone<T>(value: T): T;
|
|
32
32
|
/** Decodes a value or throws if error */
|
|
33
|
-
export declare function Decode<T extends TSchema,
|
|
33
|
+
export declare function Decode<T extends TSchema, Static = StaticDecode<T>, Result extends Static = Static>(schema: T, references: TSchema[], value: unknown): Result;
|
|
34
34
|
/** Decodes a value or throws if error */
|
|
35
|
-
export declare function Decode<T extends TSchema,
|
|
35
|
+
export declare function Decode<T extends TSchema, Static = StaticDecode<T>, Result extends Static = Static>(schema: T, value: unknown): Result;
|
|
36
36
|
/** `[Mutable]` Generates missing properties on a value using default schema annotations if available. This function does not check the value and returns an unknown type. You should Check the result before use. Default is a mutable operation. To avoid mutation, Clone the value first. */
|
|
37
37
|
export declare function Default(schema: TSchema, references: TSchema[], value: unknown): unknown;
|
|
38
38
|
/** `[Mutable]` Generates missing properties on a value using default schema annotations if available. This function does not check the value and returns an unknown type. You should Check the result before use. Default is a mutable operation. To avoid mutation, Clone the value first. */
|
|
39
39
|
export declare function Default(schema: TSchema, value: unknown): unknown;
|
|
40
40
|
/** Encodes a value or throws if error */
|
|
41
|
-
export declare function Encode<T extends TSchema,
|
|
41
|
+
export declare function Encode<T extends TSchema, Static = StaticEncode<T>, Result extends Static = Static>(schema: T, references: TSchema[], value: unknown): Result;
|
|
42
42
|
/** Encodes a value or throws if error */
|
|
43
|
-
export declare function Encode<T extends TSchema,
|
|
43
|
+
export declare function Encode<T extends TSchema, Static = StaticEncode<T>, Result extends Static = Static>(schema: T, value: unknown): Result;
|
|
44
44
|
/** Parses a value or throws an `AssertError` if invalid. */
|
|
45
45
|
export declare function Parse<T extends TSchema, R = StaticDecode<T>>(schema: T, references: TSchema[], value: unknown): R;
|
|
46
46
|
/** Parses a value or throws an `AssertError` if invalid. */
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ValueErrorIterator } from '../errors/index.mjs';
|
|
2
2
|
import { TypeBoxError } from '../type/error/index.mjs';
|
|
3
3
|
import type { TSchema } from '../type/schema/index.mjs';
|
|
4
|
-
import type { Static, StaticDecode
|
|
4
|
+
import type { Static, StaticDecode } from '../type/static/index.mjs';
|
|
5
5
|
export type CheckFunction = (value: unknown) => boolean;
|
|
6
6
|
export declare class TypeCheck<T extends TSchema> {
|
|
7
7
|
private readonly schema;
|
|
@@ -17,9 +17,9 @@ export declare class TypeCheck<T extends TSchema> {
|
|
|
17
17
|
/** Returns true if the value matches the compiled type. */
|
|
18
18
|
Check(value: unknown): value is Static<T>;
|
|
19
19
|
/** Decodes a value or throws if error */
|
|
20
|
-
Decode<
|
|
20
|
+
Decode<Static = StaticDecode<T>, Result extends Static = Static>(value: unknown): Result;
|
|
21
21
|
/** Encodes a value or throws if error */
|
|
22
|
-
Encode<
|
|
22
|
+
Encode<Static = StaticDecode<T>, Result extends Static = Static>(value: unknown): Result;
|
|
23
23
|
}
|
|
24
24
|
export declare class TypeCompilerUnknownTypeError extends TypeBoxError {
|
|
25
25
|
readonly schema: TSchema;
|
|
@@ -244,13 +244,16 @@ function* FromInteger(schema, references, path, value) {
|
|
|
244
244
|
}
|
|
245
245
|
}
|
|
246
246
|
function* FromIntersect(schema, references, path, value) {
|
|
247
|
+
let hasError = false;
|
|
247
248
|
for (const inner of schema.allOf) {
|
|
248
|
-
const
|
|
249
|
-
|
|
250
|
-
yield
|
|
251
|
-
yield next.value;
|
|
249
|
+
for (const error of Visit(inner, references, path, value)) {
|
|
250
|
+
hasError = true;
|
|
251
|
+
yield error;
|
|
252
252
|
}
|
|
253
253
|
}
|
|
254
|
+
if (hasError) {
|
|
255
|
+
return yield Create(ValueErrorType.Intersect, schema, path, value);
|
|
256
|
+
}
|
|
254
257
|
if (schema.unevaluatedProperties === false) {
|
|
255
258
|
const keyCheck = new RegExp(KeyOfPattern(schema));
|
|
256
259
|
for (const valueKey of Object.getOwnPropertyNames(value)) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { TupleToUnion, Evaluate } from '../helpers/index.mjs';
|
|
1
|
+
import type { SchemaOptions, TSchema } from '../schema/index.mjs';
|
|
2
|
+
import type { TupleToUnion, Evaluate, Ensure } from '../helpers/index.mjs';
|
|
3
3
|
import { type TRecursive } from '../recursive/index.mjs';
|
|
4
4
|
import type { TMappedKey, TMappedResult } from '../mapped/index.mjs';
|
|
5
5
|
import { type TIntersect } from '../intersect/index.mjs';
|
|
@@ -11,7 +11,8 @@ import { type TOmitFromMappedResult } from './omit-from-mapped-result.mjs';
|
|
|
11
11
|
type TFromIntersect<T extends TSchema[], K extends PropertyKey[], Acc extends TSchema[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromIntersect<R, K, [...Acc, TOmit<L, K>]> : Acc);
|
|
12
12
|
type TFromUnion<T extends TSchema[], K extends PropertyKey[], Acc extends TSchema[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromUnion<R, K, [...Acc, TOmit<L, K>]> : Acc);
|
|
13
13
|
type TFromProperties<T extends TProperties, K extends PropertyKey[], I extends PropertyKey = TupleToUnion<K>> = Evaluate<Omit<T, I>>;
|
|
14
|
-
|
|
14
|
+
type TFromObject<T extends TObject, K extends PropertyKey[], Properties extends TProperties = T['properties']> = Ensure<TObject<(TFromProperties<Properties, K>)>>;
|
|
15
|
+
export type TOmit<T extends TProperties, K extends PropertyKey[]> = (T extends TRecursive<infer S extends TSchema> ? TRecursive<TOmit<S, K>> : T extends TIntersect<infer S extends TSchema[]> ? TIntersect<TFromIntersect<S, K>> : T extends TUnion<infer S extends TSchema[]> ? TUnion<TFromUnion<S, K>> : T extends TObject<infer S extends TProperties> ? TFromObject<TObject<S>, K> : TObject<{}>);
|
|
15
16
|
/** `[Json]` Constructs a type whose keys are omitted from the given type */
|
|
16
17
|
export declare function Omit<T extends TMappedResult, K extends PropertyKey[]>(T: T, K: [...K], options?: SchemaOptions): TOmitFromMappedResult<T, K>;
|
|
17
18
|
/** `[Json]` Constructs a type whose keys are omitted from the given type */
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { CreateType } from '../create/type.mjs';
|
|
2
|
+
import { Discard } from '../discard/discard.mjs';
|
|
2
3
|
import { Intersect } from '../intersect/index.mjs';
|
|
3
4
|
import { Union } from '../union/index.mjs';
|
|
4
5
|
import { Object } from '../object/index.mjs';
|
|
5
6
|
import { IndexPropertyKeys } from '../indexed/index.mjs';
|
|
6
|
-
import { Discard } from '../discard/index.mjs';
|
|
7
|
-
import { TransformKind } from '../symbols/index.mjs';
|
|
8
7
|
import { OmitFromMappedKey } from './omit-from-mapped-key.mjs';
|
|
9
8
|
import { OmitFromMappedResult } from './omit-from-mapped-result.mjs';
|
|
9
|
+
import { TransformKind } from '../symbols/symbols.mjs';
|
|
10
10
|
// ------------------------------------------------------------------
|
|
11
11
|
// TypeGuard
|
|
12
12
|
// ------------------------------------------------------------------
|
|
@@ -31,6 +31,12 @@ function FromProperty(T, K) {
|
|
|
31
31
|
function FromProperties(T, K) {
|
|
32
32
|
return K.reduce((T, K2) => FromProperty(T, K2), T);
|
|
33
33
|
}
|
|
34
|
+
// prettier-ignore
|
|
35
|
+
function FromObject(T, K) {
|
|
36
|
+
const options = Discard(T, [TransformKind, '$id', 'required', 'properties']);
|
|
37
|
+
const properties = FromProperties(T['properties'], K);
|
|
38
|
+
return Object(properties, options);
|
|
39
|
+
}
|
|
34
40
|
// ------------------------------------------------------------------
|
|
35
41
|
// OmitResolve
|
|
36
42
|
// ------------------------------------------------------------------
|
|
@@ -38,7 +44,7 @@ function FromProperties(T, K) {
|
|
|
38
44
|
function OmitResolve(T, K) {
|
|
39
45
|
return (IsIntersect(T) ? Intersect(FromIntersect(T.allOf, K)) :
|
|
40
46
|
IsUnion(T) ? Union(FromUnion(T.anyOf, K)) :
|
|
41
|
-
IsObject(T) ?
|
|
47
|
+
IsObject(T) ? FromObject(T, K) :
|
|
42
48
|
Object({}));
|
|
43
49
|
}
|
|
44
50
|
export function Omit(T, K, options) {
|
|
@@ -49,5 +55,6 @@ export function Omit(T, K, options) {
|
|
|
49
55
|
return OmitFromMappedResult(T, K, options);
|
|
50
56
|
// non-mapped
|
|
51
57
|
const I = IsSchema(K) ? IndexPropertyKeys(K) : K;
|
|
52
|
-
|
|
58
|
+
// special: mapping types require overridable options
|
|
59
|
+
return CreateType({ ...OmitResolve(T, I), ...options });
|
|
53
60
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { TSchema, SchemaOptions } from '../schema/index.mjs';
|
|
2
|
-
import type { Evaluate } from '../helpers/index.mjs';
|
|
2
|
+
import type { Evaluate, Ensure } from '../helpers/index.mjs';
|
|
3
3
|
import type { TMappedResult } from '../mapped/index.mjs';
|
|
4
4
|
import { type TReadonlyOptional } from '../readonly-optional/index.mjs';
|
|
5
5
|
import { type TOptional } from '../optional/index.mjs';
|
|
@@ -13,7 +13,8 @@ type TFromRest<T extends TSchema[], Acc extends TSchema[] = []> = (T extends [in
|
|
|
13
13
|
type TFromProperties<T extends TProperties> = Evaluate<{
|
|
14
14
|
[K in keyof T]: T[K] extends (TReadonlyOptional<infer S>) ? TReadonlyOptional<S> : T[K] extends (TReadonly<infer S>) ? TReadonlyOptional<S> : T[K] extends (TOptional<infer S>) ? TOptional<S> : TOptional<T[K]>;
|
|
15
15
|
}>;
|
|
16
|
-
|
|
16
|
+
type TFromObject<T extends TObject, Properties extends TProperties = T['properties']> = Ensure<TObject<(TFromProperties<Properties>)>>;
|
|
17
|
+
export type TPartial<T extends TSchema> = (T extends TRecursive<infer S extends TSchema> ? TRecursive<TPartial<S>> : T extends TIntersect<infer S extends TSchema[]> ? TIntersect<TFromRest<S>> : T extends TUnion<infer S extends TSchema[]> ? TUnion<TFromRest<S>> : T extends TObject<infer S extends TProperties> ? TFromObject<TObject<S>> : TObject<{}>);
|
|
17
18
|
/** `[Json]` Constructs a type where all properties are optional */
|
|
18
19
|
export declare function Partial<T extends TMappedResult>(T: T, options?: SchemaOptions): TPartialFromMappedResult<T>;
|
|
19
20
|
/** `[Json]` Constructs a type where all properties are optional */
|
|
@@ -21,6 +21,12 @@ function FromProperties(T) {
|
|
|
21
21
|
Acc[K] = Optional(T[K]);
|
|
22
22
|
return Acc;
|
|
23
23
|
}
|
|
24
|
+
// prettier-ignore
|
|
25
|
+
function FromObject(T) {
|
|
26
|
+
const options = Discard(T, [TransformKind, '$id', 'required', 'properties']);
|
|
27
|
+
const properties = FromProperties(T['properties']);
|
|
28
|
+
return Object(properties, options);
|
|
29
|
+
}
|
|
24
30
|
// ------------------------------------------------------------------
|
|
25
31
|
// PartialResolve
|
|
26
32
|
// ------------------------------------------------------------------
|
|
@@ -28,14 +34,16 @@ function FromProperties(T) {
|
|
|
28
34
|
function PartialResolve(T) {
|
|
29
35
|
return (IsIntersect(T) ? Intersect(FromRest(T.allOf)) :
|
|
30
36
|
IsUnion(T) ? Union(FromRest(T.anyOf)) :
|
|
31
|
-
IsObject(T) ?
|
|
37
|
+
IsObject(T) ? FromObject(T) :
|
|
32
38
|
Object({}));
|
|
33
39
|
}
|
|
34
40
|
/** `[Json]` Constructs a type where all properties are optional */
|
|
35
41
|
export function Partial(T, options) {
|
|
36
|
-
if (IsMappedResult(T))
|
|
42
|
+
if (IsMappedResult(T)) {
|
|
37
43
|
return PartialFromMappedResult(T, options);
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
// special: mapping types require overridable options
|
|
47
|
+
return CreateType({ ...PartialResolve(T), ...options });
|
|
48
|
+
}
|
|
41
49
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { TSchema, SchemaOptions } from '../schema/index.mjs';
|
|
2
|
-
import type { TupleToUnion, Evaluate } from '../helpers/index.mjs';
|
|
2
|
+
import type { TupleToUnion, Evaluate, Ensure } from '../helpers/index.mjs';
|
|
3
3
|
import { type TRecursive } from '../recursive/index.mjs';
|
|
4
4
|
import { type TIntersect } from '../intersect/index.mjs';
|
|
5
5
|
import { type TUnion } from '../union/index.mjs';
|
|
@@ -12,9 +12,9 @@ type FromIntersect<T extends TSchema[], K extends PropertyKey[], Acc extends TSc
|
|
|
12
12
|
declare function FromIntersect<T extends TSchema[], K extends PropertyKey[]>(T: T, K: K): FromIntersect<T, K>;
|
|
13
13
|
type FromUnion<T extends TSchema[], K extends PropertyKey[], Acc extends TSchema[] = []> = T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? FromUnion<R, K, [...Acc, TPick<L, K>]> : Acc;
|
|
14
14
|
declare function FromUnion<T extends TSchema[], K extends PropertyKey[]>(T: T, K: K): FromUnion<T, K>;
|
|
15
|
-
type
|
|
16
|
-
|
|
17
|
-
export type TPick<T extends TProperties, K extends PropertyKey[]> = T extends TRecursive<infer S> ? TRecursive<TPick<S, K>> : T extends TIntersect<infer S> ? TIntersect<FromIntersect<S, K>> : T extends TUnion<infer S> ? TUnion<FromUnion<S, K>> : T extends TObject<infer S> ? TObject<
|
|
15
|
+
type TFromProperties<T extends TProperties, K extends PropertyKey[], I extends PropertyKey = TupleToUnion<K>> = Evaluate<Pick<T, I & keyof T>>;
|
|
16
|
+
type TFromObject<T extends TObject, K extends PropertyKey[], Properties extends TProperties = T['properties']> = Ensure<TObject<(TFromProperties<Properties, K>)>>;
|
|
17
|
+
export type TPick<T extends TProperties, K extends PropertyKey[]> = T extends TRecursive<infer S extends TSchema> ? TRecursive<TPick<S, K>> : T extends TIntersect<infer S extends TSchema[]> ? TIntersect<FromIntersect<S, K>> : T extends TUnion<infer S extends TSchema[]> ? TUnion<FromUnion<S, K>> : T extends TObject<infer S extends TProperties> ? TFromObject<TObject<S>, K> : TObject<{}>;
|
|
18
18
|
/** `[Json]` Constructs a type whose keys are picked from the given type */
|
|
19
19
|
export declare function Pick<T extends TMappedResult, K extends PropertyKey[]>(T: T, K: [...K], options?: SchemaOptions): TPickFromMappedResult<T, K>;
|
|
20
20
|
/** `[Json]` Constructs a type whose keys are picked from the given type */
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { CreateType } from '../create/type.mjs';
|
|
2
|
+
import { Discard } from '../discard/discard.mjs';
|
|
2
3
|
import { Intersect } from '../intersect/index.mjs';
|
|
3
4
|
import { Union } from '../union/index.mjs';
|
|
4
5
|
import { Object } from '../object/index.mjs';
|
|
5
6
|
import { IndexPropertyKeys } from '../indexed/index.mjs';
|
|
6
|
-
import { Discard } from '../discard/index.mjs';
|
|
7
|
-
import { TransformKind } from '../symbols/index.mjs';
|
|
8
7
|
import { PickFromMappedKey } from './pick-from-mapped-key.mjs';
|
|
9
8
|
import { PickFromMappedResult } from './pick-from-mapped-result.mjs';
|
|
9
|
+
import { TransformKind } from '../symbols/symbols.mjs';
|
|
10
10
|
// ------------------------------------------------------------------
|
|
11
11
|
// TypeGuard
|
|
12
12
|
// ------------------------------------------------------------------
|
|
@@ -26,6 +26,12 @@ function FromProperties(T, K) {
|
|
|
26
26
|
Acc[K2] = T[K2];
|
|
27
27
|
return Acc;
|
|
28
28
|
}
|
|
29
|
+
// prettier-ignore
|
|
30
|
+
function FromObject(T, K) {
|
|
31
|
+
const options = Discard(T, [TransformKind, '$id', 'required', 'properties']);
|
|
32
|
+
const properties = FromProperties(T['properties'], K);
|
|
33
|
+
return Object(properties, options);
|
|
34
|
+
}
|
|
29
35
|
// ------------------------------------------------------------------
|
|
30
36
|
// PickResolve
|
|
31
37
|
// ------------------------------------------------------------------
|
|
@@ -33,7 +39,7 @@ function FromProperties(T, K) {
|
|
|
33
39
|
function PickResolve(T, K) {
|
|
34
40
|
return (IsIntersect(T) ? Intersect(FromIntersect(T.allOf, K)) :
|
|
35
41
|
IsUnion(T) ? Union(FromUnion(T.anyOf, K)) :
|
|
36
|
-
IsObject(T) ?
|
|
42
|
+
IsObject(T) ? FromObject(T, K) :
|
|
37
43
|
Object({}));
|
|
38
44
|
}
|
|
39
45
|
export function Pick(T, K, options) {
|
|
@@ -44,5 +50,6 @@ export function Pick(T, K, options) {
|
|
|
44
50
|
return PickFromMappedResult(T, K, options);
|
|
45
51
|
// non-mapped
|
|
46
52
|
const I = IsSchema(K) ? IndexPropertyKeys(K) : K;
|
|
47
|
-
|
|
53
|
+
// special: mapping types require overridable options
|
|
54
|
+
return CreateType({ ...PickResolve(T, I), ...options });
|
|
48
55
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { TSchema, SchemaOptions } from '../schema/index.mjs';
|
|
2
|
-
import type { Evaluate } from '../helpers/index.mjs';
|
|
2
|
+
import type { Evaluate, Ensure } from '../helpers/index.mjs';
|
|
3
3
|
import type { TMappedResult } from '../mapped/index.mjs';
|
|
4
4
|
import { type TReadonlyOptional } from '../readonly-optional/index.mjs';
|
|
5
5
|
import { type TOptional } from '../optional/index.mjs';
|
|
@@ -13,7 +13,8 @@ type TFromRest<T extends TSchema[], Acc extends TSchema[] = []> = (T extends [in
|
|
|
13
13
|
type TFromProperties<T extends TProperties> = Evaluate<{
|
|
14
14
|
[K in keyof T]: T[K] extends (TReadonlyOptional<infer S>) ? TReadonly<S> : T[K] extends (TReadonly<infer S>) ? TReadonly<S> : T[K] extends (TOptional<infer S>) ? S : T[K];
|
|
15
15
|
}>;
|
|
16
|
-
|
|
16
|
+
type TFromObject<T extends TObject, Properties extends TProperties = T['properties']> = Ensure<TObject<(TFromProperties<Properties>)>>;
|
|
17
|
+
export type TRequired<T extends TSchema> = (T extends TRecursive<infer S extends TSchema> ? TRecursive<TRequired<S>> : T extends TIntersect<infer S extends TSchema[]> ? TIntersect<TFromRest<S>> : T extends TUnion<infer S extends TSchema[]> ? TUnion<TFromRest<S>> : T extends TObject<infer S extends TProperties> ? TFromObject<TObject<S>> : TObject<{}>);
|
|
17
18
|
/** `[Json]` Constructs a type where all properties are required */
|
|
18
19
|
export declare function Required<T extends TMappedResult>(T: T, options?: SchemaOptions): TRequiredFromMappedResult<T>;
|
|
19
20
|
/** `[Json]` Constructs a type where all properties are required */
|
|
@@ -20,6 +20,12 @@ function FromProperties(T) {
|
|
|
20
20
|
Acc[K] = Discard(T[K], [OptionalKind]);
|
|
21
21
|
return Acc;
|
|
22
22
|
}
|
|
23
|
+
// prettier-ignore
|
|
24
|
+
function FromObject(T) {
|
|
25
|
+
const options = Discard(T, [TransformKind, '$id', 'required', 'properties']);
|
|
26
|
+
const properties = FromProperties(T['properties']);
|
|
27
|
+
return Object(properties, options);
|
|
28
|
+
}
|
|
23
29
|
// ------------------------------------------------------------------
|
|
24
30
|
// RequiredResolve
|
|
25
31
|
// ------------------------------------------------------------------
|
|
@@ -27,7 +33,7 @@ function FromProperties(T) {
|
|
|
27
33
|
function RequiredResolve(T) {
|
|
28
34
|
return (IsIntersect(T) ? Intersect(FromRest(T.allOf)) :
|
|
29
35
|
IsUnion(T) ? Union(FromRest(T.anyOf)) :
|
|
30
|
-
IsObject(T) ?
|
|
36
|
+
IsObject(T) ? FromObject(T) :
|
|
31
37
|
Object({}));
|
|
32
38
|
}
|
|
33
39
|
/** `[Json]` Constructs a type where all properties are required */
|
|
@@ -36,8 +42,7 @@ export function Required(T, options) {
|
|
|
36
42
|
return RequiredFromMappedResult(T, options);
|
|
37
43
|
}
|
|
38
44
|
else {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
return CreateType({ ...D, ...R }, options);
|
|
45
|
+
// special: mapping types require overridable options
|
|
46
|
+
return CreateType({ ...RequiredResolve(T), ...options });
|
|
42
47
|
}
|
|
43
48
|
}
|
|
@@ -1,3 +1,12 @@
|
|
|
1
1
|
import type { TSchema } from '../schema/index.mjs';
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
export type TStrict<T extends TSchema> = T;
|
|
3
|
+
/**
|
|
4
|
+
* @deprecated `[Json]` Omits compositing symbols from this schema. It is recommended
|
|
5
|
+
* to use the JSON parse/stringify to remove compositing symbols if needed. This
|
|
6
|
+
* is how Strict works internally.
|
|
7
|
+
*
|
|
8
|
+
* ```typescript
|
|
9
|
+
* JSON.parse(JSON.stringify(Type.String()))
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export declare function Strict<T extends TSchema>(schema: T): TStrict<T>;
|
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* @deprecated `[Json]` Omits compositing symbols from this schema. It is recommended
|
|
3
|
+
* to use the JSON parse/stringify to remove compositing symbols if needed. This
|
|
4
|
+
* is how Strict works internally.
|
|
5
|
+
*
|
|
6
|
+
* ```typescript
|
|
7
|
+
* JSON.parse(JSON.stringify(Type.String()))
|
|
8
|
+
* ```
|
|
9
|
+
*/
|
|
2
10
|
export function Strict(schema) {
|
|
3
11
|
return JSON.parse(JSON.stringify(schema));
|
|
4
12
|
}
|
|
@@ -33,6 +33,7 @@ import { type TRef } from '../ref/index.mjs';
|
|
|
33
33
|
import { type TRequired, type TRequiredFromMappedResult } from '../required/index.mjs';
|
|
34
34
|
import { type TRest } from '../rest/index.mjs';
|
|
35
35
|
import { type TSchema, type SchemaOptions } from '../schema/index.mjs';
|
|
36
|
+
import { type TStrict } from '../strict/index.mjs';
|
|
36
37
|
import { type TString, type StringOptions } from '../string/index.mjs';
|
|
37
38
|
import { type TTemplateLiteral, type TTemplateLiteralKind, type TTemplateLiteralSyntax } from '../template-literal/index.mjs';
|
|
38
39
|
import { TransformDecodeBuilder } from '../transform/index.mjs';
|
|
@@ -42,8 +43,16 @@ import { type TUnknown } from '../unknown/index.mjs';
|
|
|
42
43
|
import { type TUnsafe, type UnsafeOptions } from '../unsafe/index.mjs';
|
|
43
44
|
/** Json Type Builder with Static Resolution for TypeScript */
|
|
44
45
|
export declare class JsonTypeBuilder {
|
|
45
|
-
/**
|
|
46
|
-
|
|
46
|
+
/**
|
|
47
|
+
* @deprecated `[Json]` Omits compositing symbols from this schema. It is recommended
|
|
48
|
+
* to use the JSON parse/stringify to remove compositing symbols if needed. This
|
|
49
|
+
* is how Strict works internally.
|
|
50
|
+
*
|
|
51
|
+
* ```typescript
|
|
52
|
+
* JSON.parse(JSON.stringify(Type.String()))
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
Strict<T extends TSchema>(schema: T): TStrict<T>;
|
|
47
56
|
/** `[Json]` Creates a Readonly and Optional property */
|
|
48
57
|
ReadonlyOptional<T extends TSchema>(schema: T): TReadonlyOptional<T>;
|
|
49
58
|
/** `[Json]` Creates a Readonly property */
|
|
@@ -133,19 +142,19 @@ export declare class JsonTypeBuilder {
|
|
|
133
142
|
/** `[Json]` Constructs a type whose keys are omitted from the given type */
|
|
134
143
|
Omit<T extends TMappedResult, K extends PropertyKey[]>(T: T, K: [...K], options?: SchemaOptions): TOmitFromMappedResult<T, K>;
|
|
135
144
|
/** `[Json]` Constructs a type whose keys are omitted from the given type */
|
|
136
|
-
Omit<T extends TSchema, K extends TMappedKey>(T: T, K: K): TOmitFromMappedKey<T, K>;
|
|
145
|
+
Omit<T extends TSchema, K extends TMappedKey>(T: T, K: K, options?: SchemaOptions): TOmitFromMappedKey<T, K>;
|
|
137
146
|
/** `[Json]` Constructs a type whose keys are omitted from the given type */
|
|
138
147
|
Omit<T extends TSchema, K extends TSchema, I extends PropertyKey[] = TIndexPropertyKeys<K>>(T: T, K: K, options?: SchemaOptions): TOmit<T, I>;
|
|
139
148
|
/** `[Json]` Constructs a type whose keys are omitted from the given type */
|
|
140
149
|
Omit<T extends TSchema, K extends PropertyKey[]>(T: T, K: readonly [...K], options?: SchemaOptions): TOmit<T, K>;
|
|
141
150
|
/** `[Json]` Constructs a type where all properties are optional */
|
|
142
|
-
Partial<T extends TMappedResult>(T: T, options?:
|
|
151
|
+
Partial<T extends TMappedResult>(T: T, options?: SchemaOptions): TPartialFromMappedResult<T>;
|
|
143
152
|
/** `[Json]` Constructs a type where all properties are optional */
|
|
144
|
-
Partial<T extends TSchema>(schema: T, options?:
|
|
153
|
+
Partial<T extends TSchema>(schema: T, options?: SchemaOptions): TPartial<T>;
|
|
145
154
|
/** `[Json]` Constructs a type whose keys are picked from the given type */
|
|
146
|
-
Pick<T extends TMappedResult, K extends PropertyKey[]>(T: T, K: [...K]): TPickFromMappedResult<T, K>;
|
|
155
|
+
Pick<T extends TMappedResult, K extends PropertyKey[]>(T: T, K: [...K], options?: SchemaOptions): TPickFromMappedResult<T, K>;
|
|
147
156
|
/** `[Json]` Constructs a type whose keys are picked from the given type */
|
|
148
|
-
Pick<T extends TSchema, K extends TMappedKey>(T: T, K: K): TPickFromMappedKey<T, K>;
|
|
157
|
+
Pick<T extends TSchema, K extends TMappedKey>(T: T, K: K, options?: SchemaOptions): TPickFromMappedKey<T, K>;
|
|
149
158
|
/** `[Json]` Constructs a type whose keys are picked from the given type */
|
|
150
159
|
Pick<T extends TSchema, K extends TSchema, I extends PropertyKey[] = TIndexPropertyKeys<K>>(T: T, K: K, options?: SchemaOptions): TPick<T, I>;
|
|
151
160
|
/** `[Json]` Constructs a type whose keys are picked from the given type */
|
|
@@ -159,9 +168,9 @@ export declare class JsonTypeBuilder {
|
|
|
159
168
|
/** `[Json]` Creates a Ref type. */
|
|
160
169
|
Ref<T extends TSchema>($ref: string, options?: SchemaOptions): TRef<T>;
|
|
161
170
|
/** `[Json]` Constructs a type where all properties are required */
|
|
162
|
-
Required<T extends TMappedResult>(T: T, options?:
|
|
171
|
+
Required<T extends TMappedResult>(T: T, options?: SchemaOptions): TRequiredFromMappedResult<T>;
|
|
163
172
|
/** `[Json]` Constructs a type where all properties are required */
|
|
164
|
-
Required<T extends TSchema>(schema: T, options?:
|
|
173
|
+
Required<T extends TSchema>(schema: T, options?: SchemaOptions): TRequired<T>;
|
|
165
174
|
/** `[Json]` Extracts interior Rest elements from Tuple, Intersect and Union types */
|
|
166
175
|
Rest<T extends TSchema>(schema: T): TRest<T>;
|
|
167
176
|
/** `[Json]` Creates a String type */
|
|
@@ -44,7 +44,15 @@ export class JsonTypeBuilder {
|
|
|
44
44
|
// ------------------------------------------------------------------------
|
|
45
45
|
// Strict
|
|
46
46
|
// ------------------------------------------------------------------------
|
|
47
|
-
/**
|
|
47
|
+
/**
|
|
48
|
+
* @deprecated `[Json]` Omits compositing symbols from this schema. It is recommended
|
|
49
|
+
* to use the JSON parse/stringify to remove compositing symbols if needed. This
|
|
50
|
+
* is how Strict works internally.
|
|
51
|
+
*
|
|
52
|
+
* ```typescript
|
|
53
|
+
* JSON.parse(JSON.stringify(Type.String()))
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
48
56
|
Strict(schema) {
|
|
49
57
|
return Strict(schema);
|
|
50
58
|
}
|
|
@@ -44,8 +44,8 @@ export { RegExp } from '../regexp/index.mjs';
|
|
|
44
44
|
export { Required } from '../required/index.mjs';
|
|
45
45
|
export { Rest } from '../rest/index.mjs';
|
|
46
46
|
export { ReturnType } from '../return-type/index.mjs';
|
|
47
|
-
export { Strict } from '../strict/index.mjs';
|
|
48
47
|
export { String } from '../string/index.mjs';
|
|
48
|
+
export { Strict } from '../strict/index.mjs';
|
|
49
49
|
export { Symbol } from '../symbol/index.mjs';
|
|
50
50
|
export { TemplateLiteral } from '../template-literal/index.mjs';
|
|
51
51
|
export { Transform } from '../transform/index.mjs';
|
|
@@ -47,8 +47,8 @@ export { RegExp } from '../regexp/index.mjs';
|
|
|
47
47
|
export { Required } from '../required/index.mjs';
|
|
48
48
|
export { Rest } from '../rest/index.mjs';
|
|
49
49
|
export { ReturnType } from '../return-type/index.mjs';
|
|
50
|
-
export { Strict } from '../strict/index.mjs';
|
|
51
50
|
export { String } from '../string/index.mjs';
|
|
51
|
+
export { Strict } from '../strict/index.mjs';
|
|
52
52
|
export { Symbol } from '../symbol/index.mjs';
|
|
53
53
|
export { TemplateLiteral } from '../template-literal/index.mjs';
|
|
54
54
|
export { Transform } from '../transform/index.mjs';
|
|
@@ -7,6 +7,7 @@ import { PatternStringExact, PatternNumberExact } from '../../type/patterns/inde
|
|
|
7
7
|
import { TypeRegistry } from '../../type/registry/index.mjs';
|
|
8
8
|
import { Kind } from '../../type/symbols/index.mjs';
|
|
9
9
|
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
10
|
+
import { IsFunction } from '../guard/guard.mjs';
|
|
10
11
|
// ------------------------------------------------------------------
|
|
11
12
|
// Errors
|
|
12
13
|
// ------------------------------------------------------------------
|
|
@@ -20,7 +21,7 @@ export class ValueCreateError extends TypeBoxError {
|
|
|
20
21
|
// Default
|
|
21
22
|
// ------------------------------------------------------------------
|
|
22
23
|
function FromDefault(value) {
|
|
23
|
-
return
|
|
24
|
+
return IsFunction(value) ? value() : Clone(value);
|
|
24
25
|
}
|
|
25
26
|
// ------------------------------------------------------------------
|
|
26
27
|
// Create
|
|
@@ -5,7 +5,7 @@ import { Kind } from '../../type/symbols/index.mjs';
|
|
|
5
5
|
// ------------------------------------------------------------------
|
|
6
6
|
// ValueGuard
|
|
7
7
|
// ------------------------------------------------------------------
|
|
8
|
-
import { IsString, IsObject, IsArray, IsUndefined, HasPropertyKey } from '../guard/index.mjs';
|
|
8
|
+
import { IsString, IsFunction, IsObject, IsArray, IsUndefined, HasPropertyKey } from '../guard/index.mjs';
|
|
9
9
|
// ------------------------------------------------------------------
|
|
10
10
|
// TypeGuard
|
|
11
11
|
// ------------------------------------------------------------------
|
|
@@ -14,7 +14,8 @@ import { IsKind } from '../../type/guard/kind.mjs';
|
|
|
14
14
|
// ValueOrDefault
|
|
15
15
|
// ------------------------------------------------------------------
|
|
16
16
|
function ValueOrDefault(schema, value) {
|
|
17
|
-
const
|
|
17
|
+
const defaultValue = HasPropertyKey(schema, 'default') ? schema.default : undefined;
|
|
18
|
+
const clone = IsFunction(defaultValue) ? defaultValue() : Clone(defaultValue);
|
|
18
19
|
return IsUndefined(value) ? clone : IsObject(value) && IsObject(clone) ? Object.assign(clone, value) : value;
|
|
19
20
|
}
|
|
20
21
|
// ------------------------------------------------------------------
|
|
@@ -66,7 +66,7 @@ function FromIntersect(schema, references, path, value) {
|
|
|
66
66
|
knownProperties[knownKey] = Visit(knownSchema, references, `${path}/${knownKey}`, knownProperties[knownKey]);
|
|
67
67
|
}
|
|
68
68
|
if (!IsTransform(schema.unevaluatedProperties)) {
|
|
69
|
-
return
|
|
69
|
+
return knownProperties;
|
|
70
70
|
}
|
|
71
71
|
const unknownKeys = Object.getOwnPropertyNames(knownProperties);
|
|
72
72
|
const unevaluatedProperties = schema.unevaluatedProperties;
|
|
@@ -30,17 +30,17 @@ export declare function Convert(schema: TSchema, value: unknown): unknown;
|
|
|
30
30
|
/** Returns a structural clone of the given value */
|
|
31
31
|
export declare function Clone<T>(value: T): T;
|
|
32
32
|
/** Decodes a value or throws if error */
|
|
33
|
-
export declare function Decode<T extends TSchema,
|
|
33
|
+
export declare function Decode<T extends TSchema, Static = StaticDecode<T>, Result extends Static = Static>(schema: T, references: TSchema[], value: unknown): Result;
|
|
34
34
|
/** Decodes a value or throws if error */
|
|
35
|
-
export declare function Decode<T extends TSchema,
|
|
35
|
+
export declare function Decode<T extends TSchema, Static = StaticDecode<T>, Result extends Static = Static>(schema: T, value: unknown): Result;
|
|
36
36
|
/** `[Mutable]` Generates missing properties on a value using default schema annotations if available. This function does not check the value and returns an unknown type. You should Check the result before use. Default is a mutable operation. To avoid mutation, Clone the value first. */
|
|
37
37
|
export declare function Default(schema: TSchema, references: TSchema[], value: unknown): unknown;
|
|
38
38
|
/** `[Mutable]` Generates missing properties on a value using default schema annotations if available. This function does not check the value and returns an unknown type. You should Check the result before use. Default is a mutable operation. To avoid mutation, Clone the value first. */
|
|
39
39
|
export declare function Default(schema: TSchema, value: unknown): unknown;
|
|
40
40
|
/** Encodes a value or throws if error */
|
|
41
|
-
export declare function Encode<T extends TSchema,
|
|
41
|
+
export declare function Encode<T extends TSchema, Static = StaticEncode<T>, Result extends Static = Static>(schema: T, references: TSchema[], value: unknown): Result;
|
|
42
42
|
/** Encodes a value or throws if error */
|
|
43
|
-
export declare function Encode<T extends TSchema,
|
|
43
|
+
export declare function Encode<T extends TSchema, Static = StaticEncode<T>, Result extends Static = Static>(schema: T, value: unknown): Result;
|
|
44
44
|
/** Parses a value or throws an `AssertError` if invalid. */
|
|
45
45
|
export declare function Parse<T extends TSchema, R = StaticDecode<T>>(schema: T, references: TSchema[], value: unknown): R;
|
|
46
46
|
/** Parses a value or throws an `AssertError` if invalid. */
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -77,7 +77,6 @@ License MIT
|
|
|
77
77
|
- [Transform](#types-transform)
|
|
78
78
|
- [Guard](#types-guard)
|
|
79
79
|
- [Unsafe](#types-unsafe)
|
|
80
|
-
- [Strict](#types-strict)
|
|
81
80
|
- [Values](#values)
|
|
82
81
|
- [Assert](#values-assert)
|
|
83
82
|
- [Create](#values-create)
|
|
@@ -1078,35 +1077,6 @@ if(TypeGuard.IsString(T)) {
|
|
|
1078
1077
|
}
|
|
1079
1078
|
```
|
|
1080
1079
|
|
|
1081
|
-
<a name='types-strict'></a>
|
|
1082
|
-
|
|
1083
|
-
### Strict
|
|
1084
|
-
|
|
1085
|
-
TypeBox types contain various symbol properties that are used for reflection, composition and compilation. These properties are not strictly valid Json Schema; so in some cases it may be desirable to omit them. TypeBox provides a `Strict` function that will omit these properties if necessary.
|
|
1086
|
-
|
|
1087
|
-
```typescript
|
|
1088
|
-
const T = Type.Object({ // const T = {
|
|
1089
|
-
name: Type.Optional(Type.String()) // [Symbol(TypeBox.Kind)]: 'Object',
|
|
1090
|
-
}) // type: 'object',
|
|
1091
|
-
// properties: {
|
|
1092
|
-
// name: {
|
|
1093
|
-
// type: 'string',
|
|
1094
|
-
// [Symbol(TypeBox.Kind)]: 'String',
|
|
1095
|
-
// [Symbol(TypeBox.Optional)]: 'Optional'
|
|
1096
|
-
// }
|
|
1097
|
-
// }
|
|
1098
|
-
// }
|
|
1099
|
-
|
|
1100
|
-
const U = Type.Strict(T) // const U = {
|
|
1101
|
-
// type: 'object',
|
|
1102
|
-
// properties: {
|
|
1103
|
-
// name: {
|
|
1104
|
-
// type: 'string'
|
|
1105
|
-
// }
|
|
1106
|
-
// }
|
|
1107
|
-
// }
|
|
1108
|
-
```
|
|
1109
|
-
|
|
1110
1080
|
<a name='values'></a>
|
|
1111
1081
|
|
|
1112
1082
|
## Values
|