All files / app/validators global.validator.ts

77.14% Statements 54/70
52.38% Branches 11/21
100% Functions 6/6
77.14% Lines 54/70

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 711x 1x 1x 1x 208x 208x 208x 208x     208x 208x 208x 1x 1x 117x 117x 117x     117x 117x 117x 1x 1x 117x     117x 117x 117x 1x 1x 3x 3x 3x 1x 1x 2x 2x 2x 1x 1x 234x 234x 234x     234x 234x 234x 1x 1x 156x                 156x 156x 156x 1x 1x 1x 1x 1x  
import { parsePhoneNumberFromString } from 'libphonenumber-js';
 
export class GlobalValidator {
	static mailFormat(control: any): ValidationResult | null {
		const EMAIL_REGEXP =
			/^[a-z0-9!#$%&'*+=?^_`{|}~.-]+@[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/i;
 
		if (control.value !== '' && (control.value?.length <= 5 || !EMAIL_REGEXP.test(control.value))) {
			return { EmailIsIncorrect: true };
		}
 
		return null;
	}
 
	static passwordsMatch(control: any): ValidationResult | null {
		const pass = control.root.value['password'];
		const pass_re = control.value;
		if (pass && pass_re && pass !== pass_re) {
			return { passwords_not_match: true };
		}
 
		return null;
	}
 
	static passwordsLength(control: any): ValidationResult | null {
		if (control.value !== '' && control.value?.length <= 7) {
			return { too_short: true };
		}
 
		return null;
	}
 
	static passFormat(control: any): ValidationResult | null {
		const regex = new RegExp('^(((?=.*[a-z])(?=.*[A-Z]))|((?=.*[a-z])(?=.*[0-9]))|((?=.*[A-Z])(?=.*[0-9])))(?=.{6,})');
 
		if (control.value !== '' && (control.value?.length <= 5 || !regex.test(control.value))) {
			return { PasswordIncorrect: true };
		}
 
		return null;
	}
 
	static letter(control: any): ValidationResult | null {
		const regex = /^[a-zA-Z]*$/gim;
 
		if (control.value !== '' && !regex.test(control.value)) {
			return { pattern: true };
		}
 
		return null;
	}
 
	static phoneFormat(control: any): ValidationResult | null {
		if (control.value && control.value !== '') {
			if (control.value.internationalNumber && control.value.internationalNumber === '') {
				return { phone_invalid: true };
			}
			const phoneNumber: any = parsePhoneNumberFromString(control.value.internationalNumber);
			if (!phoneNumber?.isValid()) {
				return { phone_invalid: true };
			}
		}
 
		return null;
	}
}
 
interface ValidationResult {
	[key: string]: boolean;
}