Skip to content

Commit

Permalink
3.21.4
Browse files Browse the repository at this point in the history
  • Loading branch information
colinhacks committed Mar 7, 2023
1 parent 2db0dca commit 22f3cc6
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 25 deletions.
8 changes: 8 additions & 0 deletions deno/lib/__tests__/custom.test.ts
Expand Up @@ -9,3 +9,11 @@ test("passing validations", () => {
example1.parse(1234);
expect(() => example1.parse({})).toThrow();
});

test("string params", () => {
const example1 = z.custom<number>((x) => typeof x !== "number", "customerr");
const result = example1.safeParse(1234);
expect(result.success).toEqual(false);
// @ts-ignore
expect(JSON.stringify(result.error)).toContain("customerr");
});
2 changes: 1 addition & 1 deletion deno/lib/helpers/util.ts
Expand Up @@ -114,7 +114,7 @@ export namespace objectUtil {
T extends object,
R extends keyof T = requiredKeys<T>
// O extends keyof T = optionalKeys<T>
> = Pick<T, R> & Partial<T>;
> = Pick<Required<T>, R> & Partial<T>;
// = { [k in O]?: T[k] } & { [k in R]: T[k] };

export type identity<T> = T;
Expand Down
20 changes: 17 additions & 3 deletions deno/lib/types.ts
Expand Up @@ -4743,14 +4743,28 @@ export class ZodPipeline<
type CustomParams = CustomErrorParams & { fatal?: boolean };
export const custom = <T>(
check?: (data: unknown) => any,
params: CustomParams | ((input: any) => CustomParams) = {},
/* @deprecated */
params: string | CustomParams | ((input: any) => CustomParams) = {},
/*
* @deprecated
*
* Pass `fatal` into the params object instead:
*
* ```ts
* z.string().custom((val) => val.length > 5, { fatal: false })
* ```
*
*/
fatal?: boolean
): ZodType<T> => {
if (check)
return ZodAny.create().superRefine((data, ctx) => {
if (!check(data)) {
const p = typeof params === "function" ? params(data) : params;
const p =
typeof params === "function"
? params(data)
: typeof params === "string"
? { message: params }
: params;
const _fatal = p.fatal ?? fatal ?? true;
const p2 = typeof p === "string" ? { message: p } : p;
ctx.addIssue({ code: "custom", ...p2, fatal: _fatal });
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "zod",
"version": "3.21.3",
"version": "3.21.4",
"author": "Colin McDonnell <colin@colinhacks.com>",
"repository": {
"type": "git",
Expand Down
16 changes: 0 additions & 16 deletions playground.ts
@@ -1,18 +1,2 @@
import { z } from "./src";
z;

const baseCategorySchema = z.object({
name: z.string().brand("CategoryName"),
});

type CategoryInput = z.input<typeof baseCategorySchema> & {
subcategories: CategoryInput[];
};
type CategoryOutput = z.output<typeof baseCategorySchema> & {
subcategories: CategoryOutput[];
};

const categorySchema: z.ZodType<CategoryOutput, any, CategoryInput> =
baseCategorySchema.extend({
subcategories: z.lazy(() => categorySchema.array()),
});
8 changes: 8 additions & 0 deletions src/__tests__/custom.test.ts
Expand Up @@ -8,3 +8,11 @@ test("passing validations", () => {
example1.parse(1234);
expect(() => example1.parse({})).toThrow();
});

test("string params", () => {
const example1 = z.custom<number>((x) => typeof x !== "number", "customerr");
const result = example1.safeParse(1234);
expect(result.success).toEqual(false);
// @ts-ignore
expect(JSON.stringify(result.error)).toContain("customerr");
});
2 changes: 1 addition & 1 deletion src/helpers/util.ts
Expand Up @@ -114,7 +114,7 @@ export namespace objectUtil {
T extends object,
R extends keyof T = requiredKeys<T>
// O extends keyof T = optionalKeys<T>
> = Pick<T, R> & Partial<T>;
> = Pick<Required<T>, R> & Partial<T>;
// = { [k in O]?: T[k] } & { [k in R]: T[k] };

export type identity<T> = T;
Expand Down
20 changes: 17 additions & 3 deletions src/types.ts
Expand Up @@ -4743,14 +4743,28 @@ export class ZodPipeline<
type CustomParams = CustomErrorParams & { fatal?: boolean };
export const custom = <T>(
check?: (data: unknown) => any,
params: CustomParams | ((input: any) => CustomParams) = {},
/* @deprecated */
params: string | CustomParams | ((input: any) => CustomParams) = {},
/*
* @deprecated
*
* Pass `fatal` into the params object instead:
*
* ```ts
* z.string().custom((val) => val.length > 5, { fatal: false })
* ```
*
*/
fatal?: boolean
): ZodType<T> => {
if (check)
return ZodAny.create().superRefine((data, ctx) => {
if (!check(data)) {
const p = typeof params === "function" ? params(data) : params;
const p =
typeof params === "function"
? params(data)
: typeof params === "string"
? { message: params }
: params;
const _fatal = p.fatal ?? fatal ?? true;
const p2 = typeof p === "string" ? { message: p } : p;
ctx.addIssue({ code: "custom", ...p2, fatal: _fatal });
Expand Down

0 comments on commit 22f3cc6

Please sign in to comment.