All files / lib/validation validators.ts

20% Statements 2/10
0% Branches 0/6
0% Functions 0/1
20% Lines 2/10

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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43  1x                                       1x                                          
import { CreateUserDto } from '../../src/types/user';
import { BadRequestError } from '../errors/errors';
 
/**
 * @interface Validator
 * @description An interface for validators.
 */
export interface Validator {
  /**
   * @method validate
   * @description Validates the given data.
   * @param {any} data - The data to validate.
   */
  validate(data: any): void;
}
 
/**
 * @class RegisterValidator
 * @description A validator for the registration data.
 * @implements Validator
 */
export class RegisterValidator implements Validator {
  /**
   * @method validate
   * @description Validates the registration data.
   * @param {CreateUserDto} data - The registration data.
   */
  public validate(data: CreateUserDto): void {
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    if (!emailRegex.test(data.email)) {
      throw new BadRequestError('Invalid email format');
    }
 
    if (data.password.length <= 5) {
      throw new BadRequestError('Password must be longer than 5 characters');
    }
 
    const passwordRegex = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,}$/;
    if (!passwordRegex.test(data.password)) {
      throw new BadRequestError('Password must contain at least one letter and one number');
    }
  }
}