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

data-inspector

v0.5.0

Published

A simple library to make easy validating Objects in Javascript

Downloads

2

Readme

Data Inspector NPM version NPM downloads Build Status

A simple library to make easy validating Objects in Javascript.

How to install: npm install data-inspector

Example

var Inspector = require('data-inspector');

var data = {
	name: 'Squirtle',
	description: 'A small Pokémon.',
	type: 'water',
	another: {
		nested: {
			field: 'should be a number'
		}
	}
};

var rules = {
	name: { type: 'string', alpha: true, minLength: 5 },
	description: { type: 'string', minLength: 20 },
	type: { type: 'string', pattern: /^(fire|water|grass)$/ },
	'another.nested.field': { type: 'number' }
};

var messages = {
	description: {
		type: 'The Pokémon description must be a text.',
		minLength: 'The Pokémon description must have at least length of 20.'
	}
};

Inspector.verify(data, rules, messages);

///outputs:
//[
//	{field: 'description', message: 'The Pokémon description must have at least length of 20.'},
//	{field: 'another.nested.field', message: 'The attribute `another.nested.field` must be of type number'}
//]

Built-in validation types

required

The attribute is required

type

The attribute must be of a given type. Accepted values are string, array, number or boolean.

pattern

The attribute must match a given regex.

exactLength

The attribute must have the exact given length.

minLength

The attribute must have a minimum given length.

maxLength

The attribute must have a maximum given length.

lessThan

The attribute must have a value less than the rule.

lessOrEqualThan

The attribute must have a value less than or equal than the rule.

greaterThan

The attribute must have a value greater than the rule.

greaterOrEqualThan

The attribute must have a value greater than or equal than the rule.

equal

The attribute must have a value equal the rule.

notEqual

The attribute must not have a value equal the rule.

accepted

The attribute must be 'yes', 'on', 1 or true.

afterDate

The attribute must be a valid date after the one passed on the rule.

beforeDate

The attribute must be a valid date before the one passed on the rule.

equalDate

The attribute must be a valid date equal the one passed one the rule.

equalOrBeforeDate

The attribute must be a date before or equal the one passed one the rule.

equalOrAfterDate

The attribute must be a date after or equal the one passed one the rule.

betweenDates

The attribute must be a date between two dates. Accepted rule value is an array which the first value is the initialDate and the second is the finalDate.

alpha

The attribute must be entirely alphabetic characters.

alphaDash

The attribute may have alpha-numeric characters, as well as dashes and underscores.

alphaNumeric

The attribute must be entirely alpha-numeric characters.

email

The attribute must be formatted as an email address.

url

The attribute must be formatted as an url.

ip

The attribute must be formatted as an IP address.

between

The attribute must have a value between two values. Accepted rule value is an array which the first value is the initialValue and the second is the finalValue.

betweenStrict

The same of above rule, but using strict validation.

digits

The attribute must be a number and has a value where its digits matches the value on the rule.

distinct

The attribute must be an array and all its values must be different.

in

The attribute must be included in the given array.

notIn

The attribute must not be included in thee given array.

requiredWith

The attribute must be present only if at least one of the attributes in array are present.

requiredWithout

The attribute must be present only if at least one of attributes in array are not present

requiredIf

The attribute must be present only if the function passed in requiredIf returns true

Note: For a solid example of each validator, please have a look at lib/data-inspector.spec.js

Custom rules

Ok, we have a bunch of validators, but let's suppose you want a very specific rule. You totally can! Here's how:

var data = {
	name: 'Pikachu',
	type: 'eletric',
	description: 'Useless Pokemon.'
};

var rules = {
	type: {
		theBest: function(schema, field, value){
			return value === 'water' && value !== 'eletric';
		}
	}
};

var messages = {
	type: {
		theBest: 'The Pokémon must be of type water, not eletric.'
	}
};

Inspector.verify(data, rules, messages) //[{field: 'type', message: 'The Pokémon must be of type water, not eletric.'}]

Note: if you add a custom rule that already exists in the validators above, the default one will be used.

Minimal mode

Default validation returns an array of object with invalid fields. But, let's say you need just a plan object with the first error of each attribute. You can use {minimal: true} as the forth parameter of verify method.

var data = {
	id: 'Something',
	name: 'Squirtle',
	type: 'water',
	another: {
		nested: {
			field: 'should be a number'
		}
	}
};

var rules = {
	name: { type: 'string', alpha: true, minLength: 5 },
	id: { type: 'number', lessThan: 151 },
	type: { type: 'string', pattern: /^(fire|water|grass)$/ },
	'another.nested.field': { type: 'number' }
};

var messages = {
	id: {
		type: 'The Pokémon id must be a number.',
		equal: 'The Pokémon id must be less than 151.'
	}
};

Inspector.verify(data, rules, messages, { minimal: true })
// returns:
//
//{
//	'id': 'The Pokémon id must be a number.',
//	'another.nested.field': 'The attribute `another.nested.field` must be of type number'
//}