[ACCEPTED]-Validation library for Node.js-node.js

Accepted answer
Score: 88

I recently discovered node-validator by chriso.

Example

var check = require('validator').check,
    sanitize = require('validator').sanitize

//Validate
check('test@email.com').len(6, 64).isEmail();       //Methods are chainable
check('abc').isInt();                               //Throws 'Invalid integer'
check('abc', 'Please enter a number').isInt();      //Throws 'Please enter a number'
check('abcdefghijklmnopzrtsuvqxyz').is(/^[a-z]+$/);

//Sanitize / Filter
var int = sanitize('0123').toInt();                  //123
var bool = sanitize('true').toBoolean();             //true
var str = sanitize(' \s\t\r hello \n').trim();      //'hello'
var str = sanitize('aaaaaaaaab').ltrim('a');        //'b'
var str = sanitize(large_input_str).xss();
var str = sanitize('&lt;a&gt;').entityDecode();     //'<a>'

0

Score: 15

I wanted ruby on rails and cakephp style 12 validations. I knew it was something I would 11 use over and over so I made this quick npm 10 module: https://npmjs.org/package/iz

It reads semantically like well 9 like jasmine, and can be used client or 8 server side. This means it comes with support 7 for commonjs and amd along with validation 6 rules passed in via JSON.

It is pretty well 5 unit tested, it has no production dependencies, and 4 the scope is locked down to just validation. We 3 seem to have a small community going now. Ideas, feedback 2 and pull requests are all welcome.

Current 1 library functions:

iz.alphaNumeric(*);               // Is number or string(contains only numbers or strings)
iz.between(number, start, end);   // Number is start or greater but less than or equal to end, all params numeric
iz.blank(*);                      // Empty string, undefined or null
iz.boolean(*);                    // true, false, 0, 1
iz.cc(*);                         // Luhn checksum approved value
iz.date(*);                       // Is a data obj or is a string that is easily converted to a date
iz.decimal(*);                    // Contains 1 decimal point and potentially can have a - at the beginning
iz.email(*);                      // Seems like a valid email address
iz.extension(ob1, ob2);           // If obj2's methods are all found in obj1
iz.fileExtension(arr, value);     // Checks if the extension of value is in arr. An obj can be provide, but must have indexOf defined.
iz.fileExtensionAudio(value);     // Check against mp3, ogg, wav, aac
iz.fileExtensionImage(value);     // Check against png, jpg, jpeg, gif, bmp, svg, gif
iz.inArray(arr, value);           // If * is in the array
iz.int(*, bool (optional));       // Is an int. If the 2nd variable is true (false by default) a decimal is allowed
iz.ip(str);                       // str resembles an IPV4 or IPV6 address
iz.minLen(val, min);              // val (str or arr) is greater than min
iz.maxLen(val, max);              // val (str or arr) is shorter than max
iz.multiple(num, mult);           // Number is multiple of another number
iz.number(*);                     // Is either an int or decimal
iz.ofType(obj, typeName);         // If it is a named object, and the name matches the string
iz.phone(str, canHaveExtension?); // Is an american phone number. Any punctuations are allowed.
iz.postal(*);                     // Is a postal code or zip code
iz.ssn(*);                        // Is a social security number
Score: 10

Node-validator is a library of string validation, filtering 3 and sanitization methods.

So if you want 2 to have better support for Numbers and Arrays, you 1 may try Chai.js. Here's some examples:

var expect = require('chai').expect;
try {
    expect([1, 2, 3]).to.have.length.below(4);
    expect(5).to.be.within(3,6);
    expect('test').to.have.length(4);
} catch (e) {
    // should not occur
}
Score: 6

I gather that this is the sort of thing 5 that the schema module is meant to do. Note that 4 it is labeled as being "in development" (tagged 3 as v0.1a). I haven't tried it myself, but 2 it looks pretty good from the examples shown 1 in the README.

Score: 3

Not on a variable level, but on a function 9 argument level:

http://github.com/torvalamo/argtype.js

Date currently needs to pass 8 as type 'object'. It is definately something 7 that I have forgotten, and will put on the 6 todo-list. ;)

Specific max and min length 5 is not supported, and will probably not 4 be implemented (but who knows). Email, phone 3 and all that can be checked by regex. See 2 the example on the github page, which includes 1 a (simple) regex example.

Score: 0

I recommend valida there is lack of documentation 2 however it is pretty simple to understand 1 looking at the examples.

Valida features are:

  • Sanitization
  • Synchronous and asynchronous validation
  • Groups
  • Extensible

More Related questions