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

validjs

v1.0.0

Published

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

Downloads

4

Readme

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

Links: