Skip to content

sanitize

awekrx edited this page May 29, 2026 · 1 revision

sanitize

Import

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

Category

  • parameter

Use Case

Sanitize unsafe text input before business handling.

Replaces

  • Inline regex sanitation blocks
  • Scattered content-cleanup utilities

Example 1

Without decorator

class CommentService {
  post(text: string) {
    const sanitized = text.replace(/<script[\s\S]*?>[\s\S]*?<\/script>/gi, '');
    return this.repo.save(sanitized);
  }
}

With decorator

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

class CommentService {
  post(@sanitize() text: string) {
    return this.repo.save(text);
  }
}

Why better

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

Example 2

Without decorator

class ProfileService {
  setBio(bio: string) {
    const clean = bio.replace(/[<>]/g, '');
    return this.repo.setBio(clean);
  }
}

With decorator

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

class ProfileService {
  setBio(@sanitize({ replacement: '' }) bio: string) {
    return this.repo.setBio(bio);
  }
}

Why better

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

Clone this wiki locally