typebox 1.0.10 → 1.0.12
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/schema/index.d.mts +3 -16
- package/build/schema/index.mjs +3 -13
- package/build/value/assert/assert.d.mts +1 -1
- package/build/value/clean/from-union.mjs +1 -1
- package/build/value/codec/decode.d.mts +2 -0
- package/build/value/codec/decode.mjs +8 -1
- package/build/value/codec/encode.d.mts +2 -0
- package/build/value/codec/encode.mjs +8 -1
- package/build/value/codec/from-object.mjs +5 -2
- package/build/value/convert/from-object.mjs +5 -18
- package/build/value/shared/index.d.mts +1 -0
- package/build/value/shared/index.mjs +2 -0
- package/build/value/shared/optional-undefined.d.mts +2 -0
- package/build/value/shared/optional-undefined.mjs +15 -0
- package/package.json +1 -1
package/build/schema/index.d.mts
CHANGED
|
@@ -1,16 +1,3 @@
|
|
|
1
|
-
export *
|
|
2
|
-
|
|
3
|
-
export
|
|
4
|
-
export * from './build.mjs';
|
|
5
|
-
export * from './check.mjs';
|
|
6
|
-
export * from './errors.mjs';
|
|
7
|
-
import { Build, BuildResult } from './build.mjs';
|
|
8
|
-
import { Check } from './check.mjs';
|
|
9
|
-
import { Errors } from './errors.mjs';
|
|
10
|
-
declare const _default: {
|
|
11
|
-
Build: typeof Build;
|
|
12
|
-
BuildResult: typeof BuildResult;
|
|
13
|
-
Check: typeof Check;
|
|
14
|
-
Errors: typeof Errors;
|
|
15
|
-
};
|
|
16
|
-
export default _default;
|
|
1
|
+
export * from './schema.mjs';
|
|
2
|
+
import * as Schema from './schema.mjs';
|
|
3
|
+
export default Schema;
|
package/build/schema/index.mjs
CHANGED
|
@@ -1,19 +1,9 @@
|
|
|
1
1
|
// ------------------------------------------------------------------
|
|
2
|
-
// Named
|
|
3
|
-
// ------------------------------------------------------------------
|
|
4
|
-
export * as Schema from './schema.mjs';
|
|
5
|
-
// ------------------------------------------------------------------
|
|
6
2
|
// Barrel
|
|
7
3
|
// ------------------------------------------------------------------
|
|
8
|
-
export * from './
|
|
9
|
-
export * from './types/index.mjs';
|
|
10
|
-
export * from './build.mjs';
|
|
11
|
-
export * from './check.mjs';
|
|
12
|
-
export * from './errors.mjs';
|
|
4
|
+
export * from './schema.mjs';
|
|
13
5
|
// ------------------------------------------------------------------
|
|
14
6
|
// Default
|
|
15
7
|
// ------------------------------------------------------------------
|
|
16
|
-
import
|
|
17
|
-
|
|
18
|
-
import { Errors } from './errors.mjs';
|
|
19
|
-
export default { Build, BuildResult, Check, Errors };
|
|
8
|
+
import * as Schema from './schema.mjs';
|
|
9
|
+
export default Schema;
|
|
@@ -5,4 +5,4 @@ export declare class AssertError extends Error {
|
|
|
5
5
|
/** 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. */
|
|
6
6
|
export declare function Assert<Context extends TProperties, const Type extends TSchema, Result extends unknown = Static<Type, Context>>(context: Context, type: Type, value: unknown): asserts value is Result;
|
|
7
7
|
/** 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. */
|
|
8
|
-
export declare function Assert<const Type extends TSchema, Result extends unknown = Static<Type>>(type:
|
|
8
|
+
export declare function Assert<const Type extends TSchema, Result extends unknown = Static<Type>>(type: Type, value: unknown): asserts value is Result;
|
|
@@ -5,7 +5,7 @@ import { FromType } from './from-type.mjs';
|
|
|
5
5
|
export function FromUnion(context, type, value) {
|
|
6
6
|
for (const schema of type.anyOf) {
|
|
7
7
|
const clean = FromType(context, schema, Clone(value));
|
|
8
|
-
if (Check(context,
|
|
8
|
+
if (Check(context, schema, clean))
|
|
9
9
|
return clean;
|
|
10
10
|
}
|
|
11
11
|
return value;
|
|
@@ -3,6 +3,8 @@ import { AssertError } from '../assert/index.mjs';
|
|
|
3
3
|
export declare class DecodeError extends AssertError {
|
|
4
4
|
constructor(value: unknown, errors: object[]);
|
|
5
5
|
}
|
|
6
|
+
/** Executes Decode callbacks only */
|
|
7
|
+
export declare function DecodeUnsafe(context: TProperties, type: TSchema, value: unknown): unknown;
|
|
6
8
|
/**
|
|
7
9
|
* Decodes a value against the given type by applying a sequence of Clone,
|
|
8
10
|
* Default, Convert, and Clone operations, then executing any embedded Decode
|
|
@@ -23,6 +23,13 @@ function Assert(context, type, value) {
|
|
|
23
23
|
return value;
|
|
24
24
|
}
|
|
25
25
|
// ------------------------------------------------------------------
|
|
26
|
+
// DecodeUnsafe
|
|
27
|
+
// ------------------------------------------------------------------
|
|
28
|
+
/** Executes Decode callbacks only */
|
|
29
|
+
export function DecodeUnsafe(context, type, value) {
|
|
30
|
+
return FromType('Decode', context, type, value);
|
|
31
|
+
}
|
|
32
|
+
// ------------------------------------------------------------------
|
|
26
33
|
// Decoder
|
|
27
34
|
// ------------------------------------------------------------------
|
|
28
35
|
const Decoder = Pipeline([
|
|
@@ -31,7 +38,7 @@ const Decoder = Pipeline([
|
|
|
31
38
|
(context, type, value) => Convert(context, type, value),
|
|
32
39
|
(context, type, value) => Clean(context, type, value),
|
|
33
40
|
(context, type, value) => Assert(context, type, value),
|
|
34
|
-
(context, type, value) =>
|
|
41
|
+
(context, type, value) => DecodeUnsafe(context, type, value)
|
|
35
42
|
]);
|
|
36
43
|
/**
|
|
37
44
|
* Decodes a value against the given type by applying a sequence of Clone,
|
|
@@ -3,6 +3,8 @@ import { AssertError } from '../assert/index.mjs';
|
|
|
3
3
|
export declare class EncodeError extends AssertError {
|
|
4
4
|
constructor(value: unknown, errors: object[]);
|
|
5
5
|
}
|
|
6
|
+
/** Executes Encode callbacks only */
|
|
7
|
+
export declare function EncodeUnsafe(context: TProperties, type: TSchema, value: unknown): unknown;
|
|
6
8
|
/** Encodes a value. */
|
|
7
9
|
export declare function Encode<const Type extends TSchema, Result extends unknown = StaticEncode<Type>>(type: Type, value: unknown): Result;
|
|
8
10
|
/** Encodes a value. */
|
|
@@ -23,11 +23,18 @@ function Assert(context, type, value) {
|
|
|
23
23
|
return value;
|
|
24
24
|
}
|
|
25
25
|
// ------------------------------------------------------------------
|
|
26
|
+
// EncodeUnsafe
|
|
27
|
+
// ------------------------------------------------------------------
|
|
28
|
+
/** Executes Encode callbacks only */
|
|
29
|
+
export function EncodeUnsafe(context, type, value) {
|
|
30
|
+
return FromType('Encode', context, type, value);
|
|
31
|
+
}
|
|
32
|
+
// ------------------------------------------------------------------
|
|
26
33
|
// Encoder
|
|
27
34
|
// ------------------------------------------------------------------
|
|
28
35
|
const Encoder = Pipeline([
|
|
29
36
|
(_context, _type, value) => Clone(value),
|
|
30
|
-
(context, type, value) =>
|
|
37
|
+
(context, type, value) => EncodeUnsafe(context, type, value),
|
|
31
38
|
(context, type, value) => Default(context, type, value),
|
|
32
39
|
(context, type, value) => Convert(context, type, value),
|
|
33
40
|
(context, type, value) => Clean(context, type, value),
|
|
@@ -3,6 +3,7 @@ import { Unreachable } from '../../system/unreachable/index.mjs';
|
|
|
3
3
|
import { Guard } from '../../guard/index.mjs';
|
|
4
4
|
import { FromType } from './from-type.mjs';
|
|
5
5
|
import { Callback } from './callback.mjs';
|
|
6
|
+
import { IsOptionalUndefined } from '../shared/index.mjs';
|
|
6
7
|
// ------------------------------------------------------------------
|
|
7
8
|
// Decode
|
|
8
9
|
// ------------------------------------------------------------------
|
|
@@ -12,7 +13,8 @@ function Decode(direction, context, type, value) {
|
|
|
12
13
|
return Unreachable();
|
|
13
14
|
// deno-coverage-ignore-stop
|
|
14
15
|
for (const key of Guard.Keys(type.properties)) {
|
|
15
|
-
|
|
16
|
+
// Ignore for non-present or optional-undefined
|
|
17
|
+
if (!Guard.HasPropertyKey(value, key) || IsOptionalUndefined(type.properties[key], key, value))
|
|
16
18
|
continue;
|
|
17
19
|
value[key] = FromType(direction, context, type.properties[key], value[key]);
|
|
18
20
|
}
|
|
@@ -26,7 +28,8 @@ function Encode(direction, context, type, value) {
|
|
|
26
28
|
if (!Guard.IsObjectNotArray(exterior))
|
|
27
29
|
return exterior;
|
|
28
30
|
for (const key of Guard.Keys(type.properties)) {
|
|
29
|
-
|
|
31
|
+
// Ignore for non-present or optional-undefined
|
|
32
|
+
if (!Guard.HasPropertyKey(exterior, key) || IsOptionalUndefined(type.properties[key], key, exterior))
|
|
30
33
|
continue;
|
|
31
34
|
exterior[key] = FromType(direction, context, type.properties[key], exterior[key]);
|
|
32
35
|
}
|
|
@@ -1,20 +1,8 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
-
import { IsOptional } from '../../type/index.mjs';
|
|
3
2
|
import { Guard } from '../../guard/index.mjs';
|
|
4
3
|
import { FromType } from './from-type.mjs';
|
|
5
4
|
import { FromAdditionalProperties } from './from-additional.mjs';
|
|
6
|
-
|
|
7
|
-
// IsOptionalUndefined
|
|
8
|
-
//
|
|
9
|
-
// Determines whether a property should be excluded from conversion
|
|
10
|
-
// because it is both optional and has an undefined value. This case
|
|
11
|
-
// cannot be safely converted, since it introduces ambiguity between
|
|
12
|
-
// an omitted optional property and a property explicitly set to the
|
|
13
|
-
// value undefined.
|
|
14
|
-
// ------------------------------------------------------------------
|
|
15
|
-
function IsOptionalUndefined(property, key, value) {
|
|
16
|
-
return IsOptional(property) && Guard.IsUndefined(value[key]);
|
|
17
|
-
}
|
|
5
|
+
import { IsOptionalUndefined } from '../shared/index.mjs';
|
|
18
6
|
// ------------------------------------------------------------------
|
|
19
7
|
// FromProperties
|
|
20
8
|
// ------------------------------------------------------------------
|
|
@@ -23,11 +11,10 @@ function FromProperties(context, type, value) {
|
|
|
23
11
|
const keys = Guard.Keys(value);
|
|
24
12
|
for (const [regexp, property] of entries) {
|
|
25
13
|
for (const key of keys) {
|
|
26
|
-
//
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
14
|
+
// Ignore for non-present or optional-undefined
|
|
15
|
+
if (!regexp.test(key) || IsOptionalUndefined(property, key, value))
|
|
16
|
+
continue;
|
|
17
|
+
value[key] = FromType(context, property, value[key]);
|
|
31
18
|
}
|
|
32
19
|
}
|
|
33
20
|
return (Guard.HasPropertyKey(type, 'additionalProperties') && Guard.IsObject(type.additionalProperties)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './optional-undefined.mjs';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// deno-fmt-ignore-file
|
|
2
|
+
import { Guard } from '../../guard/index.mjs';
|
|
3
|
+
import { IsOptional } from '../../type/index.mjs';
|
|
4
|
+
// ------------------------------------------------------------------
|
|
5
|
+
// IsOptionalUndefined
|
|
6
|
+
//
|
|
7
|
+
// Indicates whether a key should be excluded from processing when it is
|
|
8
|
+
// defined as optional in the schema and its corresponding value is undefined.
|
|
9
|
+
// This case cannot be reliably distinguished from an omitted key, and therefore
|
|
10
|
+
// introduces ambiguity between a key that is not provided and one that is
|
|
11
|
+
// explicitly assigned an undefined value.
|
|
12
|
+
// ------------------------------------------------------------------
|
|
13
|
+
export function IsOptionalUndefined(property, key, value) {
|
|
14
|
+
return IsOptional(property) && Guard.IsUndefined(value[key]);
|
|
15
|
+
}
|