22
33var Ajv = require ( 'ajv' )
44var merge = require ( 'deepmerge' )
5-
6- // This Ajv instance is used to validate that the passed schema
7- // is valid json schema. We reuse the instance to avoid having to
8- // pay the ajv creation cost more than once.
9- var ajv = new Ajv ( {
10- // Ignore any unknown formats as they aren't used.
11- unknownFormats : 'ignore' ,
12-
13- // Ignoring unknown formats emits warnings, but we don't need to hear about
14- // them.
15- logger : {
16- log : console . log ,
17- warn : function ( ) { } ,
18- error : console . error
19- }
20- } )
5+ var validate = require ( './schema-validator' )
216
227var uglify = null
238var isLong
@@ -34,9 +19,29 @@ var addComma = `
3419 addComma = true
3520`
3621
22+ function isValidSchema ( schema , name ) {
23+ if ( ! validate ( schema ) ) {
24+ if ( name ) {
25+ name = `"${ name } " `
26+ } else {
27+ name = ''
28+ }
29+ const first = validate . errors [ 0 ]
30+ const err = new Error ( `${ name } schema is invalid: data${ first . dataPath } ${ first . message } ` )
31+ err . errors = isValidSchema . errors
32+ throw err
33+ }
34+ }
35+
3736function build ( schema , options ) {
3837 options = options || { }
39- isValidSchema ( schema , options . schema )
38+ isValidSchema ( schema )
39+ if ( options . schema ) {
40+ for ( let key of Object . keys ( options . schema ) ) {
41+ isValidSchema ( options . schema [ key ] , key )
42+ }
43+ }
44+
4045 /* eslint no-new-func: "off" */
4146 var code = `
4247 'use strict'
@@ -985,21 +990,6 @@ function loadUglify () {
985990 }
986991}
987992
988- function isValidSchema ( schema , externalSchema ) {
989- if ( externalSchema ) {
990- Object . keys ( externalSchema ) . forEach ( key => {
991- try {
992- ajv . addSchema ( externalSchema [ key ] , key )
993- } catch ( err ) {
994- err . message = '"' + key + '" ' + err . message
995- throw err
996- }
997- } )
998- }
999- ajv . compile ( schema )
1000- ajv . removeSchema ( )
1001- }
1002-
1003993function isEmpty ( schema ) {
1004994 for ( var key in schema ) {
1005995 if ( schema . hasOwnProperty ( key ) ) return false
0 commit comments