Skip to content

Commit

Permalink
fix: super refinement function types (#2420)
Browse files Browse the repository at this point in the history
* fix: super refinement function types

* Add test

---------

Co-authored-by: Colin McDonnell <colinmcd94@gmail.com>
  • Loading branch information
carl0s-sa and colinhacks authored May 21, 2023
1 parent 0969950 commit af08390
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
29 changes: 29 additions & 0 deletions deno/lib/__tests__/refine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,35 @@ test("superRefine", () => {
Strings.parse(["asfd", "qwer"]);
});

test("superRefine async", async () => {
const Strings = z.array(z.string()).superRefine(async (val, ctx) => {
if (val.length > 3) {
ctx.addIssue({
code: z.ZodIssueCode.too_big,
maximum: 3,
type: "array",
inclusive: true,
exact: true,
message: "Too many items 😡",
});
}

if (val.length !== new Set(val).size) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `No duplicates allowed.`,
});
}
});

const result = await Strings.safeParseAsync(["asfd", "asfd", "asfd", "asfd"]);

expect(result.success).toEqual(false);
if (!result.success) expect(result.error.issues.length).toEqual(2);

Strings.parseAsync(["asfd", "qwer"]);
});

test("superRefine - type narrowing", () => {
type NarrowType = { type: string; age: number };
const schema = z
Expand Down
6 changes: 3 additions & 3 deletions deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,10 @@ export abstract class ZodType<
refinement: (arg: Output, ctx: RefinementCtx) => arg is RefinedOutput
): ZodEffects<this, RefinedOutput, Input>;
superRefine(
refinement: (arg: Output, ctx: RefinementCtx) => void
refinement: (arg: Output, ctx: RefinementCtx) => void | Promise<void>
): ZodEffects<this, Output, Input>;
superRefine(
refinement: (arg: Output, ctx: RefinementCtx) => unknown
refinement: (arg: Output, ctx: RefinementCtx) => unknown | Promise<unknown>
): ZodEffects<this, Output, Input> {
return this._refinement(refinement);
}
Expand Down Expand Up @@ -4174,7 +4174,7 @@ export class ZodPromise<T extends ZodTypeAny> extends ZodType<
//////////////////////////////////////////////

export type Refinement<T> = (arg: T, ctx: RefinementCtx) => any;
export type SuperRefinement<T> = (arg: T, ctx: RefinementCtx) => void;
export type SuperRefinement<T> = (arg: T, ctx: RefinementCtx) => void | Promise<void>;

export type RefinementEffect<T> = {
type: "refinement";
Expand Down
29 changes: 29 additions & 0 deletions src/__tests__/refine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,35 @@ test("superRefine", () => {
Strings.parse(["asfd", "qwer"]);
});

test("superRefine async", async () => {
const Strings = z.array(z.string()).superRefine(async (val, ctx) => {
if (val.length > 3) {
ctx.addIssue({
code: z.ZodIssueCode.too_big,
maximum: 3,
type: "array",
inclusive: true,
exact: true,
message: "Too many items 😡",
});
}

if (val.length !== new Set(val).size) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `No duplicates allowed.`,
});
}
});

const result = await Strings.safeParseAsync(["asfd", "asfd", "asfd", "asfd"]);

expect(result.success).toEqual(false);
if (!result.success) expect(result.error.issues.length).toEqual(2);

Strings.parseAsync(["asfd", "qwer"]);
});

test("superRefine - type narrowing", () => {
type NarrowType = { type: string; age: number };
const schema = z
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,10 @@ export abstract class ZodType<
refinement: (arg: Output, ctx: RefinementCtx) => arg is RefinedOutput
): ZodEffects<this, RefinedOutput, Input>;
superRefine(
refinement: (arg: Output, ctx: RefinementCtx) => void
refinement: (arg: Output, ctx: RefinementCtx) => void | Promise<void>
): ZodEffects<this, Output, Input>;
superRefine(
refinement: (arg: Output, ctx: RefinementCtx) => unknown
refinement: (arg: Output, ctx: RefinementCtx) => unknown | Promise<unknown>
): ZodEffects<this, Output, Input> {
return this._refinement(refinement);
}
Expand Down Expand Up @@ -4174,7 +4174,7 @@ export class ZodPromise<T extends ZodTypeAny> extends ZodType<
//////////////////////////////////////////////

export type Refinement<T> = (arg: T, ctx: RefinementCtx) => any;
export type SuperRefinement<T> = (arg: T, ctx: RefinementCtx) => void;
export type SuperRefinement<T> = (arg: T, ctx: RefinementCtx) => void | Promise<void>;

export type RefinementEffect<T> = {
type: "refinement";
Expand Down

0 comments on commit af08390

Please sign in to comment.