Socket
Socket
Sign inDemoInstall

validjs

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    validjs

A simple and quick validator implementation can also be used with forms


Version published
Weekly downloads
2
increased by100%
Maintainers
1
Install size
46.9 kB
Created
Weekly downloads
 

Readme

Source

Vjs

A Basic validator implementation, which checks if a value is valid and as expected and executes the corresponding callback function as needed, and at the end of the check if a callback for the whole test exists it will execute the corresponding callback.

Live example

Structure: validate.js exports two modules:

  • Validate <- for validation purposes
  • Checks <- the isBlah() utilities are here (for example isArray is in here).

Usage and Explanation & consedirations:

  • if the the check function fails
    • if there is no callback or string defined -> the return value will be false
    • if there is a callback or string defined -> the return value will be the string or the return of the function
  • if the the check function passes
    • if there is no callback or string defined -> the return value will be true
    • if there is a callback or string defined -> the return value will be the string or the return of the function
  • the return value of validate is an array.
  • If you have your own check functions there is no need to import the check functions.

Import the Vjs validate method and the check utils.

var v=require('../validate.js').Validate;
var c=require('../validate.js').Check;

The v object is instantiated when imported and now it can be used to call the validate method accompanied with the set method to set the validations.

result=v.validate([v.validate([c.isString('hello'), onFail, onPass ], [v.validate([c.isString(1984), onFail, onPass ], [outerFail,outerPass]);

onFail and onPass can be a string or a function. On each of the corresponding states either the string is returned or a return value from a function is retuned. After the validation finishes if there is a fail the outerFail will execute or if all is passed the outerPass is executed.

var result=v.validate([
                        c.isString('hello'),
	                    'failed is a string',
    	                'passed is a string')
		  ]);
		  
console.log(result[0]) //  passed is a string
	
var result=v.validate([
                        c.isString('hello'),
	                    'failed is a string',
    	                'passed is a string')
		  ],
		  [
                        c.isString(33),
	                    'failed is a string',
    	                'passed is a string')
		  ]
		  );
		  
console.log(result[0]) //  passed is a string
console.log(result[1]) //  failed is a string
	

or in the above examples instead of the strings a function may be used as

var myOnFail=function(){
             return 'failed is a string'
	}
var myOnPass=function(){
             return 'passed is a string'
	}
	
	
var result=v.validate([
                        c.isString('hello'),
	                    myOnFail,
    	                myOnPass)
		  ],
		  [
                        c.isString(33),
	                    myOnFail,
    	                myOnPass)
		  ]
		  );
		  
console.log(result[0]) //  passed is a string
console.log(result[1]) //  failed is a string

Tests:

To run the test navigate to the test directory and execute mocha.

mocha

Keywords

FAQs

Last updated on 19 Apr 2016

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc