From dfeb347a4a8ebb59f3cf9af4ff0f78d5d2965a67 Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Thu, 8 Jun 2017 01:09:30 +0300 Subject: [PATCH] fix: validation schema (`schema-utils`) (#527) --- index.js | 8 ++--- package.json | 2 +- schema/loader-schema.js | 19 ------------ schema/loader.json | 36 ++++++++++++++++++++++ schema/{plugin-schema.json => plugin.json} | 12 ++------ schema/validator.js | 13 -------- test/extract.test.js | 4 ++- 7 files changed, 46 insertions(+), 48 deletions(-) delete mode 100644 schema/loader-schema.js create mode 100644 schema/loader.json rename schema/{plugin-schema.json => plugin.json} (76%) delete mode 100644 schema/validator.js diff --git a/index.js b/index.js index cf734f7d..0b7b992c 100644 --- a/index.js +++ b/index.js @@ -9,9 +9,7 @@ var ExtractedModule = require("./ExtractedModule"); var Chunk = require("webpack/lib/Chunk"); var OrderUndefinedError = require("./OrderUndefinedError"); var loaderUtils = require("loader-utils"); -var schemaTester = require('./schema/validator'); -var loaderSchema = require('./schema/loader-schema'); -var pluginSchema = require('./schema/plugin-schema.json'); +var validateOptions = require('schema-utils'); var NS = fs.realpathSync(__dirname); @@ -122,7 +120,7 @@ function ExtractTextPlugin(options) { if(isString(options)) { options = { filename: options }; } else { - schemaTester(pluginSchema, options); + validateOptions('./schema/plugin.json', options, 'Extract Text Plugin'); } this.filename = options.filename; this.id = options.id != null ? options.id : ++nextId; @@ -201,7 +199,7 @@ ExtractTextPlugin.prototype.extract = function(options) { if(Array.isArray(options) || isString(options) || typeof options.options === "object" || typeof options.query === 'object') { options = { loader: options }; } else { - schemaTester(loaderSchema, options); + validateOptions('./schema/loader.json', options, 'Extract Text Plugin (Loader)'); } var loader = options.use ||  options.loader; var before = options.fallback || options.fallbackLoader || []; diff --git a/package.json b/package.json index 27a731d3..9bff38b7 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "webpack": "^2.2.0" }, "dependencies": { - "ajv": "^4.11.2", + "schema-utils": "^0.3.0", "async": "^2.1.2", "loader-utils": "^1.0.2", "webpack-sources": "^0.1.0" diff --git a/schema/loader-schema.js b/schema/loader-schema.js deleted file mode 100644 index 80266761..00000000 --- a/schema/loader-schema.js +++ /dev/null @@ -1,19 +0,0 @@ -module.exports = { - "$schema": "http://json-schema.org/draft-04/schema#", - "type": "object", - "additionalProperties": false, - "properties": { - "allChunks": { "type": "boolean"}, - "disable": { "type": "boolean" }, - "omit": { "type": "boolean" }, - "remove": { "type": "boolean" }, - "fallback": { "type": ["string", "array", "object"] }, - "filename": { "type": "string" }, - "use": { "type": ["string", "array", "object"] }, - "publicPath": { "type": "string" }, - - // deprecated - "fallbackLoader": { "type": ["string", "array", "object"] }, - "loader": { "type": ["string", "array", "object"] } - } -}; diff --git a/schema/loader.json b/schema/loader.json new file mode 100644 index 00000000..bca660af --- /dev/null +++ b/schema/loader.json @@ -0,0 +1,36 @@ +{ + "type": "object", + "additionalProperties": false, + "properties": { + "allChunks": { + "type": "boolean" + }, + "disable": { + "type": "boolean" + }, + "omit": { + "type": "boolean" + }, + "remove": { + "type": "boolean" + }, + "fallback": { + "type": ["string", "array", "object"] + }, + "filename": { + "type": "string" + }, + "use": { + "type": ["string", "array", "object"] + }, + "publicPath": { + "type": "string" + }, + "fallbackLoader": { + "type": ["string", "array", "object"] + }, + "loader": { + "type": ["string", "array", "object"] + } + } +} diff --git a/schema/plugin-schema.json b/schema/plugin.json similarity index 76% rename from schema/plugin-schema.json rename to schema/plugin.json index 7b507d17..4fcf2e66 100644 --- a/schema/plugin-schema.json +++ b/schema/plugin.json @@ -1,5 +1,4 @@ { - "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "additionalProperties": false, "properties": { @@ -14,16 +13,13 @@ "fallback": { "description": "A loader that webpack can fall back to if the original one fails.", "modes": { - "type": "string", - "type": "object", - "type": "array" + "type": ["string", "object", "array"] } }, "filename": { "description": "The filename and path that ExtractTextPlugin will extract to", "modes": { - "type": "string", - "type": "function" + "type": ["string", "function"] } }, "ignoreOrder": { @@ -33,9 +29,7 @@ "loader": { "description": "The loader that ExtractTextPlugin will attempt to load through.", "modes": { - "type": "string", - "type": "object", - "type": "array" + "type": ["string", "object", "array"] } }, "publicPath": { diff --git a/schema/validator.js b/schema/validator.js deleted file mode 100644 index 5e5f9174..00000000 --- a/schema/validator.js +++ /dev/null @@ -1,13 +0,0 @@ -var Ajv = require('ajv'); -var ajv = new Ajv({allErrors: true}); - -module.exports = function validate(schema, data) { - var ajv = new Ajv({ - errorDataPath: 'property' - }); - var isValid = ajv.validate(schema, data); - - if(!isValid) { - throw new Error(ajv.errorsText()); - } -} diff --git a/test/extract.test.js b/test/extract.test.js index b07b9d7a..3c21fa9b 100644 --- a/test/extract.test.js +++ b/test/extract.test.js @@ -29,7 +29,9 @@ describe("ExtractTextPlugin.extract()", function() { ExtractTextPlugin.extract({style: 'file.css'}); }, function(err) { - return err.message === 'data[\'style\'] should NOT have additional properties'; + return err.message === 'Validation Error\n\n' + + 'Extract Text Plugin (Loader) Invalid Options\n\n' + + 'options[\'style\'] should NOT have additional properties\n'; } ); });