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

io-validate

v2.2.2

Published

Runtime data validator.

Downloads

35

Readme

io-validate

Standard - JavaScript Style Guide Build Status Coverage Status Npm Package Info Downloads

Runtime data validator.

Note

  • This package used to be named param-check.

Install

npm install io-validate --save

Usage

Basic usage

import v from 'io-validate';

function fn(arg1, arg2) {
  v(arg1).isString();

  // If you want to print variable name, pass name as the 2nd parameter.
  v(arg2, 'arg2').greaterThan(1).lessThan(2);
}

not operator

import v from 'io-validate';

function fn(arg) {
  v(arg).not.isString();
}

Chain call

All validators of io-validate support chain calls. A chain call means an "and" expression.

import v from 'io-validate';

function fn(arg) {
  v(arg).gt(1).lt(2); // arg > 1 && arg < 2
}

Specific validator

If you want smaller dependence size, just import specific validators.

import v from 'io-validate/minimum';
import isString from 'io-validate/lib/validators/isString';

v.register(isString);

function fn(arg) {
  v(arg).isString();
}

Custom validator

import v from 'io-validate';
import isNumber from 'lodash/isNumber';

function isEven(target, name) {
  return isNumber(target) && !(target % 2);
}

check.register('isEven', isEven);

function fn(arg) {
  v(arg).isEven();
}

Custom chain

import v from 'io-validate'

function isEven (target, name) {
  return !(target % 2)
}

function isOdd (target, name) {
  return target % 2
}

function next (target) {
  return return target + 1
}

v.register('isEven', isEven, next)
v.register('isOdd', isOdd, next)

function fn (arg) {
	v(arg).isEven().isOdd()
}

Plan

plan is used to generate combinable validators.

By contrast, v('').isString() executes the validator isString immediatly, v.plan.isString() doesn't execute any validator, just return a combinable isString validator.

import v from 'io-validate';

v('').isString();

v('').or(v.plan.isString, v.plan.isNumber);

Validators

same

Validate that the input value is the same to ('===') the reference value.

v(1).same(1);

v({ a: 1 }).same({ a: 1 }); // Bang!

among

Validate that the input value is among the reference options.

v(1).among(1, 2, 3, 4);

equal | eq

Validate that the input value is equal to the reference value.

You may pass in a custom equal function instead of the default equal algorithm which is a simplified implemention of deep-equal.

v(1).equal(1);
v(1).eq(1);

v('foobar').equal('Foobar', (left, right) => left.toLowerCase() === right.toLowerCase());

equalOrGreaterThan | egt

Validate that the input number is equal to or greater than the reference number.

v(1).equalOrGreaterThan(0);
v(1).egt(1);

equalOrLessThan | elt

Validate that the input number is equal to or less than the reference number.

v(1).equalOrLessThan(2);
v(1).elt(1);

greaterThan | gt

Validate that the input number is greater than the reference number.

v(1).greaterThan(0);
v(1).gt(0);

lessThan | lt

Validate that the input number is less than the reference number.

v(1).lessThan(2);
v(1).lt(2);

within

Validate that the input number is whthin the reference range(s).

Syntax:

  • Open range is denoted in parentheses.
  • Closed range is denoted by square brackets.
v(1).within('(0, 2)', '[-1, 3.5]');

v(0).within('[0, 1)', '(-1, 0]');

is | isXXX

Runtime type checking based on lodash.

v(1).is('number', 'string') // number or string

v(1).isNumber()
lodash type validators
var isArguments = require('lodash/isArguments');
var isArray = require('lodash/isArray');
var isArrayBuffer = require('lodash/isArrayBuffer');
var isArrayLike = require('lodash/isArrayLike');
var isArrayLikeObject = require('lodash/isArrayLikeObject');
var isBoolean = require('lodash/isBoolean');
var isBuffer = require('lodash/isBuffer');
var isDate = require('lodash/isDate');
var isElement = require('lodash/isElement');
var isEmpty = require('lodash/isEmpty');
var isError = require('lodash/isError');
var isFunction = require('lodash/isFunction');
var isLength = require('lodash/isLength');
var isMap = require('lodash/isMap');
var isNative = require('lodash/isNative');
var isNil = require('lodash/isNil');
var isNull = require('lodash/isNull');
var isNumber = require('lodash/isNumber');
var isObject = require('lodash/isObject');
var isObjectLike = require('lodash/isObjectLike');
var isPlainObject = require('lodash/isPlainObject');
var isRegExp = require('lodash/isRegExp');
var isSet = require('lodash/isSet');
var isString = require('lodash/isString');
var isSymbol = require('lodash/isSymbol');
var isTypedArray = require('lodash/isTypedArray');
var isUndefined = require('lodash/isUndefined');
var isWeakMap = require('lodash/isWeakMap');
var isWeakSet = require('lodash/isWeakSet');

Other type validators

var isExist = function isExist(o) {
  return !(isUndefined(o) || isNull(o));
};

instanceOf

Validate that the input value is an instance of the reference class.

We use the keyword 'instanceof' to implement this validator.

class Foobar {}
const fb = new Foobar();

v(fb).instanceOf(fb);

isArrayOf

Validate that the input value is an array of valid values.

v(fb).isArrayOf(fb, (el) => el % 2);

match

Regular expression validator.

v('foobar42').match(/\w+\d+/);

patterns

Common regular expression validators.

Available validators:
  • matchEmail
  • matchURL
  • matchIP
v('[email protected]').matchEmail();

has | hasOwn

Validate that the input value has an OWN property of the reference name.

has() returns a validatable object that encapsulates the validated property if it exists.

If you want the validatable object that encapsulates the origin input value, use "owner" or "_" operator.

const obj = {
  a: {
    b: {
      c: 'foobar',
    },
  },
};

v(obj).has('a').has('b').has('c');
v(obj).has('a').owner.has('a').owner.has('c');
v(obj).has('a')._.has('a')._.has('a');

got

Validate that the input value has a property (maybe NOT own property) of the reference name.

const obj = {
  a: {
    b: {
      c: 'foobar',
    },
  },
};

v(obj).got('a').got('b').got('c');
v(obj).got('a').got('__proto__').got('constructor');

length

Equivalent of validator got('length').

const arr = [1, 2, 3];

v(arr).length().eq(3);

and

Validator of "and" expression.

It is often better to express "and" relationships using chain calls, but this API combines functions and plans.

v('Foobar').and(v.plan.isString, (target) => {
  return target.toLowerCase() === 'foobar';
});

or

Validator of "or" expression.

v(1).or(
  v.plan.isString,
  v.plan.and(v.plan.isNumber(), (target) => target % 2)
);

meet

Single parameter version of validator and().

v('Foobar').meet(v.plan.isString);