json-validator-util
v1.0.1
Published
This is utility package that helps in validating the json schema
Downloads
527
Readme
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);
}