typebox 1.1.17 → 1.1.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/value/assert/assert.d.mts +2 -2
- package/build/value/assert/assert.mjs +1 -0
- package/build/value/check/check.d.mts +2 -2
- package/build/value/codec/decode.d.mts +4 -14
- package/build/value/codec/decode.mjs +1 -6
- package/build/value/codec/encode.d.mts +4 -4
- package/build/value/codec/encode.mjs +1 -1
- package/build/value/codec/from-intersect.mjs +42 -12
- package/build/value/create/create.d.mts +2 -2
- package/build/value/parse/parse.d.mts +2 -2
- package/build/value/repair/repair.d.mts +2 -2
- package/package.json +1 -1
|
@@ -9,6 +9,6 @@ export declare class AssertError extends Error {
|
|
|
9
9
|
constructor(source: string, value: unknown, errors: TLocalizedValidationError[]);
|
|
10
10
|
}
|
|
11
11
|
/** Asserts the a value matches the given type. This function returns a TypeScript type asserts predicate and will throw AssertError if value does not match. */
|
|
12
|
-
export declare function Assert<
|
|
12
|
+
export declare function Assert<const Type extends TSchema>(type: Type, value: unknown): asserts value is Static<Type>;
|
|
13
13
|
/** Asserts the a value matches the given type. This function returns a TypeScript type asserts predicate and will throw AssertError if value does not match. */
|
|
14
|
-
export declare function Assert<
|
|
14
|
+
export declare function Assert<Context extends TProperties, const Type extends TSchema>(context: Context, type: Type, value: unknown): asserts value is Static<Type, Context>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Static, TProperties, TSchema } from '../../type/index.mjs';
|
|
2
2
|
/** Checks a value matches the provided type. */
|
|
3
|
-
export declare function Check<const Type extends TSchema
|
|
3
|
+
export declare function Check<const Type extends TSchema>(type: Type, value: unknown): value is Static<Type>;
|
|
4
4
|
/** Checks a value matches the provided type. */
|
|
5
|
-
export declare function Check<Context extends TProperties, const Type extends TSchema
|
|
5
|
+
export declare function Check<Context extends TProperties, const Type extends TSchema>(context: Context, type: Type, value: unknown): value is Static<Type, Context>;
|
|
@@ -6,17 +6,7 @@ export declare class DecodeError extends AssertError {
|
|
|
6
6
|
}
|
|
7
7
|
/** Executes Decode callbacks only */
|
|
8
8
|
export declare function DecodeUnsafe(context: TProperties, type: TSchema, value: unknown): unknown;
|
|
9
|
-
/**
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
* provided type, a DecodeError is thrown.
|
|
14
|
-
*/
|
|
15
|
-
export declare function Decode<const Type extends TSchema, Result extends unknown = StaticDecode<Type>>(type: Type, value: unknown): Result;
|
|
16
|
-
/**
|
|
17
|
-
* Decodes a value against the given type by applying a sequence of Clone,
|
|
18
|
-
* Default, Convert, and Clone operations, then executing any embedded Decode
|
|
19
|
-
* callbacks. If the processing sequence fails to produce a value matching the
|
|
20
|
-
* provided type, a DecodeError is thrown.
|
|
21
|
-
*/
|
|
22
|
-
export declare function Decode<Context extends TProperties, const Type extends TSchema, Result extends unknown = StaticDecode<Type, Context>>(context: Context, type: Type, value: unknown): Result;
|
|
9
|
+
/** Decodes a value with the given type. */
|
|
10
|
+
export declare function Decode<const Type extends TSchema>(type: Type, value: unknown): StaticDecode<Type>;
|
|
11
|
+
/** Decodes a value with the given type. */
|
|
12
|
+
export declare function Decode<Context extends TProperties, const Type extends TSchema>(context: Context, type: Type, value: unknown): StaticDecode<Type, Context>;
|
|
@@ -40,12 +40,7 @@ const Decoder = Pipeline([
|
|
|
40
40
|
(context, type, value) => Assert(context, type, value),
|
|
41
41
|
(context, type, value) => DecodeUnsafe(context, type, value)
|
|
42
42
|
]);
|
|
43
|
-
/**
|
|
44
|
-
* Decodes a value against the given type by applying a sequence of Clone,
|
|
45
|
-
* Default, Convert, and Clone operations, then executing any embedded Decode
|
|
46
|
-
* callbacks. If the processing sequence fails to produce a value matching the
|
|
47
|
-
* provided type, a DecodeError is thrown.
|
|
48
|
-
*/
|
|
43
|
+
/** Decodes a value with the given type. */
|
|
49
44
|
export function Decode(...args) {
|
|
50
45
|
const [context, type, value] = Arguments.Match(args, {
|
|
51
46
|
3: (context, type, value) => [context, type, value],
|
|
@@ -6,7 +6,7 @@ export declare class EncodeError extends AssertError {
|
|
|
6
6
|
}
|
|
7
7
|
/** Executes Encode callbacks only */
|
|
8
8
|
export declare function EncodeUnsafe(context: TProperties, type: TSchema, value: unknown): unknown;
|
|
9
|
-
/** Encodes a value. */
|
|
10
|
-
export declare function Encode<const Type extends TSchema
|
|
11
|
-
/** Encodes a value. */
|
|
12
|
-
export declare function Encode<Context extends TProperties, const Type extends TSchema
|
|
9
|
+
/** Encodes a value with the given type. */
|
|
10
|
+
export declare function Encode<const Type extends TSchema>(type: Type, value: unknown): StaticEncode<Type>;
|
|
11
|
+
/** Encodes a value with the given type. */
|
|
12
|
+
export declare function Encode<Context extends TProperties, const Type extends TSchema>(context: Context, type: Type, value: unknown): StaticEncode<Type, Context>;
|
|
@@ -40,7 +40,7 @@ const Encoder = Pipeline([
|
|
|
40
40
|
(context, type, value) => Clean(context, type, value),
|
|
41
41
|
(context, type, value) => Assert(context, type, value),
|
|
42
42
|
]);
|
|
43
|
-
/** Encodes a value. */
|
|
43
|
+
/** Encodes a value with the given type. */
|
|
44
44
|
export function Encode(...args) {
|
|
45
45
|
const [context, type, value] = Arguments.Match(args, {
|
|
46
46
|
3: (context, type, value) => [context, type, value],
|
|
@@ -2,27 +2,57 @@
|
|
|
2
2
|
import { Guard } from '../../guard/index.mjs';
|
|
3
3
|
import { FromType } from './from-type.mjs';
|
|
4
4
|
import { Callback } from './callback.mjs';
|
|
5
|
+
import { Clone } from '../clone/index.mjs';
|
|
6
|
+
import { Clean } from '../clean/index.mjs';
|
|
7
|
+
// ------------------------------------------------------------------
|
|
8
|
+
// MergeInteriors
|
|
9
|
+
//
|
|
10
|
+
// Merges all interior operand results into a single object. Each
|
|
11
|
+
// subsequent operand's properties override those of prior operands.
|
|
12
|
+
//
|
|
13
|
+
// ------------------------------------------------------------------
|
|
14
|
+
function MergeInteriors(interiors) {
|
|
15
|
+
return interiors.reduce((results, interior) => ({ ...results, ...interior }), {});
|
|
16
|
+
}
|
|
17
|
+
// ------------------------------------------------------------------
|
|
18
|
+
// NonMatchingInterior
|
|
19
|
+
//
|
|
20
|
+
// Used when Intersect operands do not all produce Objects. Returns
|
|
21
|
+
// the first interior result that differs from the original value,
|
|
22
|
+
// indicating a Codec has transformed the data. If no operand
|
|
23
|
+
// produced a change, defaults to the first interior result.
|
|
24
|
+
//
|
|
25
|
+
// ------------------------------------------------------------------
|
|
26
|
+
function NonMatchingInterior(value, interiors) {
|
|
27
|
+
for (const interior of interiors)
|
|
28
|
+
if (!Guard.IsDeepEqual(value, interior))
|
|
29
|
+
return interior;
|
|
30
|
+
return value; // value-unchanged
|
|
31
|
+
}
|
|
5
32
|
// ------------------------------------------------------------------
|
|
6
33
|
// Decode
|
|
7
34
|
// ------------------------------------------------------------------
|
|
8
35
|
function Decode(direction, context, type, value) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
36
|
+
if (Guard.IsEqual(type.allOf.length, 0))
|
|
37
|
+
return Callback(direction, context, type, value);
|
|
38
|
+
const interiors = type.allOf.map((schema) => FromType(direction, context, schema, Clean(schema, Clone(value))));
|
|
39
|
+
const structural = interiors.every((result) => Guard.IsObject(result));
|
|
40
|
+
const exterior = structural ? MergeInteriors(interiors) : NonMatchingInterior(value, interiors);
|
|
41
|
+
return Callback(direction, context, type, exterior);
|
|
13
42
|
}
|
|
14
43
|
// ------------------------------------------------------------------
|
|
15
44
|
// Encode
|
|
16
45
|
// ------------------------------------------------------------------
|
|
17
46
|
function Encode(direction, context, type, value) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
47
|
+
if (Guard.IsEqual(type.allOf.length, 0))
|
|
48
|
+
return Callback(direction, context, type, value);
|
|
49
|
+
const exterior = Callback(direction, context, type, value);
|
|
50
|
+
const interiors = type.allOf.map((schema) => FromType(direction, context, schema, Clean(schema, Clone(exterior))));
|
|
51
|
+
const structural = interiors.every((result) => Guard.IsObject(result));
|
|
52
|
+
if (structural)
|
|
53
|
+
return MergeInteriors(interiors);
|
|
54
|
+
return NonMatchingInterior(exterior, interiors);
|
|
23
55
|
}
|
|
24
56
|
export function FromIntersect(direction, context, type, value) {
|
|
25
|
-
return Guard.IsEqual(direction, 'Decode')
|
|
26
|
-
? Decode(direction, context, type, value)
|
|
27
|
-
: Encode(direction, context, type, value);
|
|
57
|
+
return Guard.IsEqual(direction, 'Decode') ? Decode(direction, context, type, value) : Encode(direction, context, type, value);
|
|
28
58
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { TProperties, TSchema, Static } from '../../type/index.mjs';
|
|
2
2
|
/** Creates a value from the provided type. This function will use `default` annotations if present. */
|
|
3
|
-
export declare function Create<const Type extends TSchema
|
|
3
|
+
export declare function Create<const Type extends TSchema>(type: Type): Static<Type>;
|
|
4
4
|
/** Creates a value from the provided type. This function will use `default` annotations if present. */
|
|
5
|
-
export declare function Create<const Context extends TProperties, Type extends TSchema
|
|
5
|
+
export declare function Create<const Context extends TProperties, Type extends TSchema>(context: Context, type: Type): Static<Type, Context>;
|
|
@@ -6,6 +6,6 @@ export declare class ParseError extends AssertError {
|
|
|
6
6
|
}
|
|
7
7
|
export declare const Parser: import("../pipeline/pipeline.mjs").PipelineInterface;
|
|
8
8
|
/** Parses a value with the given type. */
|
|
9
|
-
export declare function Parse<const Type extends TSchema
|
|
9
|
+
export declare function Parse<const Type extends TSchema>(type: Type, value: unknown): StaticParse<Type>;
|
|
10
10
|
/** Parses a value with the given type. */
|
|
11
|
-
export declare function Parse<Context extends TProperties, const Type extends TSchema
|
|
11
|
+
export declare function Parse<Context extends TProperties, const Type extends TSchema>(context: Context, type: Type, value: unknown): StaticParse<Type, Context>;
|
|
@@ -6,7 +6,7 @@ import type { TProperties, TSchema, Static } from '../../type/index.mjs';
|
|
|
6
6
|
* default structures derived from the type. If the value already conforms to the type, no
|
|
7
7
|
* action is performed.
|
|
8
8
|
*/
|
|
9
|
-
export declare function Repair<const Type extends TSchema
|
|
9
|
+
export declare function Repair<const Type extends TSchema>(type: Type, value: unknown): Static<Type>;
|
|
10
10
|
/**
|
|
11
11
|
* Repairs a value to match the provided type. This function is intended for data migration
|
|
12
12
|
* scenarios where existing values need to be migrating to an updated type. This function will
|
|
@@ -14,4 +14,4 @@ export declare function Repair<const Type extends TSchema, Result extends unknow
|
|
|
14
14
|
* default structures derived from the type. If the value already conforms to the type, no
|
|
15
15
|
* action is performed.
|
|
16
16
|
*/
|
|
17
|
-
export declare function Repair<Context extends TProperties, const Type extends TSchema
|
|
17
|
+
export declare function Repair<Context extends TProperties, const Type extends TSchema>(context: Context, type: Type, value: unknown): Static<Type, Context>;
|