{"id":"f83bba4bec81e8c0","repo":"colinhacks/zod","slug":"can-t-use-invalid-type-error-or-required-error","errorCode":null,"errorMessage":"Can't use \"invalid_type_error\" or \"required_error\" in conjunction with custom error map.","messagePattern":"Can't use \"invalid_type_error\" or \"required_error\" in conjunction with custom error map\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/zod/src/v3/types.ts","lineNumber":127,"sourceCode":"\nexport type RawCreateParams =\n  | {\n      errorMap?: ZodErrorMap | undefined;\n      invalid_type_error?: string | undefined;\n      required_error?: string | undefined;\n      message?: string | undefined;\n      description?: string | undefined;\n    }\n  | undefined;\nexport type ProcessedCreateParams = {\n  errorMap?: ZodErrorMap | undefined;\n  description?: string | undefined;\n};\nfunction processCreateParams(params: RawCreateParams): ProcessedCreateParams {\n  if (!params) return {};\n  const { errorMap, invalid_type_error, required_error, description } = params;\n  if (errorMap && (invalid_type_error || required_error)) {\n    throw new Error(`Can't use \"invalid_type_error\" or \"required_error\" in conjunction with custom error map.`);\n  }\n  if (errorMap) return { errorMap: errorMap, description };\n  const customMap: ZodErrorMap = (iss, ctx) => {\n    const { message } = params;\n\n    if (iss.code === \"invalid_enum_value\") {\n      return { message: message ?? ctx.defaultError };\n    }\n    if (typeof ctx.data === \"undefined\") {\n      return { message: message ?? required_error ?? ctx.defaultError };\n    }\n    if (iss.code !== \"invalid_type\") return { message: ctx.defaultError };\n    return { message: message ?? invalid_type_error ?? ctx.defaultError };\n  };\n  return { errorMap: customMap, description };\n}\n\nexport type SafeParseSuccess<Output> = {","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/colinhacks/zod/blob/912f0f51b0ced654d0069741e7160834dca742ee/packages/zod/src/v3/types.ts#L109-L145","documentation":"Thrown by processCreateParams when a schema is constructed with both a custom `errorMap` and one of `invalid_type_error` or `required_error` in the same params object. These are mutually exclusive because invalid_type_error/required_error compile down into an implicit errorMap, so supplying both is ambiguous.","triggerScenarios":"Calling a schema factory like `z.string({ errorMap: myMap, invalid_type_error: \"...\" })` or `z.object({...}, { errorMap, required_error })`. Any v3 schema creator that accepts RawCreateParams can hit it.","commonSituations":"Migrating per-field string error messages to a shared errorMap and forgetting to remove the old strings; copy-pasting params from one schema to another; mixing a global setErrorMap-style helper with inline string overrides.","solutions":["Pick one strategy: either keep errorMap and delete invalid_type_error/required_error, or drop errorMap and use the string params.","If you need both behaviours, encode invalid_type_error/required_error logic inside your custom errorMap (return your message for code 'invalid_type' and when ctx.data is undefined).","Audit every schema factory call in the affected module for the conflicting keys."],"exampleFix":"// before\nz.string({\n  errorMap: myErrorMap,\n  invalid_type_error: \"Must be a string\",\n  required_error: \"Required\",\n});\n\n// after\nz.string({ errorMap: myErrorMap });\n// or, encode the same messages inside myErrorMap:\nconst myErrorMap = (iss, ctx) => {\n  if (typeof ctx.data === \"undefined\") return { message: \"Required\" };\n  if (iss.code === \"invalid_type\") return { message: \"Must be a string\" };\n  return { message: ctx.defaultError };\n};","handlingStrategy":"validation","validationCode":"function checkParams(p: RawCreateParams) {\n  if (p && p.errorMap && (p.invalid_type_error || p.required_error)) {\n    throw new Error(\"Remove invalid_type_error/required_error when using errorMap\");\n  }\n}\n// call before z.X(..., params)","typeGuard":"function isExclusiveErrorParams(\n  p: RawCreateParams\n): p is { errorMap: NonNullable<RawCreateParams> extends never ? never : any } {\n  return !!p?.errorMap && !p.invalid_type_error && !p.required_error;\n}","tryCatchPattern":"try { const s = z.string(params); }\ncatch (e) { /* surface to schema author: pick one error strategy */ }","preventionTips":["Standardise on a single error strategy per module (all errorMap or all string params).","Wrap schema construction in a factory that enforces the mutual exclusion.","Lint for params objects containing both keys."],"tags":["errormap","schema-construction","v3"],"analyzedSha":"912f0f51b0ced654d0069741e7160834dca742ee","analyzedAt":"2026-08-03T17:41:55.908Z","schemaVersion":2}