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

Function refine

packages/zod/src/v3/types.ts:331–368  ·  view source on GitHub ↗
(
    check: (arg: Output) => unknown,
    message?: string | CustomErrorParams | ((arg: Output) => CustomErrorParams)
  )

Source from the content-addressed store, hash-verified

329 message?: string | CustomErrorParams | ((arg: Output) => CustomErrorParams)
330 ): ZodEffects<this, Output, Input>;
331 refine(
332 check: (arg: Output) => unknown,
333 message?: string | CustomErrorParams | ((arg: Output) => CustomErrorParams)
334 ): ZodEffects<this, Output, Input> {
335 const getIssueProperties = (val: Output) => {
336 if (typeof message === "string" || typeof message === "undefined") {
337 return { message };
338 } else if (typeof message === "function") {
339 return message(val);
340 } else {
341 return message;
342 }
343 };
344 return this._refinement((val, ctx) => {
345 const result = check(val);
346 const setError = () =>
347 ctx.addIssue({
348 code: ZodIssueCode.custom,
349 ...getIssueProperties(val),
350 });
351 if (typeof Promise !== "undefined" && result instanceof Promise) {
352 return result.then((data) => {
353 if (!data) {
354 setError();
355 return false;
356 } else {
357 return true;
358 }
359 });
360 }
361 if (!result) {
362 setError();
363 return false;
364 } else {
365 return true;
366 }
367 });
368 }
369
370 refinement<RefinedOutput extends Output>(
371 check: (arg: Output) => arg is RefinedOutput,

Callers

nothing calls this directly

Calls 2

setErrorFunction · 0.85
checkFunction · 0.50

Tested by

no test coverage detected