MCPcopy
hub / github.com/colinhacks/zod / custom

Function custom

packages/zod/src/v3/types.ts:4915–4950  ·  view source on GitHub ↗
(
  check?: (data: any) => any,
  _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
)

Source from the content-addressed store, hash-verified

4913}
4914type CustomParams = CustomErrorParams & { fatal?: boolean };
4915export function custom<T>(
4916 check?: (data: any) => any,
4917 _params: string | CustomParams | ((input: any) => CustomParams) = {},
4918 /**
4919 * @deprecated
4920 *
4921 * Pass `fatal` into the params object instead:
4922 *
4923 * ```ts
4924 * z.string().custom((val) => val.length > 5, { fatal: false })
4925 * ```
4926 *
4927 */
4928 fatal?: boolean
4929): ZodType<T, ZodTypeDef, T> {
4930 if (check)
4931 return ZodAny.create().superRefine((data, ctx) => {
4932 const r = check(data);
4933 if (r instanceof Promise) {
4934 return r.then((r) => {
4935 if (!r) {
4936 const params = cleanParams(_params, data);
4937 const _fatal = params.fatal ?? fatal ?? true;
4938 ctx.addIssue({ code: "custom", ...params, fatal: _fatal });
4939 }
4940 });
4941 }
4942 if (!r) {
4943 const params = cleanParams(_params, data);
4944 const _fatal = params.fatal ?? fatal ?? true;
4945 ctx.addIssue({ code: "custom", ...params, fatal: _fatal });
4946 }
4947 return;
4948 });
4949 return ZodAny.create();
4950}
4951
4952export { ZodType as Schema, ZodType as ZodSchema };
4953

Callers 1

instanceOfTypeFunction · 0.70

Calls 5

cleanParamsFunction · 0.85
superRefineMethod · 0.80
addIssueMethod · 0.65
checkFunction · 0.50
createMethod · 0.45

Tested by

no test coverage detected