Skip to content

Commit fe217ab

Browse files
refactor of ValidateSchema and Validation.test to es6
1 parent 47c2782 commit fe217ab

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

lib/validateSchema.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
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

1315
function 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

3234
function 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

3840
function 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

test/Validation.test.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
var should = require("should");
2-
var webpack = require("../lib/webpack");
3-
var WebpackOptionsValidationError = require("../lib/WebpackOptionsValidationError");
1+
"use strict";
2+
3+
const should = require("should");
4+
const webpack = require("../lib/webpack");
5+
const WebpackOptionsValidationError = require("../lib/WebpackOptionsValidationError");
46

57
describe("Validation", function() {
6-
var testCases = [{
8+
const testCases = [{
79
name: "undefined configuration",
810
config: undefined,
911
message: [

0 commit comments

Comments
 (0)