typebox 1.0.9 → 1.0.11
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/codec/from-object.mjs +5 -2
- package/build/value/convert/from-object.d.mts +1 -1
- package/build/value/convert/from-object.mjs +9 -4
- 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
|
@@ -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,2 +1,2 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type TObject, type TProperties } from '../../type/index.mjs';
|
|
2
2
|
export declare function FromObject(context: TProperties, type: TObject, value: unknown): unknown;
|
|
@@ -2,14 +2,19 @@
|
|
|
2
2
|
import { Guard } from '../../guard/index.mjs';
|
|
3
3
|
import { FromType } from './from-type.mjs';
|
|
4
4
|
import { FromAdditionalProperties } from './from-additional.mjs';
|
|
5
|
+
import { IsOptionalUndefined } from '../shared/index.mjs';
|
|
6
|
+
// ------------------------------------------------------------------
|
|
7
|
+
// FromProperties
|
|
8
|
+
// ------------------------------------------------------------------
|
|
5
9
|
function FromProperties(context, type, value) {
|
|
6
10
|
const entries = Guard.EntriesRegExp(type.properties);
|
|
7
11
|
const keys = Guard.Keys(value);
|
|
8
|
-
for (const [regexp,
|
|
12
|
+
for (const [regexp, property] of entries) {
|
|
9
13
|
for (const key of keys) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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]);
|
|
13
18
|
}
|
|
14
19
|
}
|
|
15
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
|
+
}
|