Class

EmailValidator

EmailValidator(messageopt)

Constructor

# new EmailValidator(messageopt)

The EmailValidator checks if a string matches a standard email address pattern. It extends the PatternValidator and uses a predefined email regex pattern to validate email addresses. This validator is typically used with the @email decorator.

Validator for checking if a string is a valid email address

sequenceDiagram participant C as Client participant E as EmailValidator participant P as PatternValidator C->>E: new EmailValidator(message) E->>P: super(message) C->>E: hasErrors(value, options) E->>P: super.hasErrors(value, options with EMAIL pattern) P-->>E: validation result E-->>C: validation result
Parameters:
Name Type Attributes Description
message string <optional>

Custom error message to display when validation fails, defaults to DEFAULT_ERROR_MESSAGES#EMAIL

View Source validation/Validators/EmailValidator.ts, line 11

Example
```typescript
// Create an email validator with default error message
const emailValidator = new EmailValidator();

// Create an email validator with custom error message
const customEmailValidator = new EmailValidator("Please enter a valid email address");

// Validate an email
const result = emailValidator.hasErrors("user@example.com"); // undefined (valid)
const invalidResult = emailValidator.hasErrors("invalid-email"); // Returns error message (invalid)
```

Extends

Methods

# hasErrors(value, optionsopt) → {string|undefined}

Validates that the provided string matches the email pattern. This method extends the PatternValidator's hasErrors method by ensuring the email pattern is used, even if not explicitly provided in the options.

Checks if a string is a valid email address

Parameters:
Name Type Attributes Default Description
value string

The string to validate as an email address

options PatternValidatorOptions <optional>
{}

Optional configuration options

Overrides:
See:

View Source validation/Validators/EmailValidator.ts, line 104

If no pattern is provided in the options

Error

Error message if validation fails, undefined if validation passes

string | undefined