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

valdi

v0.0.374

Published

Input validation with configured, custom 'valid' patterns definitions (pre-ES6 version).

Downloads

15

Readme

valdi

Input validation with configured, custom 'valid' patterns definitions - for ES6 version check valdi-es6.

Current version

Version|Published|By|URL --- | --- | --- | --- 0.0.374 | 2017-03-27 | codebloke | npm

MIT License Build Status npm version

In order to install please run in your project path:

npm install --save valdi

NPM

Simple assertions (multiple conditions with AND)

var Valdi = require('valdi');

var ww1 = Valdi.simple.new('WW1').integer().greaterThan(1913).lessThan(1919);

console.log(ww1.value(1918));
console.log(ww1.value(2000));
console.log(ww1.value(1900));

shows following output:

true
false
false

More complex assertions (multiple conditions with OR/AND)

var Valdi = require('valdi');

var ww1 = Valdi.simple.new('WW1').integer().greaterThan(1913).lessThan(1919),
    ww2 = Valdi.simple.new('WW2').integer().greaterThan(1938).lessThan(1946);

console.log(
    Valdi.expression.factory().setOperator('OR')
        .add('WW1', ww1.value(1944))
        .add('WW2', ww2.value(1944))
        .result()
);
console.log(
    Valdi.expression.factory().setOperator('OR')
        .add('WW1', ww1.value(1984))
        .add('WW2', ww2.value(1984))
        .result()
);

shows following output:

true
false

Simpler approach to OR cases

var War = Valdi.simple.new()
            .or().inList('1914,1915,1916,1917,1918')
            .inList('1939,1940,1941,1942,1943,1944,1945');

console.log(War.value(1905));
console.log(War.value(1918));
console.log(War.value('ABC'));
console.log(War.value(1944));
console.log(War.value('Make peace not war'));

outputs following:

false
true
true
false
true
true
false

more about this approach here.

Other examples

Using whitelists and blacklists

The example above can be asserted with a simple whitelist scenario that makes the code much neater (@spooky-monkey):

// include module in your project
var Valdi = require('valdi');
Whitelist provided in native Array
// include module in your project
var warYear = Valdi.simple.new('WW1 & WW2 years via native Array')
        .integer().inList([1914, 
                           1915, 
                           1916, 
                           1917, 
                           1918, 
                           1939, 
                           1940, 
                           1941, 
                           1942, 
                           1943, 
                           1944, 
                           1945]);

console.log(warYear.desc);
console.log(warYear.value(1944));
console.log(warYear.value(1984));

// true
// false
Whitelist provided in JSON string
// include module in your project
var warYearJson = Valdi.simple.new('WW1 & WW2 years with whitelist in JSON')
        .integer().inList('[1914, 1915, 1916, 1917, 1918, 1939, 1940, 1941, 1942, 1943, 1944, 1945]');

console.log(warYearJson.desc);
console.log(warYearJson.value(1944));
console.log(warYearJson.value(1984));

// true
// false
Whitelist provided in simple CSV string
// include module in your project
var warYearCsv = Valdi.simple.new('WW1 & WW2 years with whitelist in CSV')
        .integer().inList('1914,1915,1916,1917,1918,1939,1940,1941,1942,1943,1944,1945');

console.log(warYearCsv.desc);
console.log(warYearCsv.value(1944));
console.log(warYearCsv.value(1984));

// true
// false
Whitelist provided in CSV string with double-quote delimiter
// include module in your project
var warYearCsv2 = Valdi.simple.new(
    'WW1 & WW2 years with whitelist in CSV w. doublequote delimiters'
    ).integer().inList(
        '"1914","1915","1916","1917","1918","1939","1940","1941","1942","1943","1944","1945"'
    );

console.log(warYearCsv2.desc);
console.log(warYearCsv2.value(1944));
console.log(warYearCsv2.value(1984));

« back to list

Simple assertions

Please note:

Simple is providing only conjuntions (assertions joined by AND only)!
Including Valdi in your project
var Valdi = require('valdi'),
    simple = Valdi.simple;    // simple validators all rules in conjunction (AND)

« back to list

Methods chain simple.number().value();
console.log( simple.new('A number').number().value(false) );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('A number').number().value('ABC') );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('A number').number().value('') );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('A number').number().value({ obj: true }) );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('A number').number().value(1234) );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('A number').number().value(10.11) );
console.log( 'expected: true' ); 
console.log( '' );

« back to list

Methods chain simple.numberAsString().value()
console.log( simple.new('A number as string').numberAsString().value(false) );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('A number as string').numberAsString().value('67.14') );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('A number as string').numberAsString().value('334567') );
console.log( 'expected: true' ); 
console.log( '' );

« back to list

Methods chain simple.integer().value()
console.log( simple.new('An integer').integer().value(false) );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('An integer').integer().value(14.56) );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('An integer').integer().value(1234) );
console.log( 'expected: true' ); 
console.log( '' );

« back to list

Methods chain simple.integer() .min() .max()
console.log( simple.new('An integer with min').integer().min(23).value(false) );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('An integer with min').integer().min(23).value(100) );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('An integer with min').integer().min(123).value(100) );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('An integer with max').integer().max(23).value(false) );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('An integer with max').integer().max(23).value(22) );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('An integer with max').integer().max(23).value(55) );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('An integer with max & max').integer().min(5).max(15).value(false) );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('An integer with max & max').integer().min(5).max(15).value(14) );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('An integer with max & max').integer().min(5).max(15).value(-1) );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('An integer with max & max').integer().min(5).max(15).value(16) );
console.log( 'expected: false' ); 
console.log( '' );

« back to list

Methods chain ...integerAsString()
console.log( simple.new('An integer as string').integerAsString().value(false) );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('An integer as string').integerAsString().value('2345') );
console.log( 'expected: true' ); 
console.log( '' );

« back to list

Methods chain ...real()
console.log( simple.new('A real number').real().value(false) );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('A real number').real().value(2.15) );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('A real number').real().value('2.56') );
console.log( 'expected: false' ); 
console.log( '' );

« back to list

Methods chain ...realAsString()
console.log( simple.new('A real number as string').realAsString().value(false) );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('A real number as string').realAsString().value('3.14') );
console.log( 'expected: true' ); 
console.log( '' );

« back to list

Methods chain ...bool()
console.log( simple.new('A bool').bool().value(false) );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('A bool').bool().value(true) );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('A bool').bool().value(null) );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('A bool').bool().value({ obj: true }) );
console.log( 'expected: false' ); 
console.log( '' );

« back to list

Methods chain ..null()
console.log( simple.new('A null').null().value(null) );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('A null').null().value(false) );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('A null').null().value({ obj: true}) );
console.log( 'expected: false' ); 
console.log( '' );

« back to list

Methods chain ..validLocale()
console.log( simple.new('A valid locale').validLocale().value(false) );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('A valid locale').validLocale().value('en') );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('A valid locale').validLocale().value('ar') );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('A valid locale').validLocale().value('ja') );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('A valid locale').validLocale().value('de') );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('A valid locale').validLocale().value('invalid') );
console.log( 'expected: false' ); 
console.log( '' );

« back to list

Methods chain ..string()
console.log( simple.new('A string value').string().value('false') );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('A string value').string().value({ obj: true }) );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('A string value').string().value(112.33) );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('A string value').string().value('Valid string') );
console.log( 'expected: true' ); 
console.log( '' );

« back to list

Methods chain ...email()
console.log( simple.new('An email').string().value('[email protected]') );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('An email').string().value('[email protected]') );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('An email').string().value(false) );
console.log( 'expected: false' ); 
console.log( '' );

« back to list

Methods chain ...emptyString()
console.log( simple.new('An empty string').emptyString().value('[email protected]') );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('An empty string').emptyString().value('') );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('An empty string').emptyString().value(false) );
console.log( 'expected: false' ); 
console.log( '' );

« back to list

Methods chain ...number().greaterThan()
console.log( simple.new('A number greater than').number().greaterThan(100).value(false) );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('A number greater than').number().greaterThan(100).value(101) );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('A number greater than').number().greaterThan(2).value(1) );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('A number greater than').number().greaterThan(25).value(32) );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('A number greater than').number().greaterThan(25).value(32) );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('A number greater than').integer(true).greaterThan(25).value('32') );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('A number greater than').real(true).greaterThan(1.3).value(2.1) );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('A number greater than').real(true).greaterThan(15.1).value(30.1) );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('A number greater than').real(true).greaterThan('1.3').value('2.1') );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('A number greater than').real(true).greaterThan('15.1').value('30.1') );
console.log( 'expected: true' ); 
console.log( '' );

« back to list

Methods chain ....number().lessThan()
console.log( simple.new('A number less than').number().lessThan(100).value(false) );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('A number less than').number().lessThan(100).value(78) );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('A number less than').number().lessThan('748').value(200) );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('A number less than').number().lessThan(55).value(3) );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('A number less than').number().lessThan(11).value(71) );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('A number less than').number(true).lessThan('11').value('7') );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('A number less than').integer(true).lessThan('11').value('71') );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('A number less than').real(true).lessThan('10.56').value('10.1') );
console.log( 'expected: false' ); 
console.log( '' );

« back to list

Methods chain ...equal()
console.log( simple.new('A number equal').number().equal(100).value(100) );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('A number equal').number().equal(100).value(100.00) );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('A string equal').string().equal('ABC').value('ABC') );
console.log( 'expected: true' ); 
console.log( '' );

« back to list

Methods chain ...notEqual()
console.log( simple.new('A number not equal').number().notEqual(123).value(100) );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('A number not equal').number().notEqual(99).value(100.00) );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('A string not equal').string().notEqual('AXC').value('ABC') );
console.log( 'expected: true' ); 
console.log( '' );

« back to list

Methods chain ..arrayAsString()
console.log( simple.new('Array as string')
                .string().arrayAsString().value('["8.8.8.8","2001:4860:4860::8888","2001:4860:4860::8844"]') );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('Array as string')
                .string().arrayAsString().value('"8.8.8.8","2001:4860:4860::8888","2001:4860:4860::8844"') );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('Array as string')
                .string().arrayAsString().value('8.8.8.8,2001:4860:4860::8888,2001:4860:4860::8844') );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('Array as string')
                .string().arrayAsString().value('8.8.8.8,2001:4860:4860::8888,2001:4860:4860::8844') );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('Array as string')
                .string().arrayAsString().value(false) );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('Array as string')
                .string().arrayAsString().value([1, 2,3]) );
console.log( 'expected: false' ); 
console.log( '' );

« back to list

URL
console.log( simple.new('Valid URL')
                .string().url().value('http://cecpvmlx080.internal.boo.net:8055/') );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('Invalid URL')
                .string().url().value('GaHssDGHDGghwsgyw') );
console.log( 'expected: false' ); 
console.log( '' );

« back to list

base64
console.log( simple.new('Valid Base64').string().base64().value('aGVyZSB3ZSBjb21lIGFnYWlu') );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('Invalid Base64').string().base64().value('GaHssDGHDGghwsgyw') );
console.log( 'expected: false' ); 
console.log( '' );

« back to list

ipv4
console.log( simple.new('Valid IPv4').string().ipv4().value('10.10.1.1') );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('Invalid IPv4').string().ipv4().value('GaHssDGHDGghwsgyw') );
console.log( 'expected: false' ); 
console.log( '' );

« back to list

ipv5
console.log( simple.new('Valid IPv6').string().ipv6().value('2001:4860:4860::8888') );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('Invalid IPv6').string().ipv6().value('GaHssDGHDGghwsgyw') );
console.log( 'expected: false' ); 
console.log( '' );

« back to list

alphanumeric
console.log( simple.new('Valid Alphanum')
                .string().alphanumeric('xx').value('2001:4860:4860::8888') );
console.log( 'expected: false' ); 
console.log( '' );
console.log( simple.new('Valid Alphanum')
                .string().alphanumeric('en').value('Christopher Marlowe\'s 0123456789'));
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('Valid Alphanum')
                .string().alphanumeric('de')
                .value('Gebhard-Leberecht von Blücher Fürst von Wahlstatt 0123456789') );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('Valid Alphanum')
                .string().alphanumeric('fr').value('AntoineDeSaint-Exupéry0123456789') );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('Valid Alphanum')
                .string().alphanumeric('ja').value('宮本武蔵 0123456789') );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('Valid Alphanum')
                .string().alphanumeric('ar').value('صلاحالدينيوسفبنأيوب0123456789') );
console.log( 'expected: true' ); 
console.log( '' );

« back to list

regexMatch
console.log( simple.new('Valid Regex')
                .string().regexMatch(/^\w+$/).value('0123456789') );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('Invalid Regex')
                .string().regexMatch(/^\w+$/).value('0123456789 (trigger regex false)') );
console.log( 'expected: false' ); 
console.log( '' );

« back to list

jsonString
console.log( simple.new('Valid JSON')
                .string().jsonString().value(JSON.stringify({this: 'is JSON'})) );
console.log( 'expected: true' ); 
console.log( '' );
console.log( simple.new('Invalid JSON')
                .string().jsonString().value('GaHssDGHDGghwsgyw') );
console.log( 'expected: false' ); 
console.log( '' );

« back to list

Using or() - more examples of OR
var War = Valdi.simple.new()
            .or().inList('1914,1915,1916,1917,1918')
            .inList('1939,1940,1941,1942,1943,1944,1945');

console.log(War.value(1905));
console.log(War.value(1918));
console.log(War.value('ABC'));
console.log(War.value(1944));
console.log(War.value('Make peace not war'));

this would give you:

false
true
true
false
true
true
false

« back to list