Skip to content

Commit

Permalink
Faster paths
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcw committed Mar 21, 2022
1 parent 0780ee3 commit 0dc6714
Showing 1 changed file with 33 additions and 30 deletions.
63 changes: 33 additions & 30 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ export interface ZodTypeDef {
description?: string;
}

class ParseInputLazyPath implements ParseInput {
parent: ParseContext;
data: any;
_path: any;
_key: any;
constructor(parent: ParseContext, value: any, path: any, key: any) {
this.parent = parent;
this.data = value;
this._path = path;
this._key = key;
}
get path() {
return [...this._path, this._key];
}
}

const handleResult = <Input, Output>(
ctx: ParseContext,
result: SyncParseReturnType<Output>
Expand Down Expand Up @@ -1289,23 +1305,19 @@ export class ZodArray<
if (ctx.common.async) {
return Promise.all(
(ctx.data as any[]).map((item, i) => {
return def.type._parseAsync({
parent: ctx,
path: [...ctx.path, i],
data: item,
});
return def.type._parseAsync(
new ParseInputLazyPath(ctx, item, ctx.path, i)
);
})
).then((result) => {
return ParseStatus.mergeArray(status, result);
});
}

const result = (ctx.data as any[]).map((item, i) => {
return def.type._parseSync({
parent: ctx,
path: [...ctx.path, i],
data: item,
});
return def.type._parseSync(
new ParseInputLazyPath(ctx, item, ctx.path, i)
);
});

return ParseStatus.mergeArray(status, result);
Expand Down Expand Up @@ -1505,6 +1517,7 @@ function deepPartialify(schema: ZodTypeAny): any {
return schema;
}
}

export class ZodObject<
T extends ZodRawShape,
UnknownKeys extends UnknownKeysParam = "strip",
Expand Down Expand Up @@ -1552,11 +1565,9 @@ export class ZodObject<
const value = ctx.data[key];
pairs.push({
key: { status: "valid", value: key },
value: keyValidator._parse({
parent: ctx,
data: value,
path: [...ctx.path, key],
}),
value: keyValidator._parse(
new ParseInputLazyPath(ctx, value, ctx.path, key)
),
alwaysSet: key in ctx.data,
});
}
Expand Down Expand Up @@ -2368,11 +2379,9 @@ export class ZodTuple<
.map((item, itemIndex) => {
const schema = this._def.items[itemIndex] || this._def.rest;
if (!schema) return null as any as SyncParseReturnType<any>;
return schema._parse({
data: item,
path: [...ctx.path, itemIndex],
parent: ctx,
});
return schema._parse(
new ParseInputLazyPath(ctx, item, ctx.path, itemIndex)
);
})
.filter((x) => !!x); // filter nulls

Expand Down Expand Up @@ -2468,16 +2477,10 @@ export class ZodRecord<

for (const key in ctx.data) {
pairs.push({
key: keyType._parse({
data: key,
path: [...ctx.path, key],
parent: ctx,
}),
value: valueType._parse({
data: ctx.data[key],
path: [...ctx.path, key],
parent: ctx,
}),
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, key)),
value: valueType._parse(
new ParseInputLazyPath(ctx, ctx.data[key], ctx.path, key)
),
});
}

Expand Down

0 comments on commit 0dc6714

Please sign in to comment.