import { PasswordValidation } from "../models/password-validation.model" ; import { RegExRule } from "./regex-rules" ; const ALPHABET = "alphabet" ; const DIGIT = "digit" ; const CONTAINS = "contains" ; const LOWERCASE = "lowerCase" ; const UPPERCASE = "upperCase" ; const SPECIAL_CHARACTER = "specialCharacter" ; const MIN_LENGTH = "minLength" ; const MAX_LENGTH = "maxLength" ; export class RegexValidator { static isExits ( value , regex ) { return value . match ( regex ) != null ; } static isValid ( value , regex ) { return regex . test ( value ) ; } static isNotBlank ( value , isRemoveSpace = false ) { return ! isRemoveSpace ? ( value === 0 ) || ( value !== undefined && value !== null && value !== "" ) : ( value === 0 ) || ( value !== undefined && value !== null && String ( value ) . trim ( ) !== "" ) ; } static isValidPassword ( passwordValidation , value ) { let isValid = false ; let jObject = { } ; let keyName = "status" ; let objectProperties = Object . getOwnPropertyNames ( passwordValidation ) ; for ( let propertyName of objectProperties ) { switch ( propertyName ) { case ALPHABET : isValid = RegexValidator . isExits ( value , RegExRule . alphaExits ) ; keyName = ALPHABET ; break ; case DIGIT : isValid = RegexValidator . isValid ( value , RegExRule . isDigitExits ) ; keyName = DIGIT ; break ; case CONTAINS : isValid = value . indexOf ( passwordValidation [ CONTAINS ] ) != - 1 ; keyName = CONTAINS ; break ; case LOWERCASE : isValid = RegexValidator . isValid ( value , RegExRule . lowerCase ) ; keyName = LOWERCASE ; break ; case UPPERCASE : isValid = RegexValidator . isValid ( value , RegExRule . upperCase ) ; keyName = UPPERCASE ; break ; case SPECIAL_CHARACTER : isValid = RegexValidator . isExits ( value , RegExRule . specialCharacter ) ; keyName = SPECIAL_CHARACTER ; break ; case MIN_LENGTH : isValid = value . length >= passwordValidation [ propertyName ] ; keyName = MIN_LENGTH ; break ; case MAX_LENGTH : isValid = value . length <= passwordValidation [ propertyName ] ; keyName = MAX_LENGTH ; break ; } if ( ! isValid ) break ; } return { isValid : isValid , keyName : keyName } ; } static isZero ( value ) { return value == 0 ; } static commaRegex ( ) { return new RegExp ( "," , "g" ) ; } }