This is utility package that helps in validating the json schema. It uses ajv package internally to validate
- Validate through json schema
- Validate through json filename
npm i json-validator-util
OR
yarn json-validator-util
// 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
}
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);
}