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

batch-validator

v0.0.3

Published

Create challenges and validate values against them

Downloads

30

Readme

batch-validator

Create challenges and validate values against them

#####features

  • register challenges with regexp's or strict matching values
  • supports custom error handler for detailed error message and related data
  • can validate one or multiple values at once
  • very type safe

###usage

npm install --save batch-validator

###example, can run


var Validator= require( 'batch-validator' );

// can pass console.log as error handler, or any custom you prefer
var validator= new Validator( console.log );

// add a single one
validator.add( 'number': /^[0-9]+$/ );

// or add multiple at once
validator.add({
	 hex		: /^[0-9A-Fa-f]+$/
	,name		: /^[A-Za-z]{1,7}$/
});

// add a strict comparison, will only match 11 with type Number
validator.add( 'my-number', 11 );


// use a registered key to validate a value
var validated= validator.validate( 'number', 33 );
console.log( validated );
// true

validated= validator.validate( 'my-number', '11' );
console.log( validated );
// false

// validate multiple values of different types at once
// passing true as second argument to continue validating all values after
// on or more values didn't pass
validated= validator.validate([
	 { number	: 33 }
	,{ number	: 42 }
	,{ name		: 'hey!' }
	,{ hex		: 0 }
], true );
// value: "hey!" did not pass name! { key: 'name', value: 'hey!', regexp: /^[A-Za-z]{1,7}$/ }
console.log( validated );
// false

###usage details

All examples use the context created below.

var
	 Validator	= require( 'batch-validator' )
	 // can pass console.log as error handler, or any custom you prefer
	,validator	= new Validator( console.log )
;

#####setErrorHandler

<this> setErrorHandler( <function> callback )

The callback set with setErrorHandler will be called on any error that occurs.

// let console.log be the error handler
validator.setErrorHandler( (err, obj) => {
	console.log( 'error:', err );	
	console.log( obj );	
});

#####hasKey

<boolean>hasKey( <string> key )

Returns true if the key is added to the context object, or false if not.


#####add

<boolean> add( <object>/<string> obj, <regexp> regexp )

Adds a single validation object or multiple at once

// add a single one
validator.add({ number: /^[0-9]+$/ });

// or add multiple at once
validator.add({
	 number		: /^[0-9]+$/
	,hex		: /^[0-9A-Fa-f]+$/
	,name		: /^[A-Za-z]{1,7}$/
});


// non RegExp values will be used for strict value && type comparison
var account= new Validator;
account.add( 'john25', '?kIWs-45.oQsN[/' );
console.log( account.validate('john25', '?kIWs-45.oQsN[/') );
// true

// returns false if type is not equal
validator.add( 'my-number', 11 );
console.log( validator.validate('my-number', '11') );
// false

#####validate

<boolean>/<null> validate( <array>/<string> validations, <string>/<number> value )

Validate one or multiple values at once. If validations is a collection of validations the second argument can be set to true to continue validating al remaining validations after one or more validations didn't pass.

validate returns true or false for regexp pass or fail, but will return null if a type error occurs or a key has not been registered with add.

// single
var validated= validator.validate( 'number', 0 );

// multiple
var validated= validator.validate([
	 { number	: 33 }
	,{ number	: 42 }
	,{ name		: 'hey!' }
	,{ hex		: 0 }
]);

#####validations

<object> validations

Context property holding all validation key's


###change log

#####0.0.1

  • first commit

###license

MIT