-
Notifications
You must be signed in to change notification settings - Fork 1
/
record.ts
145 lines (132 loc) · 3.58 KB
/
record.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
139
140
141
142
143
144
145
import { getOption, makeSchema, Schema } from "../schema";
import { string } from "../simple";
import {
isFailure,
isSuccess,
Validation,
ValidationIssue,
ValidationResult,
} from "../validation";
export function record<I, O, M>(
schema: Schema<I, O, M>,
issues?: Partial<{
required: string;
wrongType: string;
}>
) {
return keyedRecord(string(), schema, issues);
}
export function keyedRecord<K extends string | number | symbol, N, I, O, M>(
key: Schema<K, K, N>,
schema: Schema<I, O, M>,
issues?: Partial<{
required: string;
wrongType: string;
invalidKey: string;
}>
): Schema<
Record<K, I>,
Record<K, O>,
{ type: "record"; schema: { key: Schema<K, K, N>; value: Schema<I, O, M> } }
> {
type ResultI = Record<K, I>;
type ResultO = Record<K, O>;
type V = ValidationResult<ResultI>;
const preValidate = (v: unknown) => {
if (v === undefined || v === null) {
return new ValidationIssue("required", issues?.required, v) as V;
}
if (typeof v !== "object") {
return new ValidationIssue(
"wrong_type",
issues?.wrongType,
v,
"object"
) as V;
}
};
class Aggregator {
constructor(readonly earlyExit: boolean) {}
public valid = true;
public readonly validations: { [key: string]: unknown } = {};
onKeyValidation(
k: string,
value: unknown,
validation: ValidationResult<K>
): boolean {
if (isSuccess(validation)) {
return false;
}
this.valid = false;
this.validations[k] = new ValidationIssue(
"invalid_key",
issues?.invalidKey,
value,
validation
) as Validation<I>;
return this.earlyExit;
}
onValidation(k: string, validation: ValidationResult<I>) {
if (isSuccess(validation)) {
return false;
}
this.valid = false;
this.validations[k] = validation;
return this.earlyExit;
}
result(): V {
if (!this.valid) {
return this.validations as V;
}
}
}
return makeSchema(
(v, o) => {
const validation = preValidate(v);
if (isFailure(validation)) {
return validation;
}
const aggregator = new Aggregator(getOption(o, "earlyExit"));
const value = v as Record<string, unknown>;
for (const k in value) {
const keyValidation = key.validate(k, o);
if (aggregator.onKeyValidation(k, value[k], keyValidation)) {
break;
}
const validation = schema.validate(value[k], o);
if (aggregator.onValidation(k, validation)) {
break;
}
}
return aggregator.result();
},
async (v, o) => {
const validation = preValidate(v);
if (isFailure(validation)) {
return validation;
}
const aggregator = new Aggregator(getOption(o, "earlyExit"));
const value = v as Record<string, unknown>;
for (const k in value) {
const keyValidation = await key.validateAsync(k, o);
if (aggregator.onKeyValidation(k, value[k], keyValidation)) {
break;
}
const validation = await schema.validateAsync(value[k], o);
if (aggregator.onValidation(k, validation)) {
break;
}
}
return aggregator.result();
},
() => ({ type: "record", schema: { key, value: schema } }),
(v, o) => {
const result: Partial<ResultO> = {};
for (const k in v) {
const parsedKey = key.parse(k).parsedValue;
result[parsedKey as K] = schema.parse(v[k], o).parsedValue;
}
return result as ResultO;
}
);
}