New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

batch-validator

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

batch-validator

Create challenges and validate values against them

  • 0.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
decreased by-20%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

FAQs

Package last updated on 13 Sep 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc