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 🙏

© 2026 – Pkg Stats / Ryan Hefner

validator-v2

v1.2.5

Published

Data Validation Made Easy

Downloads

70

Readme

data-validator v2

Javascript module that can be very efficiently and conveniently used with forms validation.
Can be used either in html file script tags or as a node.js module

Install

npm i install validator-v2

Usage Example


const Validator = require('validator-v2')
const songValidation = new Validator({
    id: {
        required: true,
        type: Number,
        onError: {
            required: "ID is necessary",
            type: "ID is not a number"
        }
    },
    title: {
        required: true,
        type: String,
        minLen: 4,
        maxLen: 25,
        regexMatch: /^[a-zA-Z\s]+$/,
        onError: {
            any: "Title must be of 4-25 chars and contain only english letters"
        }
    },
    uploaderUsername: {
        type: String,
        minLen: 4,
        maxLen: 20,
        regexMatch: /^[a-zA-Z_]+/,
        onError: {
            any: "Username must be of 4-20 chars",
            regexMatch: "Username must contain only english letters and underscore"
        }
    },
    duration: {
        required: true,
        type: String,
        regexMatch: /^(\d+):(\d+)$/,
        onError: {
            regexMatch: "Duration must be of m:s structure. example: 4:25"
        }
    }
})
songValidation.check({id: "string id", title: "One Day", duration: "3:35"})
	.then(() => {
		console.log("Song is valid")
	})
	.catch(err => {
		console.log(err.msg)
	})

Options

  • required: boolean (default is false)
  • minLen: number
  • maxLen: number
  • length: number(exact length) or array[min, max]
  • validate: function (view below)
  • type: string (Number, String, etc.)
  • regexMatch: regex (on unmatch is invalid)
  • regexFail: regex (on match is invalid)
  • onError: object (view below)
  • object: another validator-v2 instance
  • each: another validator-v2 instance

object

let personValidation = new Validator({
	name: {
		required: true,
		type: String
	},
	contact: {
		required: true,
		object: new Validator({
			tel: {
				required: false,
				regexMatch: /^\d+$/,
				onError: "Only numbers are allowed in 'tel' field. Remove last 'E' to make it work."
			},
			email: {
				required: true,
				email: true
			},
			address: {
				required: false,
				type: String
			}
		})
	}
 })

each

let authorValidation = new Validator({
	name: {
		required: true,
		type: String
	},
	books: {
		each: new Validator({
			title: {
				required: true,
				type: String
			},
			pagesAmount: {
				required: true,
				type: Number
			}
		})
	}
 })

onError

An object that decides which message to return on rejection. Every failed field may have its own message.
** 'any' property is the default error message (for all cases).
** not more then one (option) message will be returned for a field

validate

Use:

validate: (value, cb) => { ... cb(boolean) }

Parameters

value: The value which was inserted in the check method
cb: The function to invoke when a result whether the validation has succeeded or not was concluded

Details:

Best used for:

  1. Async validations (such as database querying, etc.)
  2. Customized validations

Example:

    validate: (value, cb) => {
        setTimeout(() => { 
            cb(value > 5)
        }, 1000
    });