import { AbstractControl , FormGroup , ValidatorFn } from '@angular/forms' ; import { addCommas , normalizeDiacritics } from '../utils' ; export function comparePasswords ( [ controlName1 , controlName2 ] ) { return ( group ) => { const password = group . get ( controlName1 ) . value ; const repeat = group . get ( controlName2 ) . value ; return ! password || ! repeat || password === repeat ? null : { passwordMismatch : { fields : [ controlName1 , controlName2 ] , } , } ; } ; } export type PasswordRules = Array < 'small' | 'capital' | 'number' | 'special' > ; export function validatePassword ( shouldContain = [ 'small' , 'capital' , 'number' , 'special' ] ) { return ( control ) => { if ( ! control . value ) return null ; const value = normalizeDiacritics ( control . value ) ; const regex = { small : /.*[a-z].*/ , capital : /.*[A-Z].*/ , number : /.*[0-9].*/ , special : /.*[^0-9a-zA-Z].*/ , } ; const missing = shouldContain . filter ( key => ! regex [ key ] . test ( value ) ) ; return missing . length ? { invalidPassword : { missing , description : addCommas ( missing . map ( key => ( { small : 'a small letter' , capital : 'a capital' , number : 'a number' , special : 'a special character' , } [ key ] ) ) ) , } , } : null ; } ; }