# @ibrokhim.shokirov/json-data-validator

> Great data validator

Latest version **2.0.6** (published 2018-10-09) · GPL-3.0 license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @ibrokhim.shokirov/json-data-validator
pnpm add @ibrokhim.shokirov/json-data-validator
yarn add @ibrokhim.shokirov/json-data-validator
bun add @ibrokhim.shokirov/json-data-validator
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 2.0.6 |
| Published | 2018-10-09 |
| First published | 2018-10-08 |
| Weekly downloads | 0 |
| License | GPL-3.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 47.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Ibrokhim Shokirov |
| Maintainers | ibrokhim.shokirov |
| Keywords | data, validator, nodejs, socket, io |

## Links

- npm: https://www.npmjs.com/package/@ibrokhim.shokirov/json-data-validator
- Repository: https://github.com/IIINIII/Json-Data-Validator
- Homepage: https://github.com/IIINIII/Json-Data-Validator#readme
- Issues: https://github.com/IIINIII/Json-Data-Validator/issues
- npm.io page: https://npm.io/package/@ibrokhim.shokirov/json-data-validator

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 2.0.6 (latest) — 2018-10-09
- 2.0.5 — 2018-10-09
- 2.0.4 — 2018-10-09
- 2.0.3 — 2018-10-09
- 2.0.2 — 2018-10-08
- 2.0.1 — 2018-10-08
- 2.0.0 — 2018-10-08
- 1.0.8 — 2018-10-08
- 1.0.7 — 2018-10-08
- 1.0.6 — 2018-10-08
- 1.0.5 — 2018-10-08
- 1.0.4 — 2018-10-08
- 1.0.3 — 2018-10-08
- 1.0.2 — 2018-10-08
- 1.0.1 — 2018-10-08
- … 1 more at https://npm.io/package/@ibrokhim.shokirov/json-data-validator/versions

## README

# JSON Data Validator

This is a great, easy to use and flexible JSON data validator.

## About

Let's say you are receiving a JavaScript object in your application and for using the data without a problem you have to use something like  `if(data != null)`  or  `if(data != undefined)`  each time you want to access the data's value. I know it is so annoying. Well not anymore. Using this package you can now easily create a configuration for the data you receive and validate it. It is like a dream, I know but, not anymore.

## Features

- Verbose validation results **(NEW)**
- Great, easy to use and flexible configurations
- Useful in any project
- Many useful functions
- User-friendly feedbacks
- In case of invalid data user-friendly report

## Installation

This is a [Node.js](https://nodejs.org/en/) module available through the [npm registry](https://www.npmjs.com/).

Before installing, [download and install Node.js](https://nodejs.org/en/download/).

Installation is done using the [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):

```bash
npm install --save @ibrokhim.shokirov/json-data-validator
# or
npm i --save @ibrokhim.shokirov/json-data-validator
```

Check examples below on how to use this package in your project.

## Quick Start

Install package using command below:

```bash
npm install --save @ibrokhim.shokirov/json-data-validator
```

Add the following line to the top of your JavaScript file:

```javascript
var validator = require('@ibrokhim.shokirov/json-data-validator');
```

Add your configuration to the validator:

```javascript
validator.config = my_config;
// or
validator.setConfiguration(my_config);
```

If you want verbose result, then:

```javascript
validator.verbose = true; // dafault: false
```

And now it is time to validate our data:

```javascript
validator.isValid(subConfigName, data); // Returns true if data is valid
```

If you prefer to use new configuration each time you validate a data, you can do something like this:

```javascript
validator.validate(my_config, data); // Returns true if data is valid
```

## Example Configuration

You can get an example configuration object in JSON format by running command below:

```javascript
validator.exampleConfiguration();
```

Returns:

```javascript
{
    // A basic object configuration
    'chat_data': {
        type: 'object',
        props: {
            to: {type: 'string'},
            from: {type: 'string'},
            msg: {type: 'string'}
        }
    },

    // A basic string configuration
    'username': {
        type: 'string'
    },

    // A basic number configuration
    'age': {
        type: 'number'
    },

    // A little bit more complex configuration
    'user_data_complex': {
        type: 'object',
        props: {
            name: {type: 'string'},
            surname: {type: 'string'},
            isActive: {type: 'boolean'},
            contact_info: {
                type: 'object',
                props: {
                    email: {type: 'string'},
                    phone: {type: 'string'}
                }
            }
        }
    }
}
```

## Usage Examples

Example using a simple configuration:

```javascript
var validator = require('@ibrokhim.shokirov/json-data-validator');
var config = {
    // A basic string configuration
    'username': {
        type: 'string'
    },

    // A basic number configuration
    'id': {
        type: 'number'
    },
};

validator.verbose = true;
validator.setConfiguration(config);

console.log('Test #1:');
var data = "admin";
console.log(validator.isValid('username', data));

console.log('\nTest #2:');
data = 123;
console.log(validator.isValid('id', data));

console.log('\nTest #3:');
data = 321;
console.log(validator.isValid('username', data));

console.log('\nTest #4:');
data = "asdf";
console.log(validator.isValid('id', data));
```

Output:

```
Test #1:
true

Test #2:
true

Test #3:
-----------Validator Error-----------
Object Path: data
Expected: string
Got: number
Value: 321
-------------------------------------
false

Test #4:
-----------Validator Error-----------
Object Path: data
Expected: number
Got: string
Value: asdf
-------------------------------------
false
```

Example using much more complex configuration:

```javascript
var validator = require('@ibrokhim.shokirov/json-data-validator');
var config = {
    'user_detail': {
        type: 'object',
            props: {
            name: {type: 'string'},
            surname: {type: 'string'},
            isActive: {type: 'boolean'},
            contact_info: {
                type: 'object',
                    props: {
                    email: {type: 'string'},
                    phone: {type: 'string'}
                }
            }
        }
    }
};

validator.verbose = true;
validator.setConfiguration(config);

console.log('Test #1:');
var data = {
    'name': 'Tom',
    'surname': 'Jerry'
};
console.log(validator.isValid('user_detail', data));

console.log('\nTest #2:');
data = {
    'name': 'Tom',
    'surname': 'Jerry',
    'isActive': true,
    contact_info: 'tom.jerry@example.com'
};
console.log(validator.isValid('user_detail', data));

console.log('\nTest #3:');
data = {
    'name': 'Tom',
    'surname': 'Jerry',
    'isActive': true,
    contact_info: {
        'email': 'tom.jerry@example.com'
    }
}
console.log(validator.isValid('user_detail', data));

console.log('\nTest #4:');
data = {
    'name': 'Tom',
    'surname': 'Jerry',
    'isActive': true,
    contact_info: {
        'email': 'tom.jerry@example.com',
        'phone': '5556667777'
    }
}
console.log(validator.isValid('user_detail', data));
```

Output:

```
Test #1:
-----------Validator Error-----------
Object Path: data.isActive
Expected: boolean
Got: undefined
Value: undefined
-------------------------------------
-----------Validator Error-----------
Object Path: data.contact_info
Expected: object
Got: undefined
Value: undefined
-------------------------------------
false

Test #2:
-----------Validator Error-----------
Object Path: data.contact_info
Expected: object
Got: string
Value: tom.jerry@example.com
-------------------------------------
false

Test #3:
-----------Validator Error-----------
Object Path: data.contact_info.phone
Expected: string
Got: undefined
Value: undefined
-------------------------------------
false

Test #4:
true
```

Example using single-use configuration:

```javascript
var validator = require('@ibrokhim.shokirov/json-data-validator');

var singleUseConfig = {
    type: 'object',
    props: {
        name: {type: 'string'},
        surname: {type: 'string'},
        isActive: {type: 'boolean'},
        contact_info: {
            type: 'object',
            props: {
                email: {type: 'string'},
                phone: {type: 'string'}
            }
        }
    }
};

var data = {
    'name': 'Tom',
    'surname': 'Jerry',
    'isActive': true,
    contact_info: {
        'email': 'tom.jerry@example.com',
        'phone': '5556667777'
    }
};

console.log(
    validator.validate(singleUseConfig, data)
);
```

Output:

```
true
```



## License

[GNU General Public License v3.0](https://github.com/IIINIII/Json-Data-Validator/blob/master/LICENSE)

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