jsonschema-formatter
v1.0.3
Published
Convert and output jsonschema validation error in more readable format
Downloads
10
Maintainers
Readme
NPM Package For Formatting jsonschema
Validation Error
Get jsonschema
npm package validation error in more readable and usable format.
You don't need to install jsonschema
package seperately, just install this jsonschema-formatter
package and start using.
This package internally call jsonschema
function and then formats its validation response in more readable format.
This package returns a Promise. For promise we are using q package.
Installation
Install with the node package manager npm:
$ npm install jsonschema-formatter
How To Use?
Import/require package in your application
const validateSchema = require('jsonschema-formatter').validateSchema;
Example Schema And Input Data
// Input Body Validation Schema
let schema = {
'id': '/SimplePerson',
'type': 'object',
'properties': {
'name': {'type': 'string'},
'address': {
'type': 'object',
'properties': {
'lines': {
'type': 'array',
'items': {'type': 'string'}
},
'zip': {'type': 'string'},
'city': {'type': 'string'},
'country': {'type': 'string'}
},
'required': ['country']
},
'votes': {'type': 'integer', 'minimum': 1}
},
'required': ['name']
}
// Input Body
let p = {
'names': 'Barack Obama',
'address': {
'lines': [ '1600 Pennsylvania Avenue Northwest' ],
'zip': 'DC 20500',
'city': 'Washington',
'country': 1
},
'votes': 0
}
// Custom Defined Error Codes
let resCode = {
REQUIRED_NAME: 'CUST_ERR0001',
TYPE_COUNTRY: 'CUST_ERR0002'
}
Call validateSchema
Function
/*
here p and schema refers to above step, resCode is optional if you define your custom error codes
then pass it else pre-defined error codes will be assigned
*/
validateSchema(p, schema, resCode)
.then((validationResult) => {
console.log('RESULT: ', validationResult)
})
.catch((err) => {
console.log('VALIDATION ERR: ', err)
})
.done()
Output Of Above Vaidation
VALIDATION ERR: {
status: 'fail',
errors: [ { code: 'CUST_ERR0002', // CODE FROM resCode YOUR DEFINED
error: 'country is not of a type(s) string',
parameter: 'country',
line: null },
{ code: 'ERR0006',
error: 'votes must have a minimum value of 1',
parameter: 'votes',
line: null },
{ code: 'ERR0001',
error: 'property name is missing',
parameter: 'name',
line: null } ]
}
Output Parameter Definition
code
: User Defined Or Pre-defined Error Code To Uniquely Identify Specific Errorerror
: Text Explaining What Error Occuredparameter
: Request Body Parameter(Key) For Which Error Occuredline
: In Case Of Array Which Index Element Has Error
How To Define Custom Error Codes
| Error Codes | Error Type | Example (from body used above) |
| ------------- | ------------- | ------------- |
| REQUIRED_<your-parameter-name-in-uppercase>
| Required field error | For missing name property { REQUIRED_NAME: 'CUST_ERR001' }
|
| TYPE_<your-parameter-name-in-uppercase>
| Field type error | For country peoperty type error { TYPE_COUNTRY: 'CUST_ERR002' }
|
| FORMAT_<your-parameter-name-in-uppercase>
| Field format error | Email format error |
| MINITEMS_<your-parameter-name-in-uppercase>
| Array minimum item check | Empty array check |
| UNIQUEITEMS_<your-parameter-name-in-uppercase>
| Array element duplication | Duplicate item check |
| MINIMUM_<your-parameter-name-in-uppercase>
| Field minimum value check | For votes peoperty min value error { MINIMUM_VOTES: 'CUST_ERR003' }
|
| ANYOF_<your-parameter-name-in-uppercase>
| Either-or field check | Any one of fields should be present |
Changelog
- 1.0.3 q lib removed and implemented new Promise()
- 1.0.2 Fuction output changes
- 1.0.0 Initial version