All files phone.validator.ts

93.33% Statements 14/15
100% Branches 6/6
50% Functions 1/2
88.88% Lines 8/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28  1x     1x   16x     15x       15x     11x     9x   8x            
// src/validators/phone.validator.ts
import { ValidatorConstraint, ValidatorConstraintInterface } from 'class-validator';
 
@ValidatorConstraint({ name: 'AngolanPhone', async: false })
export class AngolanPhoneValidator implements ValidatorConstraintInterface {
  validate(phone: string): boolean {
    if (!phone || typeof phone !== 'string') return false;
    
    // Remove todos os caracteres não numéricos
    const cleaned = phone.replace(/\D/g, '');
    
    // Verifica os padrões aceitáveis:
    // 1. Números nacionais: 9 dígitos começando com 9
    if (/^9[1-9]\d{7}$/.test(cleaned)) return true;
    
    // 2. Números internacionais: prefixo 244 seguido de 9 dígitos começando com 9
    if (/^2449[1-9]\d{7}$/.test(cleaned)) return true;
    
    // 3. Números internacionais com 00244
    if (/^002449[1-9]\d{7}$/.test(cleaned)) return true;
    
    return false;
  }
 
  defaultMessage(): string {
    return 'Número de telefone angolano inválido. Formatos válidos: 9XXXXXXXX, 2449XXXXXXXX, 002449XXXXXXXX';
  }
}