(params: RawCreateParams)
| 121 | description?: string | undefined; |
| 122 | }; |
| 123 | function processCreateParams(params: RawCreateParams): ProcessedCreateParams { |
| 124 | if (!params) return {}; |
| 125 | const { errorMap, invalid_type_error, required_error, description } = params; |
| 126 | if (errorMap && (invalid_type_error || required_error)) { |
| 127 | throw new Error(`Can't use "invalid_type_error" or "required_error" in conjunction with custom error map.`); |
| 128 | } |
| 129 | if (errorMap) return { errorMap: errorMap, description }; |
| 130 | const customMap: ZodErrorMap = (iss, ctx) => { |
| 131 | const { message } = params; |
| 132 | |
| 133 | if (iss.code === "invalid_enum_value") { |
| 134 | return { message: message ?? ctx.defaultError }; |
| 135 | } |
| 136 | if (typeof ctx.data === "undefined") { |
| 137 | return { message: message ?? required_error ?? ctx.defaultError }; |
| 138 | } |
| 139 | if (iss.code !== "invalid_type") return { message: ctx.defaultError }; |
| 140 | return { message: message ?? invalid_type_error ?? ctx.defaultError }; |
| 141 | }; |
| 142 | return { errorMap: customMap, description }; |
| 143 | } |
| 144 | |
| 145 | export type SafeParseSuccess<Output> = { |
| 146 | success: true; |
no outgoing calls
no test coverage detected