Skip to content

enum param

awekrx edited this page May 29, 2026 · 1 revision

enum-param

Import

import { enumParam } from '@dev-suite/decorators/enum-param'

Category

  • parameter

Use Case

Restrict parameter to allowed enum-like values.

Replaces

  • Inline includes guards
  • Repeated switch default errors

Example 1

Without decorator

class FeatureService {
  setMode(mode: string) {
    if (!['off', 'on', 'beta'].includes(mode)) throw new Error('Invalid mode');
    return this.store.set(mode);
  }
}

With decorator

import { enumParam } from '@dev-suite/decorators/enum-param';

class FeatureService {
  setMode(@enumParam({ allowed: ['off', 'on', 'beta'] }) mode: string) {
    return this.store.set(mode);
  }
}

Why better

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

Example 2

Without decorator

class ReportService {
  run(format: string) {
    if (!['csv', 'json'].includes(format)) throw new Error('Invalid format');
    return this.exporter.run(format);
  }
}

With decorator

import { enumParam } from '@dev-suite/decorators/enum-param';

class ReportService {
  run(@enumParam({ allowed: ['csv', 'json'] }) format: string) {
    return this.exporter.run(format);
  }
}

Why better

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

Clone this wiki locally