Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PoC] Add refineWith #420

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ export abstract class ZodType<
constructor(def: Def) {
this._def = def;
this.transform = this.transform.bind(this) as any;
this.refineWith = this.refineWith.bind(this) as any;
this.default = this.default.bind(this);
}

Expand Down Expand Up @@ -359,6 +360,30 @@ export abstract class ZodType<
return returnType;
}

refineWith<NewOut, This extends this>(
returnType: ZodType<NewOut>
): This extends ZodEffects<infer T, any>
? ZodEffects<T, NewOut>
: ZodEffects<This, NewOut> {
let parsed: ReturnType<typeof returnType.safeParse>;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I suppose there's a concurrency bug here due to this being in the scope of the schema rather than the specific parsing invocation of it. So parsed would have to be stashed somewhere within the context.

const refined: ZodEffects<any> = this._refinement((val, ctx) => {
parsed = returnType.safeParse(val);
if (!parsed.success) {
for (const issue of parsed.error.issues) {
ctx.addIssue(issue);
}
}
});

return refined.transform(() => {
if (!parsed.success) {
throw new Error(`Unreachable path.`);
}

return parsed.data;
}) as any;
}

default<T extends Input, This extends this = this>(
def: T
): ZodOptional<This, true>;
Expand Down
25 changes: 25 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ export abstract class ZodType<
constructor(def: Def) {
this._def = def;
this.transform = this.transform.bind(this) as any;
this.refineWith = this.refineWith.bind(this) as any;
this.default = this.default.bind(this);
}

Expand Down Expand Up @@ -359,6 +360,30 @@ export abstract class ZodType<
return returnType;
}

refineWith<NewOut, This extends this>(
returnType: ZodType<NewOut>
): This extends ZodEffects<infer T, any>
? ZodEffects<T, NewOut>
: ZodEffects<This, NewOut> {
let parsed: ReturnType<typeof returnType.safeParse>;
const refined: ZodEffects<any> = this._refinement((val, ctx) => {
parsed = returnType.safeParse(val);
if (!parsed.success) {
for (const issue of parsed.error.issues) {
ctx.addIssue(issue);
}
}
});

return refined.transform(() => {
if (!parsed.success) {
throw new Error(`Unreachable path.`);
}

return parsed.data;
}) as any;
}

default<T extends Input, This extends this = this>(
def: T
): ZodOptional<This, true>;
Expand Down