File size: 1,578 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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', '' ];

		// Build a "header" which allows the schema to be easily identified
		// from the error message. Use title and/or description if provided.
		// Otherwise, print the entire schema.
		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 } ) => {
			// data.myField is required
			msg.push( `${ field } ${ message }` );

			// Found: { my: 'state' }
			msg.push( `Found: ${ JSON.stringify( value ) }` );

			// Violates rule: { type: 'boolean' }
			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 );
};