Skip to content

range param

awekrx edited this page May 29, 2026 · 1 revision

range-param

Import

import { rangeParam } from '@dev-suite/decorators/range-param'

Category

  • parameter

Use Case

Validate numeric argument is inside required range.

Replaces

  • Inline min/max checks and throws
  • Repeated validation helpers

Example 1

Without decorator

class LoanService {
  quote(termMonths: number) {
    if (termMonths < 6 || termMonths > 84) throw new Error('Out of range');
    return this.engine.quote(termMonths);
  }
}

With decorator

import { rangeParam } from '@dev-suite/decorators/range-param';

class LoanService {
  quote(@rangeParam({ min: 6, max: 84 }) termMonths: number) {
    return this.engine.quote(termMonths);
  }
}

Why better

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

Example 2

Without decorator

class TaxService {
  calc(rate: number) {
    if (rate < 0 || rate > 1) throw new Error('Invalid rate');
    return this.core.calc(rate);
  }
}

With decorator

import { rangeParam } from '@dev-suite/decorators/range-param';

class TaxService {
  calc(@rangeParam({ min: 0, max: 1 }) rate: number) {
    return this.core.calc(rate);
  }
}

Why better

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

Clone this wiki locally