friendly-validator.js
A Node.JS validation library using node-validator, but with user-friendly validation error messages.
Here's a sample validation error sent to my clientside right now in Sails apps. Many (most) ORMs all return very "programmer friendly" errors, but not "user friendly" errors.
This is bad because it requires the programmer to "convert" the programmer-errors into user-friendly errors before displaying them to the user, and that sucks. More work sucks.
The goal of this library is:
- to be able to toss some form data (JSON key/value pairs) into a validate() function where you specify some simple rules (isEmail, isPhone, isCreditCard)
- IF there are errors: to have it return a big list of plain-english errors that you can send back to the clientside, and output immediately, without having to write logic to convert "programmer errors" into "user friendly errors".
- IF there are NO errors: to return false, so we can do the simple and familiar
if (err) doSomeErrorThing()
syntax
Before (ugly and needs to be plain-english-ified before the user can see it, E.G. more work)
"invalidAttributes": "password": "rule": "numeric" "message": "`undefined` should be an integer (instead of \"null\", which is a object)" "rule": "minLength" "message": "\"minLength\" validation rule failed for input: null" "email": "rule": "required" "message": "\"required\" validation rule failed for input: null"
After (pretty and sendable to user immediately)
password: 'Your password must contain only numbers.' password: 'Your password must be at least 4 characters long.' email: 'Email is a required field.'
Proposed Implementation 1:
Function:
- validate()
Parameters:
- JSON to validate
- Rules to apply
Returns:
- if all of the values passed in pass all of their validation rules,
err
will equalfalse
.- if ANY of the values passed in fail their test, a plain english array of
input_that_contains_error/plain_english_error_message
key/value pairs will be returned
var err = validate(someJSONObject, ['someRuleToApply', 'someOtherRuleToApply']);
var err = validate({email: 'foo@bar.com'}, 'isEmail');
var data = email: 'foo@bar.com' password: 'pass1234' phone: '5064555555'; var err = ;
Examples of implementation 1
Validate 1 value, which passes
var email = 'foo@bar.com';var err = ; // email passes this validation testconsole; // 'false'// because all values passed all validations, there is no err, continue to next code
Validate 1 value, which fails validations... and returns a simple { input_that_contained_error: 'error message' }
for the failed validation rule.
var phone = '5064555555';var err = ; // phone fails this validation testconsole; // '[{phone: 'Phone must be at least 25 characters long.'}]'// because it failed the validation, there is no err. So what we get back is a very simple "Heres what// input has the error so we know which input to make all red and scary, and heres the error message to// show below it explaining what the problem was" input/error JSON object
Validate an entire form of data, which all pass validations
var phone = '5064555555';var email = 'foo@bar.com';var name = 'foo bar';var agreed_to_terms = true; var err = ;console; // false, all values passed all validations
Validate an entire form of data, which all fail validations... and returns a simple array containing a { input_that_contained_error: 'error message' }
for every failed validation rule.
var phone = 'aaaaf3af4f4fa6g5asg453vesfa';var email = 'totallyNotAnEmail';var name = 'hi';var agreed_to_terms = false; var err = ;console; // see lines below for full output...// [ {phone: 'Phone must be exactly 10 characters long.'},// {phone: 'Phone must only contain numbers.'},// {email: 'That isn't a properly formatted email address.'},// {email: 'That email is already taken.'},// {name: 'Your name must be at least 3 characters long.'},// {agreed_to_terms: 'Agreed to Terms must be true.'} ] // now on the client side, all you do is loop through the returned array, attaching the class of "error"// to any inputs whose "name" attribute matches what the server sent back, and you add the actual error// message itself just below the input