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

checkmatejs

v1.4.3

Published

checkmate will check your inputs mate

Downloads

128

Readme

CheckMate: Data Validation Library

CheckMate is a data validation library that helps you validate and enforce rules on your data objects. It allows you to define validation rules and error messages for various data types and scenarios. Use CheckMate to simplify your data validation process.

Installation

Using npm

npm install checkmatejs

Using cdn

<script src="https://unpkg.com/checkmatejs/dist/CheckMate.min.js"></script>

Usage npm

// ES5
let CheckMate = require("checkmatejs");
// ES6
import CheckMate from "checkmatejs";

Table of Contents

Type Castings

  1. number
  2. float
  3. int
  4. string
  5. bool

Checker Rules

  1. required
  2. min
  3. max
  4. true
  5. false
  6. alpha
  7. num
  8. alpha_num
  9. alpha_num_free
  10. email
  11. url
  12. phone
  13. regex
  14. allow_empty

Schema Examples

Basic Schema Example

var data = {
  name: "john",
};

var schema = {
  name: {
    rules: ["string", "required", "alpha_num_free"],
    label: "Name",
    message: {
      required: "Name is required",
      alpha_num_free: "Name must not contain special characters",
    },
    props: {
      selector: "name",
    },
    errorHandler: function (props) {
      // Handle errors
      // porps structure
      // {
      // messages: [ 'Name is required', .... ],  --> error messages in array
      // key: 'name', --> field_name or object key
      // data: { name: 'john' } --> data or input supplied
      // ...props
      // }
    },
    successHandler: function (props) {
      // Handle non-errors
      //props structure
      // {
      // key: 'name', --> field_name or object key
      // data: { name: 'larry', phone: '+179834578' } --> data or input supplied
      // ...props
      // }
    },
  },
};

var checkmate = new CheckMate(schema, data);
var { error, messages } = checkmate.check();

console.log(error, messages);

Schema Example with require_with

var data = {
  name: "larry",
  phone: "+179834578",
};

var schema = {
  name: {
    rules: ["string", "required", "alpha_num_free"],
    label: "Name",
    message: {
      required: "Name is required",
      alpha_num_free: "Name must not contain special characters",
    },
    props: {
      selector: "name",
    },
    errorHandler: function (props) {
      // Handle errors
      // porps structure
      // {
      // messages: [ 'Name is required', .... ],  --> error messages in array
      // key: 'name', --> field_name or object key
      // data: { name: 'larry', phone: '+179834578' } --> data or input supplied
      // }
    },
    successHandler: function (props) {
      // Handle non-errors
      //props structure
      // {
      // key: 'name', --> field_name or object key
      // data: { name: 'larry', phone: '+179834578' } --> data or input supplied
      // }
    },
    async: false,
    require_with: {
      phone: {
        rules: ["string", "phone", "min:10", "max:16"],
      },
    },
  },
};

var checkmate = new CheckMate(schema, data);
var { error, messages } = checkmate.check();

console.log(error, messages);

Schema Example with Custom Checkers

var data = {
  role: "dev",
};

var schema = {
  role: {
    rules: ["string", "cu_check:enum"],
    cu_check: {
      enum: function (props) {
        // Custom checker logic
        // props structure
        // {
        //   current_value: 'dev', --> current value to check
        //   data: { role: 'dev' } --> data or input supplied
        // }
        // return true or false --> based on the validation true or false should be returned
      },
    },
    messages: {
      "cu_check:enum": "Value must be 'dev' or 'admin' only",
    },
  },
};

var checkmate = new CheckMate(schema, data);
var { error, messages } = checkmate.check();

console.log(error, messages);

Schema Example with Regex Pattern

var data = {
  dob: "1999-08-20",
};

var schema = {
  role: {
    rules: ["string", "regex_pattern:dob_pattern"],
    regex_patterns: {
        dob_pattern: /^(19|20)\d\d-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/
    }
    messages: {
      "regex_pattern:name_pattern": "DOB must be a valid date format eg: yyyy-mm-dd",
    },
  },
};

var checkmate = new CheckMate(schema, data);
var { error, messages } = checkmate.check();

console.log(error, messages);

Schema Example with regex BETA*

The regex checker is still in beta, but you can use it by providing a regex pattern as a string. This makes the checker a bit more challenging to use, so some compromises were made. For example, the - character needs to be passed at the end, like this: 'regex:/^[a-zA-Z0-9.!@%_:/,+''"\ -]$/'

var data = {
  role: "dev",
};

var schema = {
  role: {
    rules: ["string", "regex:/^[a-zA-Z0-9.!@%_:/,*+'\"\\\\ -]*$/"],
    messages: {
      regex: "Value is not a valid input",
    },
  },
};

var checkmate = new CheckMate(schema, data);
var { error, messages } = checkmate.check();

console.log(error, messages);