import { AbstractControl , ValidatorFn } from "@angular/forms" ; import { isEmpty , isNotEmpty } from "@gauzy/common-angular" ; import { patterns } from "../../@shared/regex/regex-patterns.const" ; export class EmailValidator { static pattern ( field ) { return ( formGroup ) : { [ key ] : boolean ; } | null => { const control = formGroup . get ( field ) ; return EmailValidator . validator ( control , patterns . email ) ; } ; } param control param pattern returns static validator ( control , pattern ) { if ( isNotEmpty ( control . value ) ) { control . markAsTouched ( ) ; let invalid = false ; ( control . value || [ ] ) . forEach ( ( email ) => { if ( email && ! EmailValidator . isValid ( email , pattern ) ) { invalid = true ; } } ) ; if ( invalid ) { control . setErrors ( { invalid : true } ) ; return { emailValid : false } ; } } else if ( isEmpty ( control . value ) ) { control . setErrors ( { required : true } ) ; return { emailValid : false } ; } control . setErrors ( null ) ; return null ; } static isValid ( email , regExp ) { try { return new RegExp ( regExp ) . test ( email ) ; } catch ( TypeError ) { return false ; } } }