-
-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathobjectWithRestAsync.ts
More file actions
340 lines (318 loc) 路 10.1 KB
/
Copy pathobjectWithRestAsync.ts
File metadata and controls
340 lines (318 loc) 路 10.1 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import { getDefault, getFallback } from '../../methods/index.ts';
import type {
BaseIssue,
BaseSchema,
BaseSchemaAsync,
ErrorMessage,
InferInput,
InferIssue,
InferObjectInput,
InferObjectIssue,
InferObjectOutput,
InferOutput,
ObjectEntriesAsync,
ObjectPathItem,
OutputDataset,
} from '../../types/index.ts';
import {
_addIssue,
_getStandardProps,
_hasOwnProperty,
_isValidObjectKey,
} from '../../utils/index.ts';
import type { objectWithRest } from './objectWithRest.ts';
import type { ObjectWithRestIssue } from './types.ts';
/**
* Object schema async interface.
*/
export interface ObjectWithRestSchemaAsync<
TEntries extends ObjectEntriesAsync,
TRest extends
| BaseSchema<unknown, unknown, BaseIssue<unknown>>
| BaseSchemaAsync<unknown, unknown, BaseIssue<unknown>>,
TMessage extends ErrorMessage<ObjectWithRestIssue> | undefined,
> extends BaseSchemaAsync<
InferObjectInput<TEntries> & { [key: string]: InferInput<TRest> },
InferObjectOutput<TEntries> & { [key: string]: InferOutput<TRest> },
ObjectWithRestIssue | InferObjectIssue<TEntries> | InferIssue<TRest>
> {
/**
* The schema type.
*/
readonly type: 'object_with_rest';
/**
* The schema reference.
*/
readonly reference: typeof objectWithRest | typeof objectWithRestAsync;
/**
* The expected property.
*/
readonly expects: 'Object';
/**
* The entries schema.
*/
readonly entries: TEntries;
/**
* The rest schema.
*/
readonly rest: TRest;
/**
* The error message.
*/
readonly message: TMessage;
}
/**
* Creates an object with rest schema.
*
* @param entries The entries schema.
* @param rest The rest schema.
*
* @returns An object with rest schema.
*/
export function objectWithRestAsync<
const TEntries extends ObjectEntriesAsync,
const TRest extends
| BaseSchema<unknown, unknown, BaseIssue<unknown>>
| BaseSchemaAsync<unknown, unknown, BaseIssue<unknown>>,
>(
entries: TEntries,
rest: TRest
): ObjectWithRestSchemaAsync<TEntries, TRest, undefined>;
/**
* Creates an object with rest schema.
*
* @param entries The entries schema.
* @param rest The rest schema.
* @param message The error message.
*
* @returns An object with rest schema.
*/
export function objectWithRestAsync<
const TEntries extends ObjectEntriesAsync,
const TRest extends
| BaseSchema<unknown, unknown, BaseIssue<unknown>>
| BaseSchemaAsync<unknown, unknown, BaseIssue<unknown>>,
const TMessage extends ErrorMessage<ObjectWithRestIssue> | undefined,
>(
entries: TEntries,
rest: TRest,
message: TMessage
): ObjectWithRestSchemaAsync<TEntries, TRest, TMessage>;
// @__NO_SIDE_EFFECTS__
export function objectWithRestAsync(
entries: ObjectEntriesAsync,
rest:
| BaseSchema<unknown, unknown, BaseIssue<unknown>>
| BaseSchemaAsync<unknown, unknown, BaseIssue<unknown>>,
message?: ErrorMessage<ObjectWithRestIssue>
): ObjectWithRestSchemaAsync<
ObjectEntriesAsync,
| BaseSchema<unknown, unknown, BaseIssue<unknown>>
| BaseSchemaAsync<unknown, unknown, BaseIssue<unknown>>,
ErrorMessage<ObjectWithRestIssue> | undefined
> {
return {
kind: 'schema',
type: 'object_with_rest',
reference: objectWithRestAsync,
expects: 'Object',
async: true,
entries,
rest,
message,
get '~standard'() {
return _getStandardProps(this);
},
async '~run'(dataset, config) {
// Get input value from dataset
const input = dataset.value;
// If root type is valid, check nested types
if (input && typeof input === 'object') {
// Set typed to `true` and value to blank object
// @ts-expect-error
dataset.typed = true;
dataset.value = {};
// Parse each normal and rest entry
const [normalDatasets, restDatasets] = await Promise.all([
// If key is present or its an optional schema with a default value,
// parse input of key or default value asynchronously
Promise.all(
Object.entries(this.entries).map(async ([key, valueSchema]) => {
if (
key in input ||
((valueSchema.type === 'exact_optional' ||
valueSchema.type === 'optional' ||
valueSchema.type === 'nullish') &&
// @ts-expect-error
valueSchema.default !== undefined)
) {
const value: unknown =
key in input
? // @ts-expect-error
input[key]
: await getDefault(valueSchema);
return [
key,
value,
valueSchema,
await valueSchema['~run']({ value }, config),
] as const;
}
return [
key,
// @ts-expect-error
input[key] as unknown,
valueSchema,
null,
] as const;
})
),
// Parse other entries with rest schema asynchronously
// Hint: We exclude specific keys for security reasons
Promise.all(
Object.entries(input)
.filter(
([key]) =>
_isValidObjectKey(input, key) && !_hasOwnProperty(this.entries, key)
)
.map(
async ([key, value]) =>
[
key,
value,
await this.rest['~run']({ value }, config),
] as const
)
),
]);
// Process each normal object entry of schema
for (const [key, value, valueSchema, valueDataset] of normalDatasets) {
// If key is present or its an optional schema with a default value,
// process its value dataset
if (valueDataset) {
// If there are issues, capture them
if (valueDataset.issues) {
// Create object path item
const pathItem: ObjectPathItem = {
type: 'object',
origin: 'value',
input: input as Record<string, unknown>,
key,
value,
};
// Add modified entry dataset issues to issues
for (const issue of valueDataset.issues) {
if (issue.path) {
issue.path.unshift(pathItem);
} else {
// @ts-expect-error
issue.path = [pathItem];
}
// @ts-expect-error
dataset.issues?.push(issue);
}
if (!dataset.issues) {
// @ts-expect-error
dataset.issues = valueDataset.issues;
}
// If necessary, abort early
if (config.abortEarly) {
dataset.typed = false;
break;
}
}
// If not typed, set typed to `false`
if (!valueDataset.typed) {
dataset.typed = false;
}
// Add entry to dataset
// @ts-expect-error
dataset.value[key] = valueDataset.value;
// Otherwise, if key is missing but has a fallback, use it
// @ts-expect-error
} else if (valueSchema.fallback !== undefined) {
// @ts-expect-error
dataset.value[key] = await getFallback(valueSchema);
// Otherwise, if key is missing and required, add issue
} else if (
valueSchema.type !== 'exact_optional' &&
valueSchema.type !== 'optional' &&
valueSchema.type !== 'nullish'
) {
_addIssue(this, 'key', dataset, config, {
input: undefined,
expected: `"${key}"`,
path: [
{
type: 'object',
origin: 'key',
input: input as Record<string, unknown>,
key,
value,
},
],
});
// If necessary, abort early
if (config.abortEarly) {
break;
}
}
}
// Process each rest entry of schema if necessary
if (!dataset.issues || !config.abortEarly) {
for (const [key, value, valueDataset] of restDatasets) {
// If there are issues, capture them
if (valueDataset.issues) {
// Create object path item
const pathItem: ObjectPathItem = {
type: 'object',
origin: 'value',
input: input as Record<string, unknown>,
key,
value,
};
// Add modified entry dataset issues to issues
for (const issue of valueDataset.issues) {
if (issue.path) {
issue.path.unshift(pathItem);
} else {
// @ts-expect-error
issue.path = [pathItem];
}
// @ts-expect-error
dataset.issues?.push(issue);
}
if (!dataset.issues) {
// @ts-expect-error
dataset.issues = valueDataset.issues;
}
// If necessary, abort early
if (config.abortEarly) {
dataset.typed = false;
break;
}
}
// If not typed, set typed to `false`
if (!valueDataset.typed) {
dataset.typed = false;
}
// Add entry to dataset
// @ts-expect-error
dataset.value[key] = valueDataset.value;
}
}
// Otherwise, add object issue
} else {
_addIssue(this, 'type', dataset, config);
}
// Return output dataset
// @ts-expect-error
return dataset as OutputDataset<
InferObjectOutput<ObjectEntriesAsync> & { [key: string]: unknown },
| ObjectWithRestIssue
| InferObjectIssue<ObjectEntriesAsync>
| BaseIssue<unknown>
>;
},
};
}