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

validators-constructor

v0.1.6

Published

Constructor for library of validators

Downloads

224

Readme

validators-constructor

Constructor for library of validators

NPM version Build status

Note: This module works in browsers and Node.js >= 4.0

Installation

npm install validators-constructor

Usage

const validators = require('validators-constructor')();

validators.add({
    /* Simple validator */
    maxLength: function(value, arg, options) {
        //arg have to exist and can not be an object or true. Otherwise it will be options
        //arg also is available as options.arg

        if ((isString(value) || isArray(value)) && value.length > arg) {
            return 'is too long (maximum is %{arg})';
        }
    },
    lengthMax: 'maxLength', //alias for `maxLength`

    /* Validate by several params */
    myRange: function(value, options) {
        var typeNumberError = this.number(value) //you can find any validator in `this`

        if (typeNumberError) {
            return typeNumberError; //returns error message of `number` validator
        }

        if (value > options.to) {
            return {
                error: 'range.many', //error is key. It should be unique
                message: options.manyMessage || 'is too many (should be from %{from} to %{to})',
                description: 'Make your number less' //You can use extra fields
            }
        }

        if (value < options.from) {
            return {
                error: 'range.less',
                message: options.lessMessage || 'is too less (should be from %{from} to %{to})',
                description: 'Make your number greater'
            }
        }
    },

    /* Chain of validators */
    minStrict: ['required', ['number', {strict: true}], function(value, arg, opts) {
        if (value < arg) {
            return '%{value} is too short (minimum is %{arg})';
        }
    }],
});

validators.lengthMax('abc', 2);
/* returns:
{
    message: 'is too long (maximum is 2)',
    error: 'lengthMax',
    arg: 2
}
*/

validators.range(7, {from: 1, to: 5, lessMessage: 'is too less', manyMessage: 'is too many'});
/* returns (options which end in `Message` are not in the result):
{
    description: 'Make your number less',
    message: 'is too many',
    error: 'range.many',
    from: 1,
    to: 5
}
*/

validators.minStrict(null, 3);
/* returns:
{
    message: 'can\'t be blank',
    error: 'required'
}
*/

Validator have to return error message if value is invalid and nothing in opposite case. You can handle validation result in resultHandler. It is useful for third party validators

const validatorJS = require('validator'); //https://github.com/chriso/validator.js
//These validators return true if valid, false in opposite case and can throw exception

validators.add(validatorJS, {
    resultHandler: function(result) {
        if (!result) {
            return '%{value} is not %{validator}'
        }
    },
    exceptionHandler: function(err) {
        return err;
    }
});

validators.isEmail('abc', 3);
/* returns:
{
    message: 'abc is not isEmail',
    error: 'isEmail'
}
*/

validators.isEmail(null, 3); //catch exception by default
/* returns:
{
    message: 'This library (validator.js) validates strings only',
    error: 'isEmail'
}
*/

API

Validators([params])

  • params (Object)
    • arg (String) - name of argument for compared values. By default: arg
    • simpleArgsFormat (Boolean) - any non object argument will be transformed to the {arg: <argument>}
    • oneOptionsArg (Boolean) - ignore second options argument validator(value, argOrOptions, ignoredOptions)
    • resultHandler (Function ) - handler of validation result. By default function(result) { return result }
    • exceptionHandler (Function) - handler of JS exceptions. By default: null, E.g.function(err) { return err } will return error message in standard format
    • formatStr (Function) - Custom template parser. params: templateStr, variablesObj. returns: str
    • errorFormat (Object) - Output format of error. By default:
{
    error: '%{validator}',
    message: '%{message}',
    $options: true,
    $origin: true
}

Default formatStr function allows to use %{template} syntax. Next variables are enabled: validator - validator name (e.g. maxLength); message - string that is returned in case of error; options which you set in validator options if $options: true; options which validator returns instead string (except options that end in Message) if $origin: true

  • return (Validators) new instance of Validators
const validators = require('validators-constructor')({errorFormat: '%{message}'})

--

validators.add(validatorName, validator, [params]) or (validators, [params])

  • validatorName (String) - Name of validator in validators instance

  • validators (Object) - Object has structure {validatorName: validator, ...}

  • validator (Function or String or Array) - Validator or alias or validators array (e.g. ['validatorName', ['validatorName', {...options}], validatorFn])

  • params (Object) validator params (see Validators params). Also you can set default 'message' in params

  • return (Validators) instance of Validators

validators.add('exists', function(value) {
    return !value && 'Should be';
})

validators.add({
    exists: function(value) {
        return !value && 'Should be';
    },
    notExists: function(value) {
        return value && 'Should not be';
    }
})

--

validator(value, [arg], [options])

  • value (Any) - Validated value

  • arg (Any) - Value for comparison. Have to exist and can not be an object or boolean. User can set it as options.arg. If you use 'arg' in your validator you must be sure that user will specify this value

  • options (Object) - Options

    • arg (Any) - Will be set if 'arg' is specified
    • message (Any) - Override error message
    • parse (Function) - Can change input value before validation
    • (Any) - Any custom options
  • (Any) - Any custom arguments

  • return (Any) - undefined if valid or error message. You can use %{template} syntax in message strings (validated value is enabled as value, compared value - as comparedValue). Also you can return promise with result of validation

validators.min(4, 5, {strict: false}); //'Should be less or equal 5'

--

validator.curry([arg], [options])

  • arg, options, etc. - see validator.

  • return (Function) - function, which gets value and returns result of validation

lessThen3 = validators.maxLength.curry(3);

lessThen3('1234') //Length should be less then 3

Tests

npm install
npm test

License

MIT