1.4.3 • Published 27 days ago

checkmatejs v1.4.3

Weekly downloads
-
License
ISC
Repository
-
Last release
27 days ago

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);
1.4.3

27 days ago

1.4.2

1 month ago

1.4.1

1 month ago

1.4.0

8 months ago

1.3.0

8 months ago

1.2.0

8 months ago

1.1.0

9 months ago

1.0.0

9 months ago