Validation made Ez
Who said Validation has to be hard and annoying!
https://medium.com/@27amad27/validation-made-ez-50e72d6a0b29
API
Function | Params | Description |
---|---|---|
required | errorMessage | checks if value is empty or not |
isString | errorMessage | checks if value is a STRING |
isBoolean | errorMessage | checks if value is a BOOLEAN |
isNumber | errorMessage | checks if value is a INT |
isWholeNumber | errorMessage | checks if value is a Whole Number |
isObject | errorMessage | checks if value is an OBJECT |
isEmpty | errorMessage | checks if value is an EMPTY |
isAlphanumeric | errorMessage | checks if value is Alphanumeric |
isEmail | errorMessage | checks if value is valid Email |
isPhoneNumber | errorMessage | checks if value is valid Phone Number |
isUSAZipCode | errorMessage | checks if value is valid USA Zip Code |
maxLength | maxNumber, errorMessage | checks if value exceeds max length |
minLength | minNumber, errorMessage | checks if value is below min length |
maxValue | maxNumber, errorMessage | checks if value exceeds max number value |
minValue | minNumber, errorMessage | checks if value is below min number value |
customRegex | regex, errorMessage | pass in a custom regex |
customValidation | cb, errorMessage | pass in a custom call back that returns a Boolean False if there is an error True is there is not an error |
Examples
; val = "asdf";errorMessage;// output = "Please enter valid response" val = 123; errorMessage;// output = "Too large" val = "I am a large string"; maxLength4 errorMessage;// output = "Exceeds maximum length of 4" val = "I am a large string"; maxLength4 errorMessage;// output = "DEFAULT ERROR"
Schema Based Validation
If you want to validate using a schema
; const values = noValidation: "hi" email: "fake-email" name: "im a cow"; const schema = errorMessage maxLength2errorMessage;;/* output: { email: "not a valid email", name: "name length too long"}*/
; const values = noValidation: "hi" email: "fake-email" name: "im a cow"; const schema = email: errorMessage name: maxLength2errorMessage ;;/* output: { email: "not a valid email", name: "name length too long"}*/
Advance Custom Usage
If you have a validation method that isn't provided thats used often you can extend the ValidationAPI class to add your own methods!
; { if thisvalidating == 0 this; return this; }0errorMessage;// "value can't be 0"