-
Notifications
You must be signed in to change notification settings - Fork 935
/
Copy pathobject.ts
528 lines (441 loc) · 14.5 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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
// @ts-ignore
import { getter, normalizePath, join } from 'property-expr';
import { camelCase, snakeCase } from 'tiny-case';
import { Flags, Maybe, SetFlag, ToggleDefault, UnsetFlag } from './util/types';
import { object as locale } from './locale';
import sortFields from './util/sortFields';
import sortByKeyOrder from './util/sortByKeyOrder';
import { DefaultThunk, InternalOptions, ISchema, Message } from './types';
import type { Defined, NotNull, _ } from './util/types';
import Reference from './Reference';
import Schema, { SchemaObjectDescription, SchemaSpec } from './schema';
import { ResolveOptions } from './Condition';
import type {
AnyObject,
ConcatObjectTypes,
DefaultFromShape,
MakePartial,
MergeObjectTypes,
ObjectShape,
PartialDeep,
TypeFromShape,
} from './util/objectTypes';
import parseJson from './util/parseJson';
import type { Test } from './util/createValidation';
import type ValidationError from './ValidationError';
export type { AnyObject };
type MakeKeysOptional<T> = T extends AnyObject ? _<MakePartial<T>> : T;
export type Shape<T extends Maybe<AnyObject>, C = any> = {
[field in keyof T]-?: ISchema<T[field], C> | Reference;
};
export type ObjectSchemaSpec = SchemaSpec<any> & {
noUnknown?: boolean;
};
function deepPartial(schema: any) {
if ('fields' in schema) {
const partial: any = {};
for (const [key, fieldSchema] of Object.entries(schema.fields)) {
partial[key] = deepPartial(fieldSchema);
}
return schema.setFields(partial);
}
if (schema.type === 'array') {
const nextArray = schema.optional();
if (nextArray.innerType)
nextArray.innerType = deepPartial(nextArray.innerType);
return nextArray;
}
if (schema.type === 'tuple') {
return schema
.optional()
.clone({ types: schema.spec.types.map(deepPartial) });
}
if ('optional' in schema) {
return schema.optional();
}
return schema;
}
const deepHas = (obj: any, p: string) => {
const path = [...normalizePath(p)];
if (path.length === 1) return path[0] in obj;
let last = path.pop()!;
let parent = getter(join(path), true)(obj);
return !!(parent && last in parent);
};
let isObject = (obj: any): obj is Record<PropertyKey, unknown> =>
Object.prototype.toString.call(obj) === '[object Object]';
function unknown(ctx: ObjectSchema<any, any, any>, value: any) {
let known = Object.keys(ctx.fields);
return Object.keys(value).filter((key) => known.indexOf(key) === -1);
}
const defaultSort = sortByKeyOrder([]);
export function create<
C extends Maybe<AnyObject> = AnyObject,
S extends ObjectShape = {},
>(spec?: S) {
type TIn = _<TypeFromShape<S, C>>;
type TDefault = _<DefaultFromShape<S>>;
return new ObjectSchema<TIn, C, TDefault>(spec as any);
}
export default interface ObjectSchema<
TIn extends Maybe<AnyObject>,
TContext = AnyObject,
// important that this is `any` so that using `ObjectSchema<MyType>`'s default
// will match object schema regardless of defaults
TDefault = any,
TFlags extends Flags = '',
> extends Schema<MakeKeysOptional<TIn>, TContext, TDefault, TFlags> {
default<D extends Maybe<AnyObject>>(
def: DefaultThunk<D, TContext>,
): ObjectSchema<TIn, TContext, D, ToggleDefault<TFlags, 'd'>>;
defined(
msg?: Message,
): ObjectSchema<Defined<TIn>, TContext, TDefault, TFlags>;
optional(): ObjectSchema<TIn | undefined, TContext, TDefault, TFlags>;
required(
msg?: Message,
): ObjectSchema<NonNullable<TIn>, TContext, TDefault, TFlags>;
notRequired(): ObjectSchema<Maybe<TIn>, TContext, TDefault, TFlags>;
nullable(msg?: Message): ObjectSchema<TIn | null, TContext, TDefault, TFlags>;
nonNullable(): ObjectSchema<NotNull<TIn>, TContext, TDefault, TFlags>;
strip(
enabled: false,
): ObjectSchema<TIn, TContext, TDefault, UnsetFlag<TFlags, 's'>>;
strip(
enabled?: true,
): ObjectSchema<TIn, TContext, TDefault, SetFlag<TFlags, 's'>>;
}
export default class ObjectSchema<
TIn extends Maybe<AnyObject>,
TContext = AnyObject,
TDefault = any,
TFlags extends Flags = '',
> extends Schema<MakeKeysOptional<TIn>, TContext, TDefault, TFlags> {
fields: Shape<NonNullable<TIn>, TContext> = Object.create(null);
declare spec: ObjectSchemaSpec;
private _sortErrors = defaultSort;
private _nodes: string[] = [];
private _excludedEdges: readonly [nodeA: string, nodeB: string][] = [];
constructor(spec?: Shape<TIn, TContext>) {
super({
type: 'object',
check(value): value is NonNullable<MakeKeysOptional<TIn>> {
return isObject(value) || typeof value === 'function';
},
});
this.withMutation(() => {
if (spec) {
this.shape(spec as any);
}
});
}
protected _cast(_value: any, options: InternalOptions<TContext> = {}) {
let value = super._cast(_value, options);
//should ignore nulls here
if (value === undefined) return this.getDefault(options);
if (!this._typeCheck(value)) return value;
let fields = this.fields;
let strip = options.stripUnknown ?? this.spec.noUnknown;
let props = ([] as string[]).concat(
this._nodes,
Object.keys(value).filter((v) => !this._nodes.includes(v)),
);
let intermediateValue: Record<string, unknown> = {}; // is filled during the transform below
let innerOptions: InternalOptions<TContext> = {
...options,
parent: intermediateValue,
__validating: options.__validating || false,
};
let isChanged = false;
for (const prop of props) {
let field = fields[prop];
let exists = prop in (value as {})!;
if (field) {
let fieldValue;
let inputValue = value[prop];
// safe to mutate since this is fired in sequence
innerOptions.path = (options.path ? `${options.path}.` : '') + prop;
field = field.resolve({
value: inputValue,
context: options.context,
parent: intermediateValue,
});
let fieldSpec = field instanceof Schema ? field.spec : undefined;
let strict = fieldSpec?.strict;
if (fieldSpec?.strip) {
isChanged = isChanged || prop in (value as {});
continue;
}
fieldValue =
!options.__validating || !strict
? // TODO: use _cast, this is double resolving
(field as ISchema<any>).cast(value[prop], innerOptions)
: value[prop];
if (fieldValue !== undefined) {
intermediateValue[prop] = fieldValue;
}
} else if (exists && !strip) {
intermediateValue[prop] = value[prop];
}
if (
exists !== prop in intermediateValue ||
intermediateValue[prop] !== value[prop]
) {
isChanged = true;
}
}
return isChanged ? intermediateValue : value;
}
protected _validate(
_value: any,
options: InternalOptions<TContext> = {},
panic: (err: Error, value: unknown) => void,
next: (err: ValidationError[], value: unknown) => void,
) {
let {
from = [],
originalValue = _value,
recursive = this.spec.recursive,
} = options;
options.from = [{ schema: this, value: originalValue }, ...from];
// this flag is needed for handling `strict` correctly in the context of
// validation vs just casting. e.g strict() on a field is only used when validating
options.__validating = true;
options.originalValue = originalValue;
super._validate(_value, options, panic, (objectErrors, value) => {
if (!recursive || !isObject(value)) {
next(objectErrors, value);
return;
}
originalValue = originalValue || value;
let tests = [] as Test[];
for (let key of this._nodes) {
let field = this.fields[key];
if (!field || Reference.isRef(field)) {
continue;
}
tests.push(
field.asNestedTest({
options,
key,
parent: value,
parentPath: options.path,
originalParent: originalValue,
}),
);
}
this.runTests(
{ tests, value, originalValue, options },
panic,
(fieldErrors) => {
next(fieldErrors.sort(this._sortErrors).concat(objectErrors), value);
},
);
});
}
clone(spec?: ObjectSchemaSpec): this {
const next = super.clone(spec);
next.fields = { ...this.fields };
next._nodes = this._nodes;
next._excludedEdges = this._excludedEdges;
next._sortErrors = this._sortErrors;
return next;
}
concat<IIn extends Maybe<AnyObject>, IC, ID, IF extends Flags>(
schema: ObjectSchema<IIn, IC, ID, IF>,
): ObjectSchema<
ConcatObjectTypes<TIn, IIn>,
TContext & IC,
Extract<IF, 'd'> extends never
? // this _attempts_ to cover the default from shape case
TDefault extends AnyObject
? ID extends AnyObject
? _<ConcatObjectTypes<TDefault, ID>>
: ID
: ID
: ID,
TFlags | IF
>;
concat(schema: this): this;
concat(schema: any): any {
let next = super.concat(schema) as any;
let nextFields = next.fields;
for (let [field, schemaOrRef] of Object.entries(this.fields)) {
const target = nextFields[field];
nextFields[field] = target === undefined ? schemaOrRef : target;
}
return next.withMutation((s: any) =>
// XXX: excludes here is wrong
s.setFields(nextFields, [
...this._excludedEdges,
...schema._excludedEdges,
]),
);
}
protected _getDefault(options?: ResolveOptions<TContext>) {
if ('default' in this.spec) {
return super._getDefault(options);
}
// if there is no default set invent one
if (!this._nodes.length) {
return undefined;
}
let dft: any = {};
this._nodes.forEach((key) => {
const field = this.fields[key] as any;
let innerOptions = options;
if (innerOptions?.value) {
innerOptions = {
...innerOptions,
parent: innerOptions.value,
value: innerOptions.value[key],
};
}
dft[key] =
field && 'getDefault' in field
? field.getDefault(innerOptions)
: undefined;
});
return dft;
}
private setFields<TInNext extends Maybe<AnyObject>, TDefaultNext>(
shape: Shape<TInNext, TContext>,
excludedEdges?: readonly [string, string][],
): ObjectSchema<TInNext, TContext, TDefaultNext, TFlags> {
let next = this.clone() as any;
next.fields = shape;
next._nodes = sortFields(shape, excludedEdges);
next._sortErrors = sortByKeyOrder(Object.keys(shape));
// XXX: this carries over edges which may not be what you want
if (excludedEdges) next._excludedEdges = excludedEdges;
return next;
}
shape<U extends ObjectShape>(
additions: U,
excludes: readonly [string, string][] = [],
) {
type UIn = TypeFromShape<U, TContext>;
type UDefault = Extract<TFlags, 'd'> extends never
? // not defaulted then assume the default is derived and should be merged
_<TDefault & DefaultFromShape<U>>
: TDefault;
return this.clone().withMutation((next) => {
let edges = next._excludedEdges;
if (excludes.length) {
if (!Array.isArray(excludes[0])) excludes = [excludes as any];
edges = [...next._excludedEdges, ...excludes];
}
// XXX: excludes here is wrong
return next.setFields<_<MergeObjectTypes<TIn, UIn>>, UDefault>(
Object.assign(next.fields, additions) as any,
edges,
);
});
}
partial() {
const partial: any = {};
for (const [key, schema] of Object.entries(this.fields)) {
partial[key] =
'optional' in schema && schema.optional instanceof Function
? schema.optional()
: schema;
}
return this.setFields<Partial<TIn>, TDefault>(partial);
}
deepPartial(): ObjectSchema<PartialDeep<TIn>, TContext, TDefault, TFlags> {
const next = deepPartial(this);
return next;
}
pick<TKey extends keyof TIn>(keys: readonly TKey[]) {
const picked: any = {};
for (const key of keys) {
if (this.fields[key]) picked[key] = this.fields[key];
}
return this.setFields<{ [K in TKey]: TIn[K] }, TDefault>(picked);
}
omit<TKey extends keyof TIn>(keys: readonly TKey[]) {
const fields = { ...this.fields };
for (const key of keys) {
delete fields[key];
}
return this.setFields<Omit<TIn, TKey>, TDefault>(fields);
}
from(from: string, to: keyof TIn, alias?: boolean) {
let fromGetter = getter(from, true);
return this.transform((obj) => {
if (!obj) return obj;
let newObj = obj;
if (deepHas(obj, from)) {
newObj = { ...obj };
if (!alias) delete newObj[from];
newObj[to] = fromGetter(obj);
}
return newObj;
});
}
/** Parse an input JSON string to an object */
json() {
return this.transform(parseJson);
}
noUnknown(message?: Message): this;
noUnknown(noAllow: boolean, message?: Message): this;
noUnknown(noAllow: Message | boolean = true, message = locale.noUnknown) {
if (typeof noAllow !== 'boolean') {
message = noAllow;
noAllow = true;
}
let next = this.test({
name: 'noUnknown',
exclusive: true,
message: message,
test(value) {
if (value == null) return true;
const unknownKeys = unknown(this.schema, value);
return (
!noAllow ||
unknownKeys.length === 0 ||
this.createError({ params: { unknown: unknownKeys.join(', ') } })
);
},
});
next.spec.noUnknown = noAllow;
return next;
}
unknown(allow = true, message = locale.noUnknown) {
return this.noUnknown(!allow, message);
}
transformKeys(fn: (key: string) => string) {
return this.transform((obj) => {
if (!obj) return obj;
const result: AnyObject = {};
for (const key of Object.keys(obj)) result[fn(key)] = obj[key];
return result;
});
}
camelCase() {
return this.transformKeys(camelCase);
}
snakeCase() {
return this.transformKeys(snakeCase);
}
constantCase() {
return this.transformKeys((key) => snakeCase(key).toUpperCase());
}
describe(options?: ResolveOptions<TContext>) {
let base = super.describe(options) as SchemaObjectDescription;
base.fields = {};
for (const [key, value] of Object.entries(this.fields)) {
let innerOptions = options;
if (innerOptions?.value) {
innerOptions = {
...innerOptions,
parent: innerOptions.value,
value: innerOptions.value[key],
};
}
base.fields[key] = value.describe(innerOptions);
}
return base;
}
}
create.prototype = ObjectSchema.prototype;