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

node-verifier

v1.0.1

Published

extensible value validator

Downloads

12

Readme

npm npm Dependency Status devDependency Status Build Status Coverage Status

node-verifier

Gitter

##Usage

var Verifier = require('node-verifier');

// init the rules for verify
var myValueVerifier = new Verifier(['type number', 'min_length 1', 'min_value 5']);
// equal
var myValueVerifier = new Verifier(['type "number"', 'min_length "1"', 'min_value "5"']);
// equal
var myValueVerifier = new Verifier([{type: 'number'}, {min_length: 1}, {min_value: 5}]);

// final - verify!
myValueVerifier.verify(value, function (err) {

    if (err instanceof Verifier.ValidationError) {
        // invalid!!!!
        console.log(err.rule);
        console.log(err.params);
        console.log(err.index);
        return;
    }

    // valid!!!
});

Predefined Rules:

You can verify any rule without verifier object

var Verifier = require('node-verifier');
var RuleType = Verifier.Rule.get('type');

var var rule = new RuleType('object');

rule.verify(value, function (err) {
    if (err instanceof Verifier.Rule.ValidationError) {
        // if invalid

        return;
    }

    // if valid
});

You can create your own rules. There is one required method check (value, params, done). It give 3 arguments: value - value that need check. params - params that you set in constructor. done - callback. First argument is validation error (Verifier.Rule.ValidationError). Second argument is result for error. if you set true then generate ValidationError automatically.

There is one optional function prepareParams(params) for obtain the fail-fast paradigm. (it call in constructor). if you throw the error - error will arrive in main loop

var Verifier = require('node-verifier');

var UserExistsRule = Verifier.Rule.extend({
    check: function (value, params, done) {
        if (!_.isPlainObject(value) || !_.has(value, 'email') || !_.has(value, 'password')) {
            done(new Error('rule params must be object {email: NAME_STRING, password: PASSWORD_STRING}'), false);
            return;
        }

        knex('user').where('email', params.email).exec(function (err, userArray) {
            if (!err && userArray && userArray.length === 1 && userArray[0].password === sha1(params.password)) {
                done(null, true);
                return;
            }

            done(err, false);
        });
    }
});

Verifier.Rule.add('userExists');

var myValidator = new Verifier(['type object', 'userExists']);

myValidator.verify({ email: '[email protected]', password: 123123123 }, function (err) {
    if (err instanceof Verifier.ValidationError) {
        // invalid
        return;
    }

    // valid
});

You can extend any exists rules

var Verifier = require('node-verifier');
var RuleType = Verifier.Rule.get('type');

var MyRuleType = RuleType.extend({
    prepareParams: function (params) {
        return Object.prototype.toString.call(params);
    }
});

// register new rule
Verifier.Rule.add('my-type', MyRuleType);

each

For array item validation. params - rules for verify.

var myValueVerifier = new Verifier({each: ['type number', 'min_length 1', 'min_value 5']});

myValueVerifier.verify( [ 5, 3 ], function (err) {
    console.log(err); // null
});

myValueVerifier.verify( [ 3, "5" ], function (err) {
    console.log(err.rule); // 'type'
    console.log(err.params); // 'number'
    console.log(err.index); // 1
});

email

no params

check the email format by https://www.npmjs.org/package/email-validator

empty

no params

check value on false, 0, "", null, undefined, [], {}

eq

result of _.isEqual (lodash)

var myVerifier = new Verifier('eq 5');
// equal
var myVerifier = new Verifier({ eq: 5 });

myVerifier.verify(3, callback);  // invalid
myVerifier.verify("5", callback);  // invalid
myVerifier.verify(5, callback); // valid

exact_length

for string and arrays param - number

var myVerifier = new Verifier('exact_length 5');
// equal
var myVerifier = new Verifier({ exact_length: 5 });

myVerifier.verify("asdasdasd", callback);  // invalid
myVerifier.verify("asdas", callback); // valid

format

check value on RegExp match param - regExp (string)

var myVerifier = new Verifier('format ^[a-zA-Z][a-zA-Z0-9_.-]*@[a-zA-Z0-9]+\\.[a-zA-Z0-9]+$');
// equal
var myVerifier = new Verifier({format: /^[a-zA-Z][a-zA-Z0-9_.-]*@[a-zA-Z0-9]+\.[a-zA-Z0-9]+$/);

myVerifier.verify("[email protected]", callback);  // valid
myVerifier.verify("asdasdasd@asd", callback); // invalid

max_length

for string and arrays

var myVerifier = new Verifier('max_length 3');
myVerifier.verify(null, callback); // invalid
myVerifier.verify([1], callback);  // valid
myVerifier.verify([1, 2, 3], callback); // valid
myVerifier.verify("123", callback); // valid
myVerifier.verify(123, callback); // valid (convert to String)
myVerifier.verify(NaN, callback); // invalid

min_length

for string and arrays

var myVerifier = new Verifier('min_length 3');
myVerifier.verify(null, callback); // invalid
myVerifier.verify([1], callback);  // invalid
myVerifier.verify([1, 2, 3], callback); // valid
myVerifier.verify("123", callback); // valid
myVerifier.verify(123, callback); // valid (convert to String)
myVerifier.verify(NaN, callback); // invalid

max_value

check number

var myVerifier = new Verifier('max_value 3');
myVerifier.verify(4, callback); // valid
myVerifier.verify("4", callback); // valid (convert to float)

min_value

check number. param - number

var myVerifier = new Verifier('min_value 3');
myVerifier.verify(value, callback);

not

params - rule(s) for negation

var myVerifier = new Verifier({ not: ['required', 'type number', 'eq 3'] });
myVerifier.verify(value, callback);

required

check this value on undefined only. params not need.

var myVerifier = new Verifier({ required: true});
// equal
var myVerifier = new Verifier('required');

myVerifier.verify(value, callback);

type

check on type by Object.prototype.toString.call(value). param - type name without '[Object' and ']' in lower case

var myVerifier = new Verifier({ type: 'object'});
// equal
var myVerifier = new Verifier('type object');

myVerifier.verify(value, callback);

any

True if any verification branch is valid.

var myVerifier = new Verifier({
    any: [
        ['type string', {contains: ["hello", "world"]}], // branch 1
        'type number',                                   // branch 2
    ]
});
// Equal
var myVerifier = new Verifier({
    any: {
        branch1: ['type string', {contains: ["hello", "world"]}],
        branch2: 'type number'                           // branch 2
    }
});

myVerifier.verify(value, callback);

contains

True if any item of haystack array is equal with value

var myVerifier = new Verifier({contains: [1, 2, 3, 4]});
myVerifier.verify(value, callback);