Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation library for Node.js [closed]

Is there a good validation framework for node.js that validates a variable for:

  • if its a type of String, Date, Number etc
  • max and min length
  • email, phone
  • etc...
like image 478
ajsie Avatar asked Nov 03 '10 15:11

ajsie


People also ask

How do you validate data in node JS?

js file inside the helper folder as seen below: const Validator = require('validatorjs'); const validator = async (body, rules, customMessages, callback) => { const validation = new Validator(body, rules, customMessages); validation. passes(() => callback(null, true)); validation.

What is Joi NodeJS?

Joi allows us to create blueprints of Javascript objects that ensure that we process and ultimately accept accurate data. This article will explore the use cases of Joi and how to apply it to your Node projects.

What is express validator in node JS?

According to the official website, Express Validator is a set of Express. js middleware that wraps validator. js , a library that provides validator and sanitizer functions. Simply said, Express Validator is an Express middleware library that you can incorporate in your apps for server-side data validation.

What is express validator NPM?

express-validator is a set of express. js middlewares that wraps validator. js validator and sanitizer functions.


2 Answers

I recently discovered node-validator by chriso.

Example

var check = require('validator').check,     sanitize = require('validator').sanitize  //Validate check('[email protected]').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>' 
like image 144
Baggz Avatar answered Sep 24 '22 02:09

Baggz


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

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

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

Current 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 
like image 25
Parris Avatar answered Sep 27 '22 02:09

Parris