json-validator-util
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

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

  npm i json-validator-util
  OR
  yarn json-validator-util

Usage/Examples

Usage

// validating through schema directly
import { validator } from 'json-validator-util';

const response = validator.validate(payload, jsonSchema);
if(!response.isValid) {
    console.log(response.errors);
}
// validating through schema filename
import { validator } from 'json-validator-util';

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

const response = validator.validate(payload, 'remaining_path/filename.json');
if(!response.isValid) {
    console.log(response.errors);
}
// 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

import { validator } from 'json-validator-util';

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

Readme

Keywords

none

Package Sidebar

Install

npm i json-validator-util

Weekly Downloads

84

Version

1.0.1

License

ISC

Unpacked Size

24.3 kB

Total Files

16

Last publish

Collaborators

  • satyam-bhadoria