/
object.ts
171 lines (152 loc) · 4.58 KB
/
object.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import {Schema, type Infer} from '../schema/schema.ts';
import {Context} from '../context/context.ts';
import {type ConfigValue} from '../types.ts';
import {DefaultMessageProps, messages} from '../messages.ts';
export type ObjectDesyValue = Record<string, Schema<any>>;
type PreparedTypes<TValue extends ObjectDesyValue> = {
[K in keyof TValue]: Infer<TValue[K]>;
};
type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
const strictName = 'object:strict';
const fieldsName = 'object:fields';
const testObject = (value: any, {path}: DefaultMessageProps) => {
if (typeof value !== 'object') {
return messages.object.object({path});
}
return '';
};
const createTestObjectStrict = (props: {
exclude: string[];
value: ObjectDesyValue;
}) => {
return (currentValue: Object, {path}: DefaultMessageProps) => {
const valueKeys = Object.keys(props.value);
const currentValueKeys = Object.keys(currentValue);
if (valueKeys.length - props.exclude.length !== currentValueKeys.length) {
return messages.object.unknown({path});
}
for (const key in currentValue) {
if (!(key in props.value) && !props.exclude.includes(key)) {
return messages.object.no_property({path: key});
}
}
return '';
};
};
export class ObjectDesy<
TValue extends ObjectDesyValue,
TValueTypes = PreparedTypes<TValue>,
> extends Schema<TValueTypes> {
static new<TValue extends ObjectDesyValue>(config: ConfigValue<TValue>) {
return new ObjectDesy(config);
}
value: TValue;
constructor(config: ConfigValue<TValue>) {
super(config);
this.value = config.value;
this.context.rules.push({name: 'object:object', test: testObject});
this.context.rules.push({
name: 'object:strict',
test: createTestObjectStrict({
exclude: [],
value: config.value,
}),
});
this.context.rules.push({
name: 'object:fields',
test: (currentValue, {path}) => {
for (const key in this.value) {
const schema = config.value[key];
const error = schema.validate(currentValue[key], {
path: path === '' ? key : `${path}.${key}`,
});
if (error !== '') {
return error;
}
}
return '';
},
});
}
notStrict() {
const strictIdx = this.context.rules.findIndex(
({name}) => name === strictName,
);
if (strictIdx !== undefined) {
this.context.rules.splice(strictIdx, 1);
}
const fieldsIdx = this.context.rules.findIndex(
({name}) => name === fieldsName,
);
if (fieldsIdx !== undefined) {
this.context.rules[fieldsIdx] = {
name: fieldsName,
test: (currentValue, {path}) => {
for (const key in this.value) {
const schema = this.value[key];
if (!(key in this.value)) {
continue;
}
const error = schema.validate(currentValue[key], {
path: path === '' ? key : `${path}.${key}`,
});
if (error !== '') {
return error;
}
}
return '';
},
};
}
return this as ObjectDesy<TValue, Partial<(typeof this)['types']>>;
}
optionalFields<TValueLocal extends keyof TValueTypes & string = any>(
optionalFields: TValueLocal[],
) {
const strictIdx = this.context.rules.findIndex(
({name}) => name === strictName,
);
if (strictIdx !== undefined) {
this.context.rules[strictIdx] = {
name: 'object:strict',
test: createTestObjectStrict({
exclude: optionalFields,
value: this.value,
}),
};
}
const fieldsIdx = this.context.rules.findIndex(
({name}) => name === fieldsName,
);
if (fieldsIdx !== undefined) {
this.context.rules[fieldsIdx] = {
name: fieldsName,
test: (currentValue, {path}) => {
for (const key in this.value) {
const schema = this.value[key];
if (!(key in currentValue) && optionalFields.includes(key as any)) {
continue;
}
const error = schema.validate(currentValue[key], {
path: path === '' ? key : `${path}.${key}`,
});
if (error !== '') {
return error;
}
}
return '';
},
};
}
return this as ObjectDesy<
TValue,
PartialBy<(typeof this)['types'], TValueLocal>
>;
}
}
export function object<TValue extends ObjectDesyValue>(value: TValue) {
return ObjectDesy.new({
value,
context: Context.new(),
});
}