22 MIT License http://www.opensource.org/licenses/mit-license.php
33 Author Gajus Kuizinas @gajus
44*/
5- var Ajv = require ( "ajv" ) ;
6- var ajv = new Ajv ( {
5+ "use strict" ;
6+
7+ const Ajv = require ( "ajv" ) ;
8+ const ajv = new Ajv ( {
79 errorDataPath : "configuration" ,
810 allErrors : true ,
911 verbose : true
@@ -12,16 +14,16 @@ require("ajv-keywords")(ajv, ["instanceof"]);
1214
1315function validateSchema ( schema , options ) {
1416 if ( Array . isArray ( options ) ) {
15- var errors = options . map ( validateObject . bind ( this , schema ) ) ;
16- errors . forEach ( function ( list , idx ) {
17+ const errors = options . map ( ( options ) => validateObject ( schema , options ) ) ;
18+ errors . forEach ( ( list , idx ) => {
1719 list . forEach ( function applyPrefix ( err ) {
1820 err . dataPath = "[" + idx + "]" + err . dataPath ;
1921 if ( err . children ) {
2022 err . children . forEach ( applyPrefix ) ;
2123 }
2224 } ) ;
2325 } ) ;
24- return errors . reduce ( function ( arr , items ) {
26+ return errors . reduce ( ( arr , items ) => {
2527 return arr . concat ( items ) ;
2628 } , [ ] ) ;
2729 } else {
@@ -30,20 +32,20 @@ function validateSchema(schema, options) {
3032}
3133
3234function validateObject ( schema , options ) {
33- var validate = ajv . compile ( schema ) ;
34- var valid = validate ( options ) ;
35+ const validate = ajv . compile ( schema ) ;
36+ const valid = validate ( options ) ;
3537 return valid ? [ ] : filterErrors ( validate . errors ) ;
3638}
3739
3840function filterErrors ( errors ) {
39- var newErrors = [ ] ;
40- errors . forEach ( function ( err ) {
41- var dataPath = err . dataPath ;
42- var children = [ ] ;
43- newErrors = newErrors . filter ( function ( oldError ) {
44- if ( oldError . dataPath . indexOf ( dataPath ) >= 0 ) {
41+ let newErrors = [ ] ;
42+ errors . forEach ( ( err ) => {
43+ const dataPath = err . dataPath ;
44+ let children = [ ] ;
45+ newErrors = newErrors . filter ( ( oldError ) => {
46+ if ( oldError . dataPath . includes ( dataPath ) ) {
4547 if ( oldError . children ) {
46- oldError . children . forEach ( function ( child ) {
48+ oldError . children . forEach ( ( child ) => {
4749 children . push ( child ) ;
4850 } ) ;
4951 }
@@ -58,7 +60,7 @@ function filterErrors(errors) {
5860 }
5961 newErrors . push ( err ) ;
6062 } ) ;
61- //console.log(JSON.stringify(newErrors, 0, 2));
63+
6264 return newErrors ;
6365}
6466
0 commit comments