Skip to content

Commit

Permalink
Allow external validators in z.custom
Browse files Browse the repository at this point in the history
  • Loading branch information
colinhacks committed Apr 16, 2024
1 parent 9894ce3 commit b2ed20c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
14 changes: 5 additions & 9 deletions deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,11 +592,7 @@ const nanoidRegex = /^[a-z0-9_-]{21}$/i;
// const emailRegex =
// /^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/i;
const emailRegex =
<<<<<<< HEAD
/^(?!\.)(?!.*\.\.)([A-Z0-9_'+-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
=======
/^(?!\.)(?!.*\.\.)([A-Z0-9_+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
>>>>>>> f5b65ff (fix: escape hyphen in regex)
/^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
// const emailRegex =
// /^[a-z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-z0-9-]+(?:\.[a-z0-9\-]+)*$/i;

Expand Down Expand Up @@ -5015,8 +5011,8 @@ export class ZodReadonly<T extends ZodTypeAny> extends ZodType<
////////////////////////////////////////
////////////////////////////////////////
type CustomParams = CustomErrorParams & { fatal?: boolean };
export const custom = <T>(
check?: (data: unknown) => any,
export function custom<T>(
check?: (data: any) => any,
params: string | CustomParams | ((input: any) => CustomParams) = {},
/**
* @deprecated
Expand All @@ -5029,7 +5025,7 @@ export const custom = <T>(
*
*/
fatal?: boolean
): ZodType<T> => {
): ZodType<T> {
if (check)
return ZodAny.create().superRefine((data, ctx) => {
if (!check(data)) {
Expand All @@ -5045,7 +5041,7 @@ export const custom = <T>(
}
});
return ZodAny.create();
};
}

export { ZodType as Schema, ZodType as ZodSchema };

Expand Down
8 changes: 4 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5011,8 +5011,8 @@ export class ZodReadonly<T extends ZodTypeAny> extends ZodType<
////////////////////////////////////////
////////////////////////////////////////
type CustomParams = CustomErrorParams & { fatal?: boolean };
export const custom = <T>(
check?: (data: unknown) => any,
export function custom<T>(
check?: (data: any) => any,
params: string | CustomParams | ((input: any) => CustomParams) = {},
/**
* @deprecated
Expand All @@ -5025,7 +5025,7 @@ export const custom = <T>(
*
*/
fatal?: boolean
): ZodType<T> => {
): ZodType<T> {
if (check)
return ZodAny.create().superRefine((data, ctx) => {
if (!check(data)) {
Expand All @@ -5041,7 +5041,7 @@ export const custom = <T>(
}
});
return ZodAny.create();
};
}

export { ZodType as Schema, ZodType as ZodSchema };

Expand Down

0 comments on commit b2ed20c

Please sign in to comment.