npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsonschema-formatter

v1.0.3

Published

Convert and output jsonschema validation error in more readable format

Downloads

24

Readme

NPM Package For Formatting jsonschema Validation Error

HitCount Generic badge GitHub license GitHub contributors GitHub issues GitHub issues-closed

NPM

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 Error
  • error: Text Explaining What Error Occured
  • parameter: Request Body Parameter(Key) For Which Error Occured
  • line: 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