import { ValidationFunction , Values } from './interfaces' ; import { isNumber , isString , isTypeUndefined , isValueNullOrUndefined , isValueStringEmpty , isValueUndefined , } from './utils' ; export function isExisty < V > ( value ) { return ! isValueNullOrUndefined ( value ) ; } export function isEmpty < V > ( value ) { if ( isString ( value ) ) { return isValueStringEmpty ( value ) ; } if ( isTypeUndefined ( value ) ) { return false ; } return isValueUndefined ( value ) ; } export function isDefaultRequiredValue ( value ) { return isString ( value ) ? isValueStringEmpty ( value ) : isValueNullOrUndefined ( value ) ; } export function matchRegexp < V > ( _values , value , regexp ) { return ! isExisty ( value ) || isEmpty ( value ) || regexp . test ( `${ value }` ) ; } interface Validations < V > { [ key ] : ValidationFunction < V > ; } const REGEX_PATTERNS = { ALPHA : /^[A-Z]+$/i , ALPHANUMERIC : /^[0-9A-Z]+$/i , EMAIL : /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/i , FLOAT : /^(?:[-+]?(?:\d+))?(?:\.\d*)?(?:[eE][+-]?(?:\d+))?$/ , INT : /^(?:[-+]?(?:0|[1-9]\d*))$/ , NUMERIC : /^[-+]?(?:\d*[.])?\d+$/ , SPECIAL_WORDS : /^[\sA-ZÀ-ÖØ-öø-ÿ]+$/i , URL : /^(?:\w+:)?\/\/([^\s.]+\.\S{2}|localhost[:?\d]*)\S*$/i , WORDS : /^[A-Z\s]+$/i } ; const validations = { equals : < V > ( _values , value , eql ) => ! isExisty ( value ) || isEmpty ( value ) || value === eql , equalsField : < V > ( values , value , field ) => value === values [ field ] , isAlpha : < V > ( values , value ) => matchRegexp ( values , value , REGEX_PATTERNS . ALPHA ) , isAlphanumeric : < V > ( values , value ) => matchRegexp ( values , value , REGEX_PATTERNS . ALPHANUMERIC ) , isDefaultRequiredValue : < V > ( _values , value ) => isDefaultRequiredValue ( value ) , isEmail : ( values , value ) => matchRegexp ( values , value , REGEX_PATTERNS . EMAIL ) , isEmptyString : ( _values , value ) => isEmpty ( value ) , isExisty : < V > ( _values , value ) => isExisty ( value ) , isFalse : ( _values , value ) => value === false , isFloat : < V > ( values , value ) => matchRegexp ( values , value , REGEX_PATTERNS . FLOAT ) , isInt : < V > ( values , value ) => matchRegexp ( values , value , REGEX_PATTERNS . INT ) , isLength : ( _values , value , length ) => ! isExisty ( value ) || isEmpty ( value ) || value . length === length , isNumeric : < V > ( values , value ) => isNumber ( value ) || matchRegexp ( values , value , REGEX_PATTERNS . NUMERIC ) , isSpecialWords : < V > ( values , value ) => matchRegexp ( values , value , REGEX_PATTERNS . SPECIAL_WORDS ) , isTrue : ( _values , value ) => value === true , isUndefined : < V > ( _values , value ) => isValueUndefined ( value ) , isUrl : < V > ( values , value ) => matchRegexp ( values , value , REGEX_PATTERNS . URL ) , isWords : < V > ( values , value ) => matchRegexp ( values , value , REGEX_PATTERNS . WORDS ) , matchRegexp , maxLength : ( _values , value , length ) => ! isExisty ( value ) || value . length <= length , minLength : ( _values , value , length ) => ! isExisty ( value ) || isEmpty ( value ) || value . length >= length } ; export const addValidationRule = < V > ( name , func ) => { validations [ name ] = func ; } ; export default validations ;