2
2
3
3
var Ajv = require ( 'ajv' )
4
4
var 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' )
21
6
22
7
var uglify = null
23
8
var isLong
@@ -34,9 +19,29 @@ var addComma = `
34
19
addComma = true
35
20
`
36
21
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
+
37
36
function build ( schema , options ) {
38
37
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
+
40
45
/* eslint no-new-func: "off" */
41
46
var code = `
42
47
'use strict'
@@ -985,21 +990,6 @@ function loadUglify () {
985
990
}
986
991
}
987
992
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
-
1003
993
function isEmpty ( schema ) {
1004
994
for ( var key in schema ) {
1005
995
if ( schema . hasOwnProperty ( key ) ) return false
0 commit comments