affaan-m/ECC · warning · ErrEmailEmpty

email cannot be empty

Error message

email cannot be empty

What it means

A Go sentinel error defined as errors.New("email cannot be empty") and returned by validator.ValidateEmail when the input string is the empty string. Sentinel errors are compared by identity with errors.Is. This is the first guard in the validator, checked before the regex.

Source

Thrown at commands/go-test.md:131

```

✓ Tests fail as expected (panic).

## Step 4: Implement Minimal Code (GREEN)

```go
// validator/email.go
package validator

import (
    "errors"
    "regexp"
)

var emailRegex = regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`)

var (
    ErrEmailEmpty   = errors.New("email cannot be empty")
    ErrEmailInvalid = errors.New("email format is invalid")
)

func ValidateEmail(email string) error {
    if email == "" {
        return ErrEmailEmpty
    }
    if !emailRegex.MatchString(email) {
        return ErrEmailInvalid
    }
    return nil
}
```

## Step 5: Run Tests - Verify PASS

```bash
$ go test ./validator/...

View on GitHub (pinned to 01e15490f0)

Solutions

  1. Require the email field at the API contract layer (e.g. binding:"required,email") so empty never reaches the validator.
  2. Trim and check for empty before calling ValidateEmail, returning a 400 with a field-level message.
  3. Compare with errors.Is(err, validator.ErrEmailEmpty) at the handler layer to map to the correct HTTP status and message.
  4. If empty email is legitimately optional for this flow, skip validation rather than treating it as an error.

Example fix

// before
if email == "" {
    return ErrEmailEmpty
}

// after: route by sentinel at the boundary
if err := validator.ValidateEmail(input.Email); err != nil {
    if errors.Is(err, validator.ErrEmailEmpty) {
        return httpError(400, "email", "email is required")
    }
    if errors.Is(err, validator.ErrEmailInvalid) {
        return httpError(400, "email", "email format is invalid")
    }
    return httpError(500, "email", "validation failed")
}
Defensive patterns

Strategy: validation

Validate before calling

func RequireEmail(email string) error {
    email = strings.TrimSpace(email)
    if email == "" {
        return validator.ErrEmailEmpty
    }
    return validator.ValidateEmail(email)
}

Type guard

func IsNonEmptyString(s string) bool { return strings.TrimSpace(s) != "" }

Try / catch

null

Prevention

When it happens

Trigger: Calling ValidateEmail("") or ValidateEmail with a variable that resolved to "" — e.g. a form field omitted, a JSON "email":"" payload, or a header that was trimmed to empty.

Common situations: HTTP handler binding where the client omitted the email field; leading/trailing whitespace stripped leaving empty; default zero-value string from an uninitialized struct field; CSV import with a blank email column.

Related errors


AI-assisted analysis of affaan-m/ECC@01e15490f0 (2026-08-13). Data as JSON: /api/errors/454a80e22a028d8c. Report an issue: GitHub.