# checkmatejs

> checkmate will check your inputs mate

Latest version **1.4.3** (published 2024-04-16) · ISC license · 0 weekly downloads

## Install

```sh
npm install checkmatejs
pnpm add checkmatejs
yarn add checkmatejs
bun add checkmatejs
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.4.3 |
| Published | 2024-04-16 |
| First published | 2023-09-10 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 38.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | rathnakumar |
| Maintainers | rathnakumar-m |

## Links

- npm: https://www.npmjs.com/package/checkmatejs
- npm.io page: https://npm.io/package/checkmatejs

## Recent versions

- 1.4.3 (latest) — 2024-04-16
- 1.4.2 — 2024-04-13
- 1.4.1 — 2024-04-01
- 1.4.0 — 2023-09-10
- 1.3.0 — 2023-09-10
- 1.2.0 — 2023-09-10

## 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

```bash
npm install checkmatejs
```

### Using cdn

```html
<script src="https://unpkg.com/checkmatejs/dist/CheckMate.min.js"></script>
```

### Usage npm

```js
// ES5
let CheckMate = require("checkmatejs");
```

```js
// ES6
import CheckMate from "checkmatejs";
```

## Table of Contents

- [Type Castings](#type-castings)
- [Checkers Rules](#checkers-rules)
- [Schema Examples](#schema-examples)
  - [Basic Schema Example](#basic-schema-example)
  - [Schema Example with `require_with`](#schema-example-with-require_with)
  - [Schema Example with Custom Checkers](#schema-example-with-custom-checkers)

## 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

```javascript
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

```javascript
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

```javascript
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

```javascript
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.!@%_:/,+''"\ -]$/'

```javascript
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);
```

---
_Source: https://npm.io/package/checkmatejs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
