-
-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathpartialAsync.ts
More file actions
302 lines (295 loc) 路 8.32 KB
/
Copy pathpartialAsync.ts
File metadata and controls
302 lines (295 loc) 路 8.32 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
import {
type LooseObjectIssue,
type LooseObjectSchemaAsync,
type ObjectIssue,
type ObjectSchemaAsync,
type ObjectWithRestIssue,
type ObjectWithRestSchemaAsync,
optionalAsync,
type OptionalSchemaAsync,
type StrictObjectIssue,
type StrictObjectSchemaAsync,
} from '../../schemas/index.ts';
import type {
BaseIssue,
BaseSchema,
BaseSchemaAsync,
Config,
ErrorMessage,
InferInput,
InferIssue,
InferObjectInput,
InferObjectOutput,
InferOutput,
ObjectEntriesAsync,
ObjectKeys,
OutputDataset,
SchemaWithoutPipe,
StandardProps,
UnknownDataset,
} from '../../types/index.ts';
import { _standardSchema } from '../../utils/index.ts';
/**
* Schema type.
*/
type Schema = SchemaWithoutPipe<
| LooseObjectSchemaAsync<
ObjectEntriesAsync,
ErrorMessage<LooseObjectIssue> | undefined
>
| ObjectSchemaAsync<ObjectEntriesAsync, ErrorMessage<ObjectIssue> | undefined>
| ObjectWithRestSchemaAsync<
ObjectEntriesAsync,
| BaseSchema<unknown, unknown, BaseIssue<unknown>>
| BaseSchemaAsync<unknown, unknown, BaseIssue<unknown>>,
ErrorMessage<ObjectWithRestIssue> | undefined
>
| StrictObjectSchemaAsync<
ObjectEntriesAsync,
ErrorMessage<StrictObjectIssue> | undefined
>
>;
/**
* Partial entries type.
*/
type PartialEntries<
TEntries extends ObjectEntriesAsync,
TKeys extends readonly (keyof TEntries)[] | undefined,
> = {
[TKey in keyof TEntries]: TKeys extends readonly (keyof TEntries)[]
? TKey extends TKeys[number]
? OptionalSchemaAsync<TEntries[TKey], undefined>
: TEntries[TKey]
: OptionalSchemaAsync<TEntries[TKey], undefined>;
};
/**
* Schema with partial type.
*/
export type SchemaWithPartialAsync<
TSchema extends Schema,
TKeys extends ObjectKeys<TSchema> | undefined,
> = TSchema extends
| ObjectSchemaAsync<infer TEntries, ErrorMessage<ObjectIssue> | undefined>
| StrictObjectSchemaAsync<
infer TEntries,
ErrorMessage<StrictObjectIssue> | undefined
>
? Omit<TSchema, 'entries' | '~standard' | '~run' | '~types'> & {
/**
* The object entries.
*/
readonly entries: PartialEntries<TEntries, TKeys>;
/**
* The Standard Schema properties.
*
* @internal
*/
readonly '~standard': StandardProps<
InferObjectInput<PartialEntries<TEntries, TKeys>>,
InferObjectOutput<PartialEntries<TEntries, TKeys>>
>;
/**
* Parses unknown input.
*
* @param dataset The input dataset.
* @param config The configuration.
*
* @returns The output dataset.
*
* @internal
*/
readonly '~run': (
dataset: UnknownDataset,
config: Config<BaseIssue<unknown>>
) => Promise<
OutputDataset<
InferObjectOutput<PartialEntries<TEntries, TKeys>>,
InferIssue<TSchema>
>
>;
/**
* The input, output and issue type.
*
* @internal
*/
readonly '~types'?:
| {
readonly input: InferObjectInput<PartialEntries<TEntries, TKeys>>;
readonly output: InferObjectOutput<PartialEntries<TEntries, TKeys>>;
readonly issue: InferIssue<TSchema>;
}
| undefined;
}
: TSchema extends LooseObjectSchemaAsync<
infer TEntries,
ErrorMessage<LooseObjectIssue> | undefined
>
? Omit<TSchema, 'entries' | '~standard' | '~run' | '~types'> & {
/**
* The object entries.
*/
readonly entries: PartialEntries<TEntries, TKeys>;
/**
* The Standard Schema properties.
*
* @internal
*/
readonly '~standard': StandardProps<
InferObjectInput<PartialEntries<TEntries, TKeys>> & {
[key: string]: unknown;
},
InferObjectOutput<PartialEntries<TEntries, TKeys>> & {
[key: string]: unknown;
}
>;
/**
* Parses unknown input.
*
* @param dataset The input dataset.
* @param config The configuration.
*
* @returns The output dataset.
*
* @internal
*/
readonly '~run': (
dataset: UnknownDataset,
config: Config<BaseIssue<unknown>>
) => Promise<
OutputDataset<
InferObjectOutput<PartialEntries<TEntries, TKeys>> & {
[key: string]: unknown;
},
InferIssue<TSchema>
>
>;
/**
* The input, output and issue type.
*
* @internal
*/
readonly '~types'?:
| {
readonly input: InferObjectInput<
PartialEntries<TEntries, TKeys>
> & {
[key: string]: unknown;
};
readonly output: InferObjectOutput<
PartialEntries<TEntries, TKeys>
> & {
[key: string]: unknown;
};
readonly issue: InferIssue<TSchema>;
}
| undefined;
}
: TSchema extends ObjectWithRestSchemaAsync<
infer TEntries,
infer TRest,
ErrorMessage<ObjectWithRestIssue> | undefined
>
? Omit<TSchema, 'entries' | '~standard' | '~run' | '~types'> & {
/**
* The object entries.
*/
readonly entries: PartialEntries<TEntries, TKeys>;
/**
* The Standard Schema properties.
*
* @internal
*/
readonly '~standard': StandardProps<
InferObjectInput<PartialEntries<TEntries, TKeys>> & {
[key: string]: InferInput<TRest>;
},
InferObjectOutput<PartialEntries<TEntries, TKeys>> & {
[key: string]: InferOutput<TRest>;
}
>;
/**
* Parses unknown input.
*
* @param dataset The input dataset.
* @param config The configuration.
*
* @returns The output dataset.
*
* @internal
*/
readonly '~run': (
dataset: UnknownDataset,
config: Config<BaseIssue<unknown>>
) => Promise<
OutputDataset<
InferObjectOutput<PartialEntries<TEntries, TKeys>> & {
[key: string]: InferOutput<TRest>;
},
InferIssue<TSchema>
>
>;
/**
* The input, output and issue type.
*
* @internal
*/
readonly '~types'?:
| {
readonly input: InferObjectInput<
PartialEntries<TEntries, TKeys>
> & {
[key: string]: InferInput<TRest>;
};
readonly output: InferObjectOutput<
PartialEntries<TEntries, TKeys>
> & { [key: string]: InferOutput<TRest> };
readonly issue: InferIssue<TSchema>;
}
| undefined;
}
: never;
/**
* Creates a modified copy of an object schema that marks all entries as optional.
*
* @param schema The schema to modify.
*
* @returns An object schema.
*/
export function partialAsync<const TSchema extends Schema>(
schema: TSchema
): SchemaWithPartialAsync<TSchema, undefined>;
/**
* Creates a modified copy of an object schema that marks the selected entries
* as optional.
*
* @param schema The schema to modify.
* @param keys The selected entries.
*
* @returns An object schema.
*/
export function partialAsync<
const TSchema extends Schema,
const TKeys extends ObjectKeys<TSchema>,
>(schema: TSchema, keys: TKeys): SchemaWithPartialAsync<TSchema, TKeys>;
// @__NO_SIDE_EFFECTS__
export function partialAsync(
schema: Schema,
keys?: ObjectKeys<Schema>
): SchemaWithPartialAsync<Schema, ObjectKeys<Schema> | undefined> {
// Create modified object entries
const entries: PartialEntries<ObjectEntriesAsync, ObjectKeys<Schema>> = {};
for (const key in schema.entries) {
// @ts-expect-error
entries[key] =
!keys || keys.includes(key)
? optionalAsync(schema.entries[key])
: schema.entries[key];
}
// Return modified copy of schema
return _standardSchema<
SchemaWithPartialAsync<Schema, ObjectKeys<Schema> | undefined>
>({
...schema,
entries,
});
}