# JSON Validator

This is utility package that helps in validating the json schema. It uses ajv package internally to validate


## Features

- Validate through json schema
- Validate through json filename


## Installation

```bash
  npm i json-validator-utill
  OR
  yarn json-validator-utill
```
    
## Usage/Examples

### Usage

```javascript
// validating through schema directly
import { validator } from 'json-validator-utill';

const response = validator.validate(payload, jsonSchema);
if(!response.isValid) {
    console.log(response.errors);
}
```

```javascript
// validating through schema filename
import { validator } from 'json-validator-utill';

validator.initialize({
    schemaPath: 'relative/path/to/folder'
})

const response = validator.validate(payload, 'remaining_path/filename.json');
if(!response.isValid) {
    console.log(response.errors);
}
```

```javascript
// if need to get ajv original error then pass third parameter value as true

const response = validator.validate(first_param, second_param, true);
if(!response.isValid) {
    console.log(response.errors); // will print original ajv style error if validation fails
}
```

### Example

```javascript
import { validator } from 'json-validator-utill';

const payload = {
    foo: 123,
    bar: "value"
};
const jsonSchema = {
  "type": "object",
  "properties": {
    "foo": {
      "type": "integer"
    },
    "bar": {
      "type": "string"
    }
  },
  "required": [
    "foo"
  ],
  "additionalProperties": false
}

const response = validator.validate(payload, jsonSchema);
if(!response.isValid) {
    console.log(response.errors);
}
```