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

checkerz

v1.4.2

Published

A package to validate input.

Downloads

2

Readme

Checkerz

A package for validating JS objects motivated by Validators in Lararvel framework.

Installation

npm install checkerz

Usage

Importing the Validator Constructor

// ES5 Syntax
const Validator = require("Checkerz");

// ES6 Syntax
import Validator from "Checkerz";

Once the constructor is imported you can create as many validator as you like and validate your objects.

Examples

// Rules validator should use.
const rules = {
    name: "string|required",
    address: {
        city: "string|required|len:3,8", 
        state: "string|required|len:3,8"
    },
};

// Creating a new Validator.
const userValidator = new Validator(rules);

// Object to validate.
let user = {
    address: {
        city: "Albany",
        state: "NYK"
    }
}

// Validate the object using the check() function of validator.
let response = userValidator.check(user);

// Check if the validation passed using passed property of the response.
if(!response.passed) {
    // Response's error property contians an array of all the errors.
    console.log(err);
}

Ouput:

[ 'name property was required but was not found.' ]

Creating Validator

  • Rules for a value are seperated by |.

    [key]: "[rule1] | [rule2] | [rule3]"
  • If name of the rule and it's parameters are separated by : and the parameters themselves are separated by ,

    "name": "len:3,10" // Checks length of name is 3-10.
  • If you want to change the name of the key when displaying error message you can pass the name you want as the last value when specifying rules.

    [key]: "[rule1] | [rule2] | [name]"

    The new name must not match any rule.

  • If you want to change the name of the key the value of which is an object, you can pass in a __name property and specify the name you want to use.

    address: {
        city: "string|required|len:3,6",
        state: "string|required|len:3,8",
        __name: "Address"
    },

    All the errors in any child will be referenced with respect to that name. For eg. If the state's len property fails the name used will be 'Address.city' in the error message.

Available Rules

Type Rules: Checks whether the values is of the type specified.

  • array

  • boolean

  • date

  • json

  • string

  • number

    Type Rules are conflicting rules with each other which means while using one you cannot use the other one for validation.

Value Rules:

  • accepted:

    • Checks if the value is one of the following values: "1", 1, "True", true, "yes", "on".

    • Can be used for checkboxes like for terms & conditions.

      // Validator Rules:
      const rules = {
          "tnc": "accepted"
      };

    This is a conflicting rule with acceptedIf, present, required, & requiredIf which means while using one you cannot use the other one.

  • acceptedIf:

    • Conditional Version of Accepted Rule.
    • The values should be accepted if a certain key matches some value. If the specified doesn't match the specified value then it doesn't matter whether this key is accepted or not.
    • The rules for accessing the key to check value are specified here.

    This is a conflicting rule with accepted, present, required, & requiredIf which means while using one you cannot use the other one.

  • after:

    • Checks if the a particular date is after a given date.

    • Can be used to check if user is not giving an invalid date.

      // Validator Rules:
      const rules = {
          "dob": "after:2023/07/01",
      };
  • afterOrEqual:

    • Similar to after but also returns true for same day.

      // Validator Rules:
      const rules = {
          "dob": "afterOrEqual:2023/07/01",
      };
  • before:

    • Checks if the a particular date is before a given date.

    • Can be used to check age limit based on DOB.

      // Validator Rules:
      const rules = {
          "dob": "before:2023/07/01",
      };
  • beforeOrEqual:

    • Similar to before but also returns true for same day.
  • confirmed:

    • Looks for a key named "{current_key_name}_confirmed" in the object at same level, & sees if the values match.

    • Can be used for passwords.

      // Validator Rules:
      const rules = {
          "email": "email",
          "password": "string|confirmed"
      };
      
      // Object to validate.
      let user = {
          "email": "[email protected]",
          "password": "password",
          "password_confirmed": "password"
      };
  • different:

    • The field under validation must have a different value than the specified fields.
    • Various keys can be specified separated by commas.
    • The rules for accessing the key to check value are specified here.
  • digits: The integer under validation must have an exact number of digits as specified.

    "age": "digits:2" // Exactly 3 digits.
  • digitsBetween: The integer under validation must have digits between the given range.

    "num": "digitsBetween:2,3" // 2-3 Digits.
  • email: Checks if the value is a valid email.

  • gt: Checks if the value is greater than the given number.

  • gte: Checks if the value is greater than or equal to the given number.

  • in: Checks whether the value is from one of the value provided or not.

    level: 'number|in:1,2,3,4', // Level can have any value from 1, 2, 3, 4 and nothing else.
  • len:

    • Expects the value to be validated to be an Array or a String.

    • Checks if the length is in given range.

    • If a single value is given it is considered as lower limit.

      "name": "len:3" // Length must be atleast 3 characters.
    • If 2 values are given 1st is considered lower and 2nd as upper limit.

      "name": "len:3,6" // Length must be 3-6 characters.
  • lt: Checks if the value is less than the given number.

  • lte: Checks if the value is less than or equal to the given number.

  • notIn: Checks whether the value is from something that is not provided.

    level: 'number|notIn:1,2,3,4', // Level can have any value except 1, 2, 3, 4.
  • present: The key must be present. The value can be anything undefined, null or something else.

    This is a conflicting rule with accepted, acceptedIf, required, & requiredIf which means while using one you cannot use the other one.

  • required:

    • Checks if the value is the value is present & must not be null or undefined.
    • If a value is not required it will be considered optional and the validation for such properties will be skipped in case the value is absent in the object to be validted.

    This is a conflicting rule with accepted, acceptedIf, present, & requiredIf which means while using one you cannot use the other one.

  • requiredIf:

    • Conditional version of required.
    • The values is required if a certain key matches some value. If the specified doesn't match the specified value then this becomes an optional field.
    • The rules for accessing the key to check value are specified here.

    This is a conflicting rule with accepted, acceptedIf, present, & required which means while using one you cannot use the other one.

Rules to Access Keys During Validation:

Since, we are using objects we have 2 method to reference a key in the object.

  • Starting at top: To access from top start with a '.' and then traverse the object as you would with a dot notation. In the example below state is required phone.type = home. (Notice how it starts with a '.').

  • Starting at current level: Traverse the object as you would with a dot notation. (Notice that since it is at same level no '.' is required).

// Rules validator should use.
const rules = {
    "person": {
        "name": "string|required",
        "address": {
            "state": "string|requiredIf:.phone.type=home",
            "zip": "string|requiredIf:state=NY"
        }
    },
    "phone": {
        "number": "string|required",
        "type": "string|required"
    }
};