{"record":{"id":"e893ef66516f18a2","repo":"medusajs/medusa","slug":"email-is-required-to-create-a-customer","errorCode":null,"errorMessage":"Email is required to create a customer","messagePattern":"Email is required to create a customer","errorType":"exception","errorClass":"MedusaError","httpStatus":400,"severity":"error","filePath":"packages/core/core-flows/src/customer/steps/validate-customer-account-creation.ts","lineNumber":34,"sourceCode":" * @example\n * const data = validateCustomerAccountCreation({\n *   authIdentityId: \"au_1234\",\n *   customerData: {\n *     first_name: \"John\",\n *     last_name: \"Doe\",\n *     email: \"john.doe@example.com\",\n *   }\n * })\n */\nexport const validateCustomerAccountCreation = createStep(\n  validateCustomerAccountCreationStepId,\n  async (input: CreateCustomerAccountWorkflowInput, { container }) => {\n    const customerService = container.resolve(Modules.CUSTOMER)\n\n    const { email } = input.customerData\n\n    if (!email) {\n      throw new MedusaError(\n        MedusaError.Types.INVALID_DATA,\n        \"Email is required to create a customer\"\n      )\n    }\n\n    // Check if customer with email already exists\n    const existingCustomers = await customerService.listCustomers({ email })\n\n    if (existingCustomers?.length) {\n      const hasExistingAccount = existingCustomers.some(\n        (customer) => customer.has_account\n      )\n\n      if (hasExistingAccount && input.authIdentityId) {\n        throw new MedusaError(\n          MedusaError.Types.DUPLICATE_ERROR,\n          \"Customer with this email already has an account\"\n        )","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/medusajs/medusa/blob/5e06e544a296b9033f20f71f11c559f81a0e5739/packages/core/core-flows/src/customer/steps/validate-customer-account-creation.ts#L16-L52","documentation":"validateCustomerAccountCreationStep requires customerData.email before creating a customer account; without an email the account cannot be linked to an auth identity.","triggerScenarios":"Calling createCustomerAccountWorkflow with customerData that has no email (undefined or empty string).","commonSituations":"Signup form submitted without email; email field named differently in client payload; guest-checkout code path reusing the account-creation workflow by mistake; trimming email to empty before passing through.","solutions":["Include a non-empty email in input.customerData","Validate the request body with a schema (require email, validate format) before invoking the workflow","If creating a guest customer, use createCustomerWorkflow or the store-customer route instead of the account workflow"],"exampleFix":"// before\nawait createCustomerAccountWorkflow(container).run({\n  input: { customerData: { first_name: \"A\" }, authIdentityId },\n})\n\n// after\nawait createCustomerAccountWorkflow(container).run({\n  input: { customerData: { first_name: \"A\", email: \"a@b.com\" }, authIdentityId },\n})","handlingStrategy":"validation","validationCode":"if (!input?.customerData?.email?.trim()) throw new Error(\"Email is required\")","typeGuard":"const hasEmail = (i?: { customerData?: { email?: string } }) => !!i?.customerData?.email?.trim()","tryCatchPattern":"try { await createCustomerAccountWorkflow(scope).run({ input }) } catch (e) { if (e.type === MedusaError.Types.INVALID_DATA && /Email is required/.test(e.message)) { return badRequest(\"email is required\") } throw e }","preventionTips":["Require + validate email format in the request schema","Use guest-customer flows when no account is intended"],"tags":["customer","email","signup","validation"],"backgroundTag":"required-field-missing","analyzedSha":"5e06e544a296b9033f20f71f11c559f81a0e5739","analyzedAt":"2026-08-27T07:24:39.599Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}