import { isBlank } from './utils' ; export interface Validation { required ? : boolean ; minLength ? : number ; maxLength ? : number ; pattern ? : string | RegExp ; } export class Validators { static get ( validation ) { const result = ( name , value ) => Validators . validate ( name , value , validation ) ; return result ; } static validate ( name , value , validation ) { const temp = [ ] ; if ( ! validation ) { return temp ; } const length = ! isBlank ( value ) ? value . toString ( ) . length : 0 ; if ( validation . required && isBlank ( value ) ) { temp . push ( `${ name } is required.` ) ; } if ( validation . minLength && length < validation . minLength ) { temp . push ( `${ name } has to be at least ${ validation . minLength } characters long. ActualLength: ${ length }` ) ; } if ( validation . maxLength && length > validation . maxLength ) { temp . push ( `${ name } can't be longer then ${ validation . maxLength } characters. ActualLength: ${ length }` ) ; } if ( validation . pattern && value ) { const patternResult = Validators . patternValidate ( name , value , validation . pattern ) ; if ( patternResult ) { temp . push ( patternResult ) ; } } return temp ; } static patternValidate ( name , value , pattern ) { let regex ; let regexStr ; if ( typeof pattern === 'string' ) { regexStr = pattern ; regex = new RegExp ( regexStr ) ; } else { regexStr = pattern . toString ( ) ; regex = pattern ; } return regex . test ( value ) ? null : `${ name } must match this pattern: ${ regexStr }.` ; } }