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 🙏

© 2025 – Pkg Stats / Ryan Hefner

validate-obj

v0.8.5

Published

simple way to validate object

Readme

What is validate-obj?

validate-obj is a javascript validation framework, easy to validate object or form, and is very easy to extend. validate-obj mainly for node.js server-side, but also work for front-end.

Build Status

Why I create validate-obj?

I started this project just because I cannot find existing one fits my requirements. There are many other validation projects, but many of them is very easy to end up code like the following:

var validator = require('validator');
var myUserForm = {
	name: 'John', // required, string
	email: '[email protected]', // required, email
	age: 27, // int
	gender: 'male', // male or female
	createdOn: '10/12/1987'
};

//Start validating
var errs = [],
if (typeof myUserForm.name === undefined) {
	errs.push('name is required');
}
else if (!validator.isString(myUserForm.name)) {
	errs.push('name must be a string');
}

if (typeof myUserForm.email === undefined) {
	errs.push('email is required');
} else if (myUserForm.email !== undefined && !validator.isEmail(myUserForm.email)) {
	errs.push('email is invalid');
}

if (typeof myUserForm.age !== undefined && !validator.isNumber(myUserForm.age) {
	errs.push('age is invalid');
}

if (typeof myUserForm.gender !== undefined && !validator.isIn(myUserForm.gender, ['male', 'female']) {
	errs.push('gender is invalid');
}

Are you tired of the above? How about we just simply do the following?

var v = require('validate-obj');
var myUserForm = {
	name: 'John', // required, string
	email: '[email protected]', // required, email
	age: 27, // int
	gender: 'male', // male or female
	createdOn: '10/12/1987'
};
var validationExpression = {
   name: [v.required, v.isString],
   email: [v.required, v.isEmail],
   age: v.isNumber,
   gender: v.isIn(['male', 'female']),
   createdOn: v.isDate
};

var errs = v.hasErrors(myUserForm, validationExpression);

It is beautiful, isn't it? and even we save this validation sytax object and reuse it.

How to install?

For node.js

npm install validate-obj

For front end (coming soon)

bower install validate-obj

APIs

Overview

var v = require('validate-obj'); //load the library, to make it short, I will not include it in the following examples

// target: the object going to be validated
// validationExpression: the validation defination indicating how to validate
// return value: null means no errors or array of error string
v.hasErrors(target, validationExpression)

To validate simple variables

v.hasErrors(1, v.isNumber) // ==> null
v.hasErrors('a', v.isNumber) // ==> ['it is not number']
v.hasErrors('male', v.isIn(['male', 'female'])) // ==> null
v.hasErrors('middle', v.isIn(['male', 'female'])) // ==> ['it must be one of (male, female)']

To override the error message

v.hasErrors('a', v.isNumber('age should be a number')) // ==> ['age should be a number']
v.hasErrors('middle', v.isIn(['male', 'female'], 'gender has to be male or female')) // ==> ['gender has to be male or female']
v.hasErrors('middle', v.isIn('gender has to be male or female', ['male', 'female'])) // ==> ['gender has to be male or female']

Validate with multiple validators

v.hasErrors('a', [v.required, v.isString])) // ==> null
v.hasErrors(undefined, [v.isRequired, v.isNumber]) // ==> ['it is required']

To validate array

v.hasErrors('a', [[v.isString]]) // ==> it is not array
v.hasErrors(['a', 'b'], [[v.isString]]) // ==> null

To validate object

v.hasErrors({name: 'john', age: 27}, {name: v.isString, age: v.isNumber}) // ==> null
v.hasErrors({name: 'john', age: '27'}, {name: v.isString, age: v.isNumber}) // ==> ['it.age is not number']

To validate complex object

v.hasErrors(
	{
		name: 'john', 
		orderNumber: 12345, 
		items: [{sku: 222, quantity: 2}]
	},
	{
		name: v.isString, 
		orderNumber: v.isNumber, 
		items: [{ sku: v.isNumber, quantity: v.isNumber}]
	}) // ==> null
	
v.hasErrors(
	{
		name: 'john', 
		orderNumber: 12345, 
		items: [{sku: '123', quantity: 2}]
	},
	{
		name: v.isString, 
		orderNumber: v.isNumber, 
		items: [{ sku: v.isNumber, quantity: v.isNumber}]
	}) // ==> ['it.items[0].sku is not number']

Validate logic which have multiple properties involved

	var schema = {
		password: [v.isString, v.required],
		matchedPassword: [v.isString, v.required],
		selfCrossValidators : function(obj) {
			if (obj.password !== obj.matchedPassword) return 'passwords do not match';
			return undefined;
		}
	};
	v.hasErrors({password:'123', matchedPassword: '123'}, schema) // => null
	v.hasErrors({password:'123', matchedPassword: 'a23'}, schema) // => ['passwords do not match']

	// The selfCrossValidators can also be array like the below
	var schema = {
        password: [v.isString, v.required],
        matchedPassword: [v.isString, v.required],
        selfCrossValidators : [function(obj) {
            if (obj.password !== obj.matchedPassword) return 'passwords do not match';
            return undefined;
        }]
    };

Extend your custom validators

  • With auto error message
v.register('isGender', 
	function(value) {
		return value ==='male' || value ==='female'
	}
);
v.hasErrors('male', v.isGender) // ==> null
v.hasErrors('middle', v.isGender) // ==> ['it is invalid']
  • With error message
v.register('isGender', v.build(
	function(value) {
		return value ==='male' || value ==='female';
	},
	function(name) {
		return  name + ' is not gender'
	}
));
v.hasErrors('male', v.isGender); // ==> null
v.hasErrors('middle', v.isGender) // ==> ['it is not gender']

Built-in validators

  • required
  • isDate
  • isBool
  • isString
  • isNumber
  • isEmail
  • isCreditCard
  • isIn
  • minLength
  • maxLength
  • isUrl
  • isBefore
  • isAfter
  • isObject

Progress

The whole framework is done, the only missing is not enough built-in validators. I will add more soon.