typebox 1.0.40 → 1.0.41
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.
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
2
|
import { Guard, GlobalsGuard } from '../../guard/index.mjs';
|
|
3
3
|
import * as T from '../../type/index.mjs';
|
|
4
|
+
import { Check } from '../check/index.mjs';
|
|
5
|
+
import { Create } from '../create/index.mjs';
|
|
4
6
|
import { FromArray } from './from-array.mjs';
|
|
5
7
|
import { FromBase } from './from-base.mjs';
|
|
6
8
|
import { FromEnum } from './from-enum.mjs';
|
|
@@ -42,16 +44,35 @@ function AssertRepairableType(context, type, value) {
|
|
|
42
44
|
}
|
|
43
45
|
}
|
|
44
46
|
// ------------------------------------------------------------------
|
|
47
|
+
// FinalizeRepair
|
|
48
|
+
//
|
|
49
|
+
// When a type includes the ~refine modifier, a post-repair validation
|
|
50
|
+
// check must be performed to ensure the repaired value satisfies the
|
|
51
|
+
// refine constraint. This logic is implemented as part of FromType to
|
|
52
|
+
// ensure the post-refine validation check is handled outside of
|
|
53
|
+
// sub-schema constraint checking (i.e., at the top level).
|
|
54
|
+
//
|
|
55
|
+
// ------------------------------------------------------------------
|
|
56
|
+
function FinalizeRepair(context, type, repaired) {
|
|
57
|
+
return T.IsRefine(type)
|
|
58
|
+
? Check(context, type, repaired)
|
|
59
|
+
? repaired
|
|
60
|
+
: Create(context, type)
|
|
61
|
+
: repaired;
|
|
62
|
+
}
|
|
63
|
+
// ------------------------------------------------------------------
|
|
45
64
|
// FromType
|
|
46
65
|
// ------------------------------------------------------------------
|
|
47
66
|
export function FromType(context, type, value) {
|
|
48
|
-
//
|
|
49
|
-
if (T.IsBase(type))
|
|
50
|
-
|
|
51
|
-
|
|
67
|
+
// Base Repair
|
|
68
|
+
if (T.IsBase(type)) {
|
|
69
|
+
const repaired = FromBase(context, type, value);
|
|
70
|
+
return FinalizeRepair(context, type, repaired);
|
|
71
|
+
}
|
|
72
|
+
// Schema Repair
|
|
52
73
|
AssertRepairableValue(context, type, value);
|
|
53
74
|
AssertRepairableType(context, type, value);
|
|
54
|
-
|
|
75
|
+
const repaired = (T.IsArray(type) ? FromArray(context, type, value) :
|
|
55
76
|
T.IsEnum(type) ? FromEnum(context, type, value) :
|
|
56
77
|
T.IsIntersect(type) ? FromIntersect(context, type, value) :
|
|
57
78
|
T.IsObject(type) ? FromObject(context, type, value) :
|
|
@@ -61,4 +82,5 @@ export function FromType(context, type, value) {
|
|
|
61
82
|
T.IsTuple(type) ? FromTuple(context, type, value) :
|
|
62
83
|
T.IsUnion(type) ? FromUnion(context, type, value) :
|
|
63
84
|
FromUnknown(context, type, value));
|
|
85
|
+
return FinalizeRepair(context, type, repaired);
|
|
64
86
|
}
|
|
@@ -2,8 +2,6 @@
|
|
|
2
2
|
import { Arguments } from '../../system/arguments/index.mjs';
|
|
3
3
|
import { FromType } from './from-type.mjs';
|
|
4
4
|
import { Assert } from '../assert/index.mjs';
|
|
5
|
-
import { Check } from '../check/index.mjs';
|
|
6
|
-
import { Create } from '../create/index.mjs';
|
|
7
5
|
/**
|
|
8
6
|
* Repairs a value to match the provided type. This function is intended for data migration
|
|
9
7
|
* scenarios where existing values need to be migrating to an updated type. This function will
|
|
@@ -17,7 +15,6 @@ export function Repair(...args) {
|
|
|
17
15
|
2: (type, value) => [{}, type, value],
|
|
18
16
|
});
|
|
19
17
|
const repaired = FromType(context, type, value);
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return checked;
|
|
18
|
+
Assert(context, type, repaired);
|
|
19
|
+
return repaired;
|
|
23
20
|
}
|