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

the-vladiator

v1.3.5

Published

Javascript validation and assertion library for checking incoming parameters.

Downloads

51

Readme

The Vladiator

Build Status

Javascript validation and assertion library. Suggestions are welcome.

Includes Typescript definitions.

Installation

You can grab the latest version via NPM or Bower.

npm install --save the-vladiator
bower install --save the-vladiator

Then either use require through NodeJS.

var validate = require('the-vladiator');
validate('[email protected]')

Or add as a script to your HTML.

<script src="vladiator.js"></script>
validate('[email protected]')

Getting Started

Below are some basic examples of usage.

var validate = require('the-vladiator');

//E-mail address
validate('[email protected]').isRequired().isString().isEmail().didPass(); //returns true if passed validation

//Name
validate('John').isRequired().isString().notEmpty().didFail(); //returns true if failed validation

//Positive number
validate(5).isRequired().isNumber().isPositive().throws('Number must be positive'); //throws if failed validation

Type Checks

You can test whether your input is a certain type.

.isBool()
.notBool()

.isTrue()
.notTrue()

.isFalse()
.notFalse()

.isNumber()
.notNumber()

.isString()
.notString()

.isDate()
.notDate()

.isArray()
.notArray()

.isObject()
.notObject()

//Checks for an array or object
.isParent()
.notParent()

Equality Checks

//Checks for equality using ===

.isEqual(comparison)

.notEqual(comparison)

//Check contents using indexOf

.contains(comparison)

.notContains(comparison)

Numeric Checks

.isPositive()
.isNegative()

.higherThan(amount)
.lowerThan(amount)

Length Checks

.isEmpty()
.notEmpty()

.hasLength(length)

.longerThan(length)
.shorterThan(length)

Optional Checks

//Ensures that value is defined, not null and not NaN
.isRequired()

//Skips remaining checks if value is undefined, null or NaN
.isOptional()

//Ensures the value is defined
.valueRequired()

//Skips remaining checks if value is undefined
.valueOptional()

Object Checks

.missingKey(key)
.hasKey(key)

.missingValue(value)
.hasValue(value)

.missingKeyValue(key, value)
.hasKeyValue(key, value)

Nested Checks

//The value can be replaced with a nested object one or more levels deeper
.open('key')
.open('firstKey.secondKey')

//Iterates checking inside arrays or objects, all subsequent checks will be performed on each
.extract()
.extract('key')
.extract('firstKey.secondKey')

//Iterates checking through all nested levels inside arrays or objects
.recursive()

Custom Checks

//Validates whether input is pie
.is(function(value){
	return value === Math.PI;
});

Other Checks

.isEmail(email)
.notEmail(email)

//Checks for MongoDB ObjectIds
.isMongoId()
.notMongoId()

//Checks whether value is an Enum.
//Arguments are a mongoose model or schema and property name string
.isMongoEnum(type, field)
.notMongoEnum(type, field)

//Checks whether value confirms to semver
.isSemver(version)
.notSemver(version)

Other Stuff

//Replaces the currently checking value
.and(newValue)

//Inverts the following checks result
.not()

//Changes values case
.upperCase()
.lowerCase()

Advanced Examples

We can compose some pretty lengthy validation rules as demonstrated below though it's recommended to split them into smaller more readable chunks.

Example One

validate('myPassword').isString().longerThan(6).shorterThan(20).notContains('password').notEmail().throw('Enter a strong password')

Example Two

var data = {
	usernames: [
		'User1',
		'User2',
		'User3',
		'User4',
		'User5'
	],
	emails: [
		'[email protected]',
		'[email protected]',
		'[email protected]',
		'[email protected]',
		'[email protected]'
	]
};

validate(data).hasKey('usernames').hasKey('emails')
	.extract('usernames').isString().notEmpty()
	.and(data)
	.extract('emails').isString().isEmail()
	.throw('failed');

Example Three

var database = {
	users: {
		metadata: {},
		ids: [
			'507f1f77bcf86cd799439011',
			'507f1f77bcf86cd799439012',
			'507f1f77bcf86cd799439013',
			'507f1f77bcf86cd799439014',
			'507f1f77bcf86cd799439015',
			'507f1f77bcf86cd799439016',
			'507f1f77bcf86cd799439017',
			'507f1f77bcf86cd799439018',
			'507f1f77bcf86cd799439019',
			'507f1f77bcf86cd799439020',
			'507f1f77bcf86cd799439021'
		]
	}
};

validate(database).extract('users.ids').isMongoId().is(function(value){

	//Check whether exists in database
	var exists = true;
	return exists;

}).throw('failed');