Skip to content

email param

awekrx edited this page May 29, 2026 · 1 revision

email-param

Import

import { emailParam } from '@dev-suite/decorators/email-param'

Category

  • parameter

Use Case

Validate and normalize email argument before use.

Replaces

  • Regex validation inside each method
  • Manual trim/lowercase boilerplate

Example 1

Without decorator

class InviteService {
  send(email: string) {
    const normalized = email.trim().toLowerCase();
    if (!/^[^@]+@[^@]+\.[^@]+$/.test(normalized)) throw new Error('Invalid email');
    return this.mailer.send(normalized);
  }
}

With decorator

import { emailParam } from '@dev-suite/decorators/email-param';

class InviteService {
  send(@emailParam({ normalize: true }) email: string) {
    return this.mailer.send(email);
  }
}

Why better

  • Centralizes cross-cutting behavior.
  • Method/class/property code stays focused on domain logic.

Example 2

Without decorator

class AuthService {
  resetPassword(email: string) {
    if (!email.includes('@')) throw new Error('Bad email');
    return this.tokens.issue(email.toLowerCase());
  }
}

With decorator

import { emailParam } from '@dev-suite/decorators/email-param';

class AuthService {
  resetPassword(@emailParam() email: string) {
    return this.tokens.issue(email);
  }
}

Why better

  • Second scenario reuses same policy without duplication.
  • Behavior is more consistent and easier to audit.

Clone this wiki locally