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

datavader

v2.0.0

Published

JavaScript Data Validator

Downloads

2

Readme

DATAVADER

A library for object validation

Install

npm i datavader

Usage

import { validate, validateByScheme, addValidatorRule, addParameterRule } from 'datavader';

// it will be tested

const user = {
  name: 'Rick',
  age: 70,
  quality: 'alcoholic',
  catchphrase: 'wubba lubba dub-dub',
  relatives: ['morty', 'summer', 'beth']
};

// use validate method to сheck a single property

validate(user).check('name').with('presence', {exist: true});
validate(user).check('age').with('number', {greaterThan: 60});
validate(user).check('quality').with('exclusion', {values: ['kind', 'gentle']});

// use validateByScheme method to validate the whole object

const scheme = {
  name: {
    presence: {exist: true},
    length: {
      min: 1,
      max: 10,
    },
  },
  age: {
    number: {
      lessThan: 80,
      greaterThanOrEqualTo: 70,
      isEven: true;
      isInteger: true,
    },
  }
  ...
}

validateByScheme(user, scheme);

// use addValidatorRule and addParameterRule to define custom rule

const customValidator = ({firstName, lastName}) => {
  return firstName !== lastName;
}
  
const customOptions = (item) => {
  return {
    firstName: item.firstName,
    lastName: item.lastName,
  }
}

addValidatorRule('firstNameNotEqLastName', customValidator);
addParameterRule('firstNameNotEqLastName', customOptions);

validate(user).check('firstName').with('firstNameNotEqLastName', {});

Validators

presence

validate([object]).check([property]).with('presence', [options])

|options|behavior| |----|-------| |{exist: true}|true if property is not null and its length > 0| |{exist: true, allowNull: true}|true if property is null| |{exist: true, allowBlank: true}|true if property is empty| |{exist: false}|true if property is undefined or null or empty|

absence

validate([object]).check([property]).with('absence', [options])

|options|behavior| |----|-------| |{exist: false}|true if property is undefined| |{exist: true}|false if property is undefined|

confirmation

validate([object]).check([property]).with('confirmation', {})

|options|behavior| |----|-------| |{}|true if object has property with same name and suffix _confirmation and its value === property value

inclusion

validate([object]).check([property]).with('inclusion', [options])

|options|behavior| |----|-------| |{values: []}|true if property value includes in an array

exclusion

validate([object]).check([property]).with('exclusion', [options])

|options|behavior| |----|-------| |{values: []}|true if property value does not include in an array

length

validate([object]).check([property]).with('length', [options])

|options|behavior| |----|-------| |{in: 5}|true if length === 5 |{min: 5}|true if length >= 5 |{max: 5}|true if length < 5 |{min: 1, max: 5}|true if length >= 1 and < 5

number

validate([object]).check([property]).with('number', [options])

|options|behavior| |----|-------| |{isInteger: true}| |{isOdd: true}| |{isEven: true}| |{equalTo: 5}| |{otherThan: 5}| |{greaterThan: 5}| |{greaterThanOrEqualTo: 5}| |{lessThan: 5}| |{lessThanOrEqualTo: 5}|

Validation of the entire object

The validators we saw above let you check a single property. But you can check the whole object with the validateByScheme method. This method takes two arguments:

  1. object
  2. validation scheme

Validation scheme example:

{
  username: {
    presence: {exist: true},
    length: {min: 2, max: 8}
  },
  password: {
    confirmation: {},
    length: {min: 6, max: 20}
  }
}

If there are any errors, it will return an array with failed rules:

{
  username: ['presence'],
  password: ['confirmation', 'length']
}

Customization

You can create your own validator and register one with addValidatorRule method.

For expample let's create isEmail rule.

const isEmailValidator = ({value, domains}) => {
  const reg = new RegExp(`^[a-zA-Z0-9_.+-]+@(?:(?:[a-zA-Z0-9-]+\.)?[a-zA-Z]+\.)?(${domains.join('|')})\.com$`, 'ig');
  return reg.test(value)
}

Now you should register it as a validation rule.

addValidatorRule('isEmail', isEmailValidator, isPrimary);

The last parameter is optional. If it is true the validator will be checked as primary. Primary validators are executed in first order. If a primary validator returns false, all of the following validators will be skipped. By default there are two primary validators - 'presence' and 'absence'.

So, after that you can use it.

validate(user).check('email').with('isEmail', {domains: ['wubba', 'lubba', 'dub']});

By default each validator gets the following parameters:

  1. property value
  2. and options from with method

You can change it with addParameterRule method.

For example, look at the confirmation validator. It returns true if the object has property with a suffix _confirmation.

const confirmation = (params) => {
  const {value, valueConfirmation} = params;
  return value === valueConfirmation;
}

To set the correct values into parameters we should create special method.

const foo = (item, property, options) => {
  return {
    value: item[property],
    valueConfirmation: item[`${property}_confirmation`],
  }
}

...and register it by validator name.

addParameterRule('confirmation', foo);