@sinclair/typebox 0.33.5 → 0.33.7
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.
|
@@ -18,7 +18,8 @@ const kind_1 = require("../../type/guard/kind");
|
|
|
18
18
|
// ValueOrDefault
|
|
19
19
|
// ------------------------------------------------------------------
|
|
20
20
|
function ValueOrDefault(schema, value) {
|
|
21
|
-
|
|
21
|
+
const clone = (0, index_5.HasPropertyKey)(schema, 'default') ? (0, index_2.Clone)(schema.default) : undefined;
|
|
22
|
+
return (0, index_5.IsUndefined)(value) ? clone : (0, index_5.IsObject)(value) && (0, index_5.IsObject)(clone) ? Object.assign(clone, value) : value;
|
|
22
23
|
}
|
|
23
24
|
// ------------------------------------------------------------------
|
|
24
25
|
// HasDefaultProperty
|
|
@@ -47,24 +48,28 @@ function FromIntersect(schema, references, value) {
|
|
|
47
48
|
}
|
|
48
49
|
function FromObject(schema, references, value) {
|
|
49
50
|
const defaulted = ValueOrDefault(schema, value);
|
|
51
|
+
// return defaulted
|
|
50
52
|
if (!(0, index_5.IsObject)(defaulted))
|
|
51
53
|
return defaulted;
|
|
52
|
-
const additionalPropertiesSchema = schema.additionalProperties;
|
|
53
54
|
const knownPropertyKeys = Object.getOwnPropertyNames(schema.properties);
|
|
54
55
|
// properties
|
|
55
56
|
for (const key of knownPropertyKeys) {
|
|
56
|
-
if
|
|
57
|
+
// note: we need to traverse into the object and test if the return value
|
|
58
|
+
// yielded a non undefined result. Here we interpret an undefined result as
|
|
59
|
+
// a non assignable property and continue.
|
|
60
|
+
const propertyValue = Visit(schema.properties[key], references, defaulted[key]);
|
|
61
|
+
if ((0, index_5.IsUndefined)(propertyValue))
|
|
57
62
|
continue;
|
|
58
63
|
defaulted[key] = Visit(schema.properties[key], references, defaulted[key]);
|
|
59
64
|
}
|
|
60
65
|
// return if not additional properties
|
|
61
|
-
if (!HasDefaultProperty(
|
|
66
|
+
if (!HasDefaultProperty(schema.additionalProperties))
|
|
62
67
|
return defaulted;
|
|
63
68
|
// additional properties
|
|
64
69
|
for (const key of Object.getOwnPropertyNames(defaulted)) {
|
|
65
70
|
if (knownPropertyKeys.includes(key))
|
|
66
71
|
continue;
|
|
67
|
-
defaulted[key] = Visit(
|
|
72
|
+
defaulted[key] = Visit(schema.additionalProperties, references, defaulted[key]);
|
|
68
73
|
}
|
|
69
74
|
return defaulted;
|
|
70
75
|
}
|
|
@@ -5,7 +5,7 @@ import { Kind } from '../../type/symbols/index.mjs';
|
|
|
5
5
|
// ------------------------------------------------------------------
|
|
6
6
|
// ValueGuard
|
|
7
7
|
// ------------------------------------------------------------------
|
|
8
|
-
import { IsString, IsObject, IsArray, IsUndefined } from '../guard/index.mjs';
|
|
8
|
+
import { IsString, 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
|
-
|
|
17
|
+
const clone = HasPropertyKey(schema, 'default') ? Clone(schema.default) : undefined;
|
|
18
|
+
return IsUndefined(value) ? clone : IsObject(value) && IsObject(clone) ? Object.assign(clone, value) : value;
|
|
18
19
|
}
|
|
19
20
|
// ------------------------------------------------------------------
|
|
20
21
|
// HasDefaultProperty
|
|
@@ -43,24 +44,28 @@ function FromIntersect(schema, references, value) {
|
|
|
43
44
|
}
|
|
44
45
|
function FromObject(schema, references, value) {
|
|
45
46
|
const defaulted = ValueOrDefault(schema, value);
|
|
47
|
+
// return defaulted
|
|
46
48
|
if (!IsObject(defaulted))
|
|
47
49
|
return defaulted;
|
|
48
|
-
const additionalPropertiesSchema = schema.additionalProperties;
|
|
49
50
|
const knownPropertyKeys = Object.getOwnPropertyNames(schema.properties);
|
|
50
51
|
// properties
|
|
51
52
|
for (const key of knownPropertyKeys) {
|
|
52
|
-
if
|
|
53
|
+
// note: we need to traverse into the object and test if the return value
|
|
54
|
+
// yielded a non undefined result. Here we interpret an undefined result as
|
|
55
|
+
// a non assignable property and continue.
|
|
56
|
+
const propertyValue = Visit(schema.properties[key], references, defaulted[key]);
|
|
57
|
+
if (IsUndefined(propertyValue))
|
|
53
58
|
continue;
|
|
54
59
|
defaulted[key] = Visit(schema.properties[key], references, defaulted[key]);
|
|
55
60
|
}
|
|
56
61
|
// return if not additional properties
|
|
57
|
-
if (!HasDefaultProperty(
|
|
62
|
+
if (!HasDefaultProperty(schema.additionalProperties))
|
|
58
63
|
return defaulted;
|
|
59
64
|
// additional properties
|
|
60
65
|
for (const key of Object.getOwnPropertyNames(defaulted)) {
|
|
61
66
|
if (knownPropertyKeys.includes(key))
|
|
62
67
|
continue;
|
|
63
|
-
defaulted[key] = Visit(
|
|
68
|
+
defaulted[key] = Visit(schema.additionalProperties, references, defaulted[key]);
|
|
64
69
|
}
|
|
65
70
|
return defaulted;
|
|
66
71
|
}
|