Skip to content

uuid param

awekrx edited this page May 29, 2026 · 1 revision

uuid-param

Import

import { uuidParam } from '@dev-suite/decorators/uuid-param'

Category

  • parameter

Use Case

Validate UUID argument format before use.

Replaces

  • Regex UUID checks in methods
  • Repeated validator calls

Example 1

Without decorator

class UserService {
  get(id: string) {
    if (!/^[0-9a-f-]{36}$/i.test(id)) throw new Error('Invalid UUID');
    return this.repo.get(id);
  }
}

With decorator

import { uuidParam } from '@dev-suite/decorators/uuid-param';

class UserService {
  get(@uuidParam() id: string) {
    return this.repo.get(id);
  }
}

Why better

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

Example 2

Without decorator

class SessionService {
  revoke(sessionId: string) {
    if (!isUuid(sessionId)) throw new Error('Bad sessionId');
    return this.sessions.revoke(sessionId);
  }
}

With decorator

import { uuidParam } from '@dev-suite/decorators/uuid-param';

class SessionService {
  revoke(@uuidParam({ message: 'Bad sessionId' }) sessionId: string) {
    return this.sessions.revoke(sessionId);
  }
}

Why better

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

Clone this wiki locally