| 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, |