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

Short circuit preprocess only on own errors #3124

Closed
wants to merge 2 commits into from
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
21 changes: 21 additions & 0 deletions deno/lib/__tests__/transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,27 @@ test("z.NEVER in preprocess", async () => {
}
});

// Fix for #3123
test("preprocess only short circuit on its own issues", () => {
const schema1 = z.object({
first: z.string(),
second: z.preprocess(() => undefined, z.string()),
});
const schema2 = z.object({
second: z.preprocess(() => undefined, z.string()),
first: z.string(),
});

const result1 = schema1.safeParse({});
const result2 = schema2.safeParse({});

expect(result1.success).toEqual(false);
expect(result2.success).toEqual(false);
if (!result1.success && !result2.success) {
expect(result1.error.issues).toEqual(result2.error.issues.reverse());
}
});

test("short circuit on dirty", () => {
const schema = z
.string()
Expand Down
4 changes: 3 additions & 1 deletion deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4264,8 +4264,10 @@ export class ZodEffects<

const effect = this._def.effect || null;

let hasIssue = false;
const checkCtx: RefinementCtx = {
addIssue: (arg: IssueData) => {
hasIssue = true;
addIssueToContext(ctx, arg);
if (arg.fatal) {
status.abort();
Expand All @@ -4282,7 +4284,7 @@ export class ZodEffects<

if (effect.type === "preprocess") {
const processed = effect.transform(ctx.data, checkCtx);
if (ctx.common.issues.length) {
if (hasIssue) {
return {
status: "dirty",
value: ctx.data,
Expand Down
21 changes: 21 additions & 0 deletions src/__tests__/transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,27 @@ test("z.NEVER in preprocess", async () => {
}
});

// Fix for #3123
test("preprocess only short circuit on its own issues", () => {
const schema1 = z.object({
first: z.string(),
second: z.preprocess(() => undefined, z.string()),
});
const schema2 = z.object({
second: z.preprocess(() => undefined, z.string()),
first: z.string(),
});

const result1 = schema1.safeParse({});
const result2 = schema2.safeParse({});

expect(result1.success).toEqual(false);
expect(result2.success).toEqual(false);
if (!result1.success && !result2.success) {
expect(result1.error.issues).toEqual(result2.error.issues.reverse());
}
});

test("short circuit on dirty", () => {
const schema = z
.string()
Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4264,8 +4264,10 @@ export class ZodEffects<

const effect = this._def.effect || null;

let hasIssue = false;
const checkCtx: RefinementCtx = {
addIssue: (arg: IssueData) => {
hasIssue = true;
addIssueToContext(ctx, arg);
if (arg.fatal) {
status.abort();
Expand All @@ -4282,7 +4284,7 @@ export class ZodEffects<

if (effect.type === "preprocess") {
const processed = effect.transform(ctx.data, checkCtx);
if (ctx.common.issues.length) {
if (hasIssue) {
return {
status: "dirty",
value: ctx.data,
Expand Down