| | import imjv from 'is-my-json-valid'; |
| | import { forEach, get, isEmpty } from 'lodash'; |
| | import jsonSchemaDraft04 from './lib/json-schema-draft-04.json'; |
| |
|
| | const validateSchema = imjv( jsonSchemaDraft04, { verbose: true, greedy: true } ); |
| |
|
| | function throwOnInvalidSchema( schema ) { |
| | if ( ! validateSchema( schema ) ) { |
| | const msg = [ 'Invalid schema received', '' ]; |
| |
|
| | |
| | |
| | |
| | if ( schema.title || schema.description ) { |
| | if ( schema.title ) { |
| | msg.push( 'Title: ' + schema.title ); |
| | } |
| | if ( schema.description ) { |
| | msg.push( 'Description: ' + schema.description ); |
| | } |
| | msg.push( '' ); |
| | } else { |
| | msg.push( JSON.stringify( schema, undefined, 2 ), '' ); |
| | } |
| |
|
| | forEach( validateSchema.errors, ( { field, message, schemaPath, value } ) => { |
| | |
| | msg.push( `${ field } ${ message }` ); |
| |
|
| | |
| | msg.push( `Found: ${ JSON.stringify( value ) }` ); |
| |
|
| | |
| | if ( ! isEmpty( schemaPath ) ) { |
| | msg.push( `Violates rule: ${ JSON.stringify( get( jsonSchemaDraft04, schemaPath ) ) }` ); |
| | } |
| | msg.push( '' ); |
| | } ); |
| | throw new Error( msg.join( '\n' ) ); |
| | } |
| | } |
| |
|
| | export default function validator( schema, options ) { |
| | throwOnInvalidSchema( schema ); |
| | return imjv( schema, options ); |
| | } |
| |
|
| | validator.filter = ( schema, options ) => { |
| | throwOnInvalidSchema( schema ); |
| | return imjv.filter( schema, options ); |
| | }; |
| |
|