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

Preprocess method #468

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
26 changes: 26 additions & 0 deletions src/__tests__/preprocess.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// @ts-ignore TS6133
import { expect, test } from "@jest/globals";

import { z } from "..";

test("preprocess test suite", () => {
expect(z.string().email().preprocess(v => v.trim()).parse(' [email protected] ')).toBe("[email protected]");
z.string().max(3).preprocess(v => v.slice(0,3)).parse('abcde')
try {
z.string().max(3).preprocess(v => v.slice(0,4)).parse('abcde')
} catch (e) {
expect(e).toBeInstanceOf(z.ZodError)
}
// @ts-expect-error
expect(z.number().preprocess(v => v.trim()).parse(1)).toEqual({invalid_data: true})
expect(z.number().preprocess(v => v+1).parse(1)).toBe(2)
expect(z.number().preprocess(v => v+1).refine(v => v>1).parse(1)).toBe(2)
expect(z.boolean().preprocess(() => false).parse(true)).toBe(false)
expect(z.enum(['A','B']).preprocess(() => 'B').parse('A')).toBe('B')
expect(z.number().preprocess(v => v+1).transform(v => v+1).parse(1)).toBe(3);
expect(z.number().transform(v => v+1).preprocess(v => v+1).parse(1)).toBe(3);
expect(z.number().default(1).preprocess(v => (v ?? 0) + 1).parse(undefined)).toBe(2);
expect(z.string().nullable().preprocess(() => null).parse('abcde')).toBe(undefined);
expect(z.string().optional().preprocess(() => undefined).parse('abcde')).toBe(undefined);
});

43 changes: 43 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ export abstract class ZodType<
this._def = def;
this.transform = this.transform.bind(this) as any;
this.default = this.default.bind(this);
this.preprocess = this.preprocess.bind(this);
}

optional: <This extends this = this>() => ZodOptional<This> = () =>
Expand Down Expand Up @@ -366,6 +367,16 @@ export abstract class ZodType<
return returnType;
}

preprocess<This extends this = this>(
preprocessFn: (value: Input) => Output
): ZodPreprocess<This> {
return new ZodPreprocess({
// @ts-expect-error: 'innerType' does not exist on type 'Def'
innerType: this._def.innerType || this,
preprocessFn,
}) as any;
}

default<This extends this = this>(
def: util.noUndefined<Input>
): ZodDefault<This>;
Expand Down Expand Up @@ -2738,6 +2749,38 @@ export class ZodDefault<T extends ZodTypeAny> extends ZodType<
}) as any;
};
}
export interface ZodPreprocessDef<T extends ZodTypeAny = ZodTypeAny>
extends ZodTypeDef {
innerType: T;
preprocessFn: (value: T["_input"]) => T["_output"];
}

export class ZodPreprocess<T extends ZodTypeAny> extends ZodType<
T["_input"],
ZodPreprocessDef<T>,
T["_output"]
> {
_parse(ctx: ParseContext): any {
const data = this._def.preprocessFn(ctx.data);

return new PseudoPromise().then(() => {
return this._def.innerType._parseWithInvalidFallback(data, {
...ctx,
parentError: ctx.currentError,
});
});
}

removePreprocess() {
return this._def.innerType;
}

static create = <T extends ZodTypeAny>(type: T): ZodOptional<T> => {
return new ZodOptional({
innerType: type,
}) as any;
};
}

export const custom = <T>(
check?: (data: unknown) => any,
Expand Down