npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

friendly-validator

v0.0.2

Published

A Node.JS validation library using node-validator, but with user-friendly validation error messages.

Downloads

12

Readme

Build Status

###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:

  1. JSON to validate
  1. Rules to apply

Returns:

  • if all of the values passed in pass all of their validation rules, err will equal false.
  • 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: '[email protected]'}, 'isEmail');

var data = {
  email: '[email protected]',
  password: 'pass1234',
  phone: '5064555555'
};

var err = validate([
  {value: data.email, rule: 'isEmail'},
  {value: data.phone, rule: 'isPhone'},
  {value: data.password, rules: ['minLength(8)', 'maxLength(16)']}
]);

Examples of implementation 1

Validate 1 value, which passes

var email = '[email protected]';
var err = validate({value: email, rule: 'isEmail'}); // email passes this validation test
console.log(err); // '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 = validate({value: phone, rule: 'isMinLength(25)'}); // phone fails this validation test
console.log(err); // '[{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 = '[email protected]';
var name = 'foo bar';
var agreed_to_terms = true;

var err = validate([
  {value: phone, rules: ['length(10)', 'isNumeric']}, // passes all validations
  {value: email, rules: ['isEmail', 'isUnique']}, // passes all validations
  {value: name, rule: 'minLength(3)'}, // passes validation
  {value: agreed_to_terms, rule: 'isTrue'} // passes validation
]);
console.log(err); // 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 = validate([
  {value: phone, rules: ['length(10)', 'isNumeric']}, // fails all validations
  {value: email, rules: ['isEmail', 'isUnique']}, // fails all validations
  {value: name, rule: 'minLength(3)'}, // fails validation
  {value: agreed_to_terms, rule: 'isTrue'} // fails validation
]);
console.log(err); // 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