Skip to content

validate

awekrx edited this page May 29, 2026 · 1 revision

validate

Import

import { validate } from '@dev-suite/decorators/validate'

Category

  • property

Use Case

Validate property assignment with predicate.

Replaces

  • Manual if/throw in every setter
  • Field-validation logic scattered across methods

Example 1

Without decorator

class TransferForm {
  private _amount = 0;

  set amount(next: number) {
    if (next <= 0) throw new Error('amount must be positive');
    this._amount = next;
  }
}

With decorator

import { validate } from '@dev-suite/decorators/validate';

class TransferForm {
  @validate(({ value }) => Number(value) > 0)
  amount = 0;
}

Why better

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

Example 2

Without decorator

class SignupState {
  private _email = '';

  set email(next: string) {
    if (!next.includes('@')) throw new Error('invalid email');
    this._email = next;
  }
}

With decorator

import { validate } from '@dev-suite/decorators/validate';

class SignupState {
  @validate(({ value }) => String(value).includes('@'))
  email = '';
}

Why better

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

Clone this wiki locally