Skip to content

Commit c7de73e

Browse files
authored
Merge pull request webpack#3218 from webpack/SpaceK33z-validate-custom-schema
Allow to validate a custom schema with `webpack.validate`
2 parents 14e55e3 + aa4c5a0 commit c7de73e

3 files changed

Lines changed: 12 additions & 11 deletions

File tree

bin/convert-argv.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ fs.existsSync = fs.existsSync || path.existsSync;
44
var resolve = require("enhanced-resolve");
55
var interpret = require("interpret");
66
var WebpackOptionsDefaulter = require("../lib/WebpackOptionsDefaulter");
7-
var validateWebpackOptions = require("../lib/validateWebpackOptions");
87

98
module.exports = function(yargs, argv, convertOptions) {
109

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Gajus Kuizinas @gajus
44
*/
5-
var webpackOptionsSchema = require("../schemas/webpackOptionsSchema.json");
65
var Ajv = require("ajv");
76
var ajv = new Ajv({
87
errorDataPath: "configuration",
98
allErrors: true,
109
verbose: true
1110
});
12-
var validate = ajv.compile(webpackOptionsSchema);
1311

14-
function validateWebpackOptions(options) {
12+
function validateSchema(schema, options) {
1513
if(Array.isArray(options)) {
16-
var errors = options.map(validateObject);
14+
var errors = options.map(validateObject.bind(this, schema));
1715
errors.forEach(function(list, idx) {
1816
list.forEach(function applyPrefix(err) {
1917
err.dataPath = "[" + idx + "]" + err.dataPath;
@@ -26,11 +24,12 @@ function validateWebpackOptions(options) {
2624
return arr.concat(items);
2725
}, []);
2826
} else {
29-
return validateObject(options);
27+
return validateObject(schema, options);
3028
}
3129
}
3230

33-
function validateObject(options) {
31+
function validateObject(schema, options) {
32+
var validate = ajv.compile(schema);
3433
var valid = validate(options);
3534
return valid ? [] : filterErrors(validate.errors);
3635
}
@@ -60,4 +59,4 @@ function filterErrors(errors) {
6059
return newErrors;
6160
}
6261

63-
module.exports = validateWebpackOptions;
62+
module.exports = validateSchema;

lib/webpack.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ var MultiCompiler = require("./MultiCompiler");
77
var NodeEnvironmentPlugin = require("./node/NodeEnvironmentPlugin");
88
var WebpackOptionsApply = require("./WebpackOptionsApply");
99
var WebpackOptionsDefaulter = require("./WebpackOptionsDefaulter");
10-
var validateWebpackOptions = require("./validateWebpackOptions");
10+
var validateSchema = require("./validateSchema");
1111
var WebpackOptionsValidationError = require("./WebpackOptionsValidationError");
12+
var webpackOptionsSchema = require("../schemas/webpackOptionsSchema.json");
1213

1314
function webpack(options, callback) {
14-
var webpackOptionsValidationErrors = validateWebpackOptions(options);
15+
var webpackOptionsValidationErrors = validateSchema(webpackOptionsSchema, options);
1516
if(webpackOptionsValidationErrors.length) {
1617
throw new WebpackOptionsValidationError(webpackOptionsValidationErrors);
1718
}
@@ -52,7 +53,9 @@ webpack.WebpackOptionsApply = WebpackOptionsApply;
5253
webpack.Compiler = Compiler;
5354
webpack.MultiCompiler = MultiCompiler;
5455
webpack.NodeEnvironmentPlugin = NodeEnvironmentPlugin;
55-
webpack.validate = validateWebpackOptions;
56+
webpack.validate = validateSchema.bind(this, webpackOptionsSchema);
57+
webpack.validateSchema = validateSchema;
58+
webpack.WebpackOptionsValidationError = WebpackOptionsValidationError;
5659

5760
function exportPlugins(exports, path, plugins) {
5861
plugins.forEach(function(name) {

0 commit comments

Comments
 (0)