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

validation-engine

v0.3.3

Published

A promise-based validation engine for Node.js.

Downloads

72

Readme

node-validation-engine

This is a validation engine for Node.js. It is not intended to have every possible validation rule in the core, but it is built in such a way that you can easily add external rules.

The main advantage of this module is that you can asynchronously validate every rule of your Model at once:

  People.prototype.save = function(data){
    this.validate(data).then(function(){
      //save people data
    }
    .catch(function(validation_error){
      //some validation rule was not satisfied
    }
  }

It is my first module, so contributions and hints are very welcome!

Installation

Use

  npm install validation-engine

Configuring Validation Rules for a Model

You can use directly the rules property of the validator object:

  var validator = require('validator-engine');
  
  validator.rules = [
    {
      'field' : 'email',
      'rules' : [
        'required',
         {
          //these parameters are passed to the validator function as arguments
          'rule' : 'maxLength',
          'length' : 255,
          'message' : 'Maximum of 255 characters'
        },
        'email',
        //you can run database queries asynchronously
        'unique'  
      ]
    },
    {
      'field' : 'email_confirmation',
      'rules' : [{
        'rule' : 'equal',
        'fieldToCompare' : 'email'
      }]
    }
  ];

obs: Check the core validators here.

Adding Custom Validators

It is very simple to add custom validators:

  var Promise = require('bluebird');

  validator.addValidator('equal', function(rule, field_name, data){
    return new Promise(function(resolve, reject){
      if (data[field_name] === data[rule.fieldToCompare]) return resolve();
      return reject(new validator.ValidatorException('error message', field_name, rule, data));
    });
  });

You can even extend the module object with all of your validation rules at once. At lib/myValidator.js. put

  var validator = require('validator-engine');
  
  //add your validators
  
  module.exports = validator;

So, at your model, you can use

  var validator = require('../lib/myValidator');

Conditional Validation

You may choose to check certain validation rules only when inserting or updating data, using the on option in the rule

  validator.rules = [
    {
      'field' : 'email',
      'rules' : [{
        'rule' : 'required',
        'on' : 'create'
      }]
    }
  ];

The accepted values for on are create and update. The module checks if the current operation is create or update looking for the primary option.

If data[validator.primary] is present, the operation is update; otherwise, it is create. The default value for primary is id, but you can change it to any value you need.

  validator.primary = 'email';

Core validators

There are some validators in the core. All the core validators accept undefined values, except, of course, required. They all have default error messages, but you are encouraged to customize them. To do it, use the message parameter.

  • length validate the string.length property. Parameters:
    • length (optional) if set, string.length must match its value;
    • minLength (optional) if set, string.length must be greater or equal to its value;
    • maxLength (optional) if set, string.length must be less or equal to its value;

You can combine minLength and maxLength in the same rule, in such a way that string.length must be between minLength (inclusive) and maxLenght (inclusive).

  • required check if typeof data[field_name] is not undefined.
  • alphanumeric - check if the field has only letters and numbers. Optionally, this rule may accept whitespaces and underscores too. Parameters:
    • whitespace (default : false) if true, whitespaces are allowed.
    • underscore (default : false) if tre, underscores are allowed.
  • regExp check if the field match a regular expression. Parameters:
    • reg_exp (required) - regular expression which the field should match.