All files / app/core/users user.registerform.validation.ts

80.55% Statements 29/36
77.77% Branches 7/9
83.33% Functions 5/6
78.78% Lines 26/33

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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70            1x   1x                 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x           1x   1x       1x 1x 1x 1x 1x 1x 1x                         1x       1x        
import { Injectable, signal } from '@angular/core';
import { UserRegisterFormData } from './user.interfaces';
import { email, form, minLength, required, validate } from '@angular/forms/signals';
 
 
@Injectable({ providedIn: 'root' })
export class RegisterFormValidationService {
  
  private readonly initialValues: UserRegisterFormData = {
    name: '',
    lastname: '',
    email: '',
    password: '',
    confirmPassword: '',
    termsAndConditions: false
  };
 
  private readonly registerModel = signal<UserRegisterFormData>(this.initialValues);
 
  private readonly registerForm = form(this.registerModel, (schemaPath) => {
    required(schemaPath.name, {message: 'Nombre es requerido'});
    required(schemaPath.lastname, {message: 'Apellido es requerido'});
    required(schemaPath.email, {message: 'El correo electronico es requerido'});
    email(schemaPath.email, {message: 'Ingrese una dirección de email válida'});
    required(schemaPath.password, {message: 'Contraseña es requerida'});
    minLength(schemaPath.password, 8, {message: 'La contraseña debe tener al menos 8 caracteres'});
    required(schemaPath.confirmPassword, {message: 'Confirmar contraseña es requerido'});
    validate(schemaPath.confirmPassword, ({value, valueOf}) => {
      const confirmPassword = value();
      const password = valueOf(schemaPath.password);
      Iif (confirmPassword !== password) {
        return {
          kind: 'passwordMismatch',
          message: 'Las contraseñas no coinciden',
        };
      }
      return null;
    });
    required(schemaPath.termsAndConditions, {message: 'Debe aceptar los términos y condiciones'});
  });
 
  resetForm() {
    this.registerModel.set({ ...this.initialValues });
    this.registerForm.name().reset();
    this.registerForm.lastname().reset();
    this.registerForm.email().reset();
    this.registerForm.password().reset();
    this.registerForm.confirmPassword().reset();
    this.registerForm.termsAndConditions().reset();
  }
 
  markAllFieldsAsTouched() {
    this.registerForm.name().markAsTouched();
    this.registerForm.lastname().markAsTouched();
    this.registerForm.email().markAsTouched();
    this.registerForm.password().markAsTouched();
    this.registerForm.confirmPassword().markAsTouched();
    this.registerForm.termsAndConditions().markAsTouched();
  }
 
  getRegisterModel() {
    return this.registerModel;
  }
 
  getRegisterForm() {
    return this.registerForm;
  }
}