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

supervalidation

v1.0.31

Published

Easy validation for your applications. Based on Laravel 3.x validation module

Downloads

9

Readme

supervalidation

Easy validation for your forms. Based on Laravel 3.x validation module

Installation

npm install --save supervalidation

Usage

The response could or could not be a promise. If you provide only synchronous methods (which be default all rule definitions is), there will be no promise at the end, but if you do, you will totally need to deal with a promise.

Example 1

var validator = new Validator({
	name: 'Victor Queiroz',
	email: '[email protected]'
}, {
	email: 'string|required|email'
});

if(validator.passes()) {
	// do some work
} else if (validator.fails()) {
	// do more work
}

Example 2

var Q = require('q');

/* There is no limit for parameters in your rule definition */

Validator.defineRule('unique', function (
	value /* Value typed in the field by the user */,
	attributeName /* Attribute name (in our case: "email") */,
	collection /* Param 1 */,
	attribute /* Param 2 */
) {
	var col = db.collection(collection);

	var deferred = Q.defer();

	col.findOne({
		email: value
	}, function (doc) {
		if(doc) {
			deferred.reject();
		} else {
			deferred.resolve();
		}
	});

	return deferred.promise;
});

controller.js

module.exports = {
	registerUser: function (req, res) {
		var rules = {
			email: 'required|unique:users,email'
		};

		var validator = new Validator(req.body, rules, {
			templatePath: 'my-custom-language-variables.js'
		});

		validator.validate().then(function () {
			// THE VALIDATION HAS NO ERRORS! :)

			db.collection('users').insertOne(req.body).then(function(user) {
				res.json(user);
			});
		}, function (err) {
			res.json(err.getMessages());
		});
	}
};

When you fire err.getMessages() it will return a bunch of messages which by default are in english, but you can change at any time your template.

Example 3 (validating complex objects)

var validator = new Validator({
	address: {
		streetNumber: 1234
	}
}, {
	'address.streetNumber': 'number|required'
});

if(validator.fails()) {
	return res.status(400).json(validator.getMessages());
}

// do the work

Changing your language variables

All the messages are consumed from a file, which by default are this. It just return a big object (just like Grunt), which symbolizes each RULE that is defined.

module.exports = {
	min: {
		numeric: 'The :attribute must be at least :min.',
		file: 'The :attribute must be at least :min kilobytes.',
		string: 'The :attribute must be at least :min characters.',
		array: 'The :attribute must have at least :min items.',
	},
}

If the rule is an object and not a string, it will make a check through the type of the value typed in the field, and match with the right one. And this can be used with any rule.

:attribute will always be replaced by the name of the attribute, and :min in this case will be replaced by the first argument of the rule usage, and you can use this for any rule (expect for those who has no parameters, like required)

module.exports = {
	myCustomRule: 'The :attribute must have at least :myCustomRule items.'
}

Changing your language variables template

Example 1 (Globally)

var path = require('path');
var Translator = require('supervalidator/translator');
Translator.setTemplatePath(path.resolve(__dirname, 'app/lang/en/validation.js'));

Example 2 (Privately)

var validator = new Validator(req.body, {
	email: 'required|unique:users,email'
}, {
	templatePath: 'my-custom-language-variables.js'
});

Predefined rule definitions

  • required
  • max:length
  • min:length
  • string
  • email
  • number
  • url

(Soon more)