-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypio.ts
138 lines (138 loc) · 3.43 KB
/
typio.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { InvalidTypeError } from "./InvalidTypeError";
import { TypioType } from "./TypioType";
import { isPlainObject } from "./util/isPlainObject";
export function typioValue<T extends any>(
obj: any,
model: T,
path = "",
): { status: "success"; value: T } | { status: "error"; error: any } {
if (typeof model === "function") {
return {
status: "success",
value: model(obj),
};
}
if (
(model as any)._____$TYPIO$ === true &&
(typeof obj !== "undefined" ||
((model as any) as TypioType<any>).name === "empty")
) {
const res = ((model as any) as TypioType<any>).cast(obj);
if (res.type === "error") {
return {
status: "error",
error: new InvalidTypeError({
path,
message: res.error,
operator: res.operator,
value: res.value,
}),
};
}
return { status: "success", value: res.value };
}
if (typeof model !== "undefined" && typeof obj === "undefined") {
return {
status: "error",
error: new InvalidTypeError({
path,
message:
"For model `" +
JSON.stringify(model) +
"` value should not be undefined",
value: obj,
}),
};
}
// if array
if (Array.isArray(model)) {
if (!Array.isArray(obj)) {
return {
status: "error",
error: new InvalidTypeError({
path,
message: "Value is not array, but model is array",
value: obj,
}),
};
}
const isTuple = model.length > 1;
const newValues: any[] = [];
for (const [index, child] of obj.entries()) {
const res = typioValue(
child,
model[isTuple ? index : 0],
`${path}[${index}]`,
);
if (res.status === "error") {
return res;
}
newValues.push(res.value);
}
return {
status: "success",
value: newValues as any,
};
}
if (isPlainObject(model)) {
if (!isPlainObject(obj)) {
return {
status: "error",
error: new InvalidTypeError({
path,
message: "Object is not plain-object, but model is plain-object",
value: obj,
}),
};
}
let objKeys = Object.keys(obj);
const res: any = {};
for (const fieldName of Object.keys(model)) {
const valRes = typioValue(
(obj as any)[fieldName],
(model as any)[fieldName],
`${path}.${fieldName}`,
);
if (valRes.status === "error") {
return valRes;
}
res[fieldName] = valRes.value;
objKeys = objKeys.filter((k) => k !== fieldName);
}
if (objKeys.length > 0) {
return {
status: "error",
error: new InvalidTypeError({
path,
message:
"Unknown keys " +
JSON.stringify(objKeys) +
" in object `" +
JSON.stringify(obj) +
"`",
value: obj,
}),
};
}
return { status: "success", value: res };
}
if (model !== obj) {
return {
status: "error",
error: new InvalidTypeError({
path,
message: "Model `" + model + "` not equal `" + obj + "`",
value: obj,
}),
};
}
return { status: "success", value: obj };
}
export function typio<T extends any>(obj: any, model: T, path = ""): T {
const res = typioValue(obj, model, path);
if (res.status === "error") {
throw res.error;
}
return res.value;
}
export default typio;