jsonschema
Validation Error
NPM Package For Formatting
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 = validateSchema;
Example Schema And Input Data
// Input Body Validation Schemalet 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 Bodylet p = 'names': 'Barack Obama' 'address': 'lines': '1600 Pennsylvania Avenue Northwest' 'zip': 'DC 20500' 'city': 'Washington' 'country': 1 'votes': 0 // Custom Defined Error Codeslet resCode = REQUIRED_NAME: 'CUST_ERR0001' TYPE_COUNTRY: 'CUST_ERR0002'
validateSchema
Function
Call /* 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*/
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