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

abstract-validator

v1.1.0

Published

Simple js data validator to check form inputs.

Readme

Abstract Validator

Abstract Validator is a javascript library to help you to validate data objects, for html forms for example, which can work server-side or client-side.

Installation

Make sure that you have npm installed, and run:

npm install abstract-validator

Usage

Validator has a method called validate which takes as arguments the inputs (as an object) and the rules, and which returns the errors as an object.

For example, here is the validation of a comment post:

const Validator = require('abstract-validator')

let rules =
{
    pseudo: ['required', 'string', {maxChars: 15}],
    email: ['required', 'email'],
    comment: ['required', 'string', {maxChars: 1500}]
}

let inputs =
{
    pseudo: 'John Doe',
    email: '[email protected]',
    comment: 'Blablabla...'
}

let errors = Validator.validate(inputs, rules)
errors == {pseudo: null, email: null, comment: null} // true

inputs =
{
    pseudo: '   '
    email: 'john.doe',
    comment: 'Imagine that there are more than 1500 characters here'
}

errors = Validator.validate(inputs, rules)
errors ==
{
    pseudo: 'This field is required.',
    email: 'This field must be a valid email.',
    comment: 'This field must be less than or equal to 1500.'
} // true

So, Validator.validate returns an object of errors with the field names as keys and the first error message as value.

Some rules are simply strings, whereas some rules have values (like maxChars) so they are objects with the rule name as key and the value as value (like {maxChars: 15}).

Here are the different available rules:

  • required: Check if the field exists and if it's not empty.

  • nullable: Authorize the field to be a null value.

  • string: Check if the field is a string.

  • int: Check if the field is an integer.

  • number: Check if the field is a number.

  • boolean: Check if the field is a boolean.

  • array: Check if the field is an array.

  • object: Check if the field is an object.

  • equalTo (has whatever as value): Check if the field equals some value.

  • match (has a RegExp as value): Check if the field match some regular expression.

  • minChars (has a number as value): Check if the field has a least a certain number of characters.

  • maxChars (has a number as value): Check if the field has at most a certain number of characters.

  • min (has a number as value): Check if the field is greater than or equal to a certain number.

  • max (has a number as value): Check if the field is less than or equal to a certain number.

  • email: Check if the field is a valid email.

Language

By default, the messages are in English. You can change the language with the method Validator.setLocale:

const Validator = require('abstract-validator')
Validator.setLocale('en')

Currently, there is only two languages available: English (en) and French (fr).

You can check which locale is currently setted with the method Validator.getLocale.

You can add a new language by passing a object of messages for each rules, like this:

const Validator = require('abstract-validator')

Validator.addLocale('en', // To replace with your language
{
    required: 'This field is required.',
    string: 'This field must be text.',
    int: 'This field must be an integer.',
    number: 'This field must be an number.',
    boolean: 'This field must be a boolean.',
    array: 'This field must be an array.',
    object: 'This field must be an object.',
    equalTo: 'This field must be equal to "%val%".', // %val% will be replaced by the rule value
    match: 'This field is not valid.',
    minChars: 'This field must have at least %val% characters.', // %val% will be replaced by the rule value
    maxChars: 'This field must have at most %val% characters.', // %val% will be replaced by the rule value
    min: 'This field must be greater than or equal to %val%.', // %val% will be replaced by the rule value
    max: 'This field must be less than or equal to %val%.', // %val% will be replaced by the rule value
    email: 'This field must be a valid email.'
})

Or you can contribute to the project and add the translation to the repo, by creating a json file in the messages/ folder and adding addLocale('aa', require('./messages/aa.json')) (where aa is the locale) to abstract-validator.js.