Skip to content

Commit 5b29f9a

Browse files
authored
Merge pull request #358 from scottdj92/master
Update to include JSON schema and some validation
2 parents 35248ed + 75435b7 commit 5b29f9a

File tree

6 files changed

+85
-0
lines changed

6 files changed

+85
-0
lines changed

index.js

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var ExtractedModule = require("./ExtractedModule");
99
var Chunk = require("webpack/lib/Chunk");
1010
var OrderUndefinedError = require("./OrderUndefinedError");
1111
var loaderUtils = require("loader-utils");
12+
var schemaTester = require('./schema/validator');
1213

1314
var NS = fs.realpathSync(__dirname);
1415

@@ -118,6 +119,8 @@ function ExtractTextPlugin(options) {
118119
}
119120
if(isString(options)) {
120121
options = { filename: options };
122+
} else {
123+
schemaTester(options);
121124
}
122125
this.filename = options.filename;
123126
this.id = options.id != null ? options.id : ++nextId;
@@ -181,6 +184,8 @@ ExtractTextPlugin.prototype.extract = function(options) {
181184
}
182185
if(Array.isArray(options) || isString(options) || typeof options.options === "object" || typeof options.query === 'object') {
183186
options = { loader: options };
187+
} else {
188+
schemaTester(options);
184189
}
185190
var loader = options.loader;
186191
var before = options.fallbackLoader || [];

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"webpack-sources": "^0.1.0"
1616
},
1717
"devDependencies": {
18+
"ajv": "^4.11.2",
1819
"codecov.io": "^0.1.2",
1920
"coveralls": "^2.11.2",
2021
"css-loader": "^0.26.1",

schema/schema.json

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"type": "object",
4+
"additionalProperties": false,
5+
"properties": {
6+
"allChunks": {
7+
"description": "",
8+
"type": "boolean"
9+
},
10+
"disable": {
11+
"description": "",
12+
"type": "boolean"
13+
},
14+
"fallbackLoader": {
15+
"description": "A loader that webpack can fall back to if the original one fails.",
16+
"modes": {
17+
"type": "string",
18+
"type": "object",
19+
"type": "array"
20+
}
21+
},
22+
"filename": {
23+
"description": "The filename and path that ExtractTextPlugin will extract to",
24+
"type": "string"
25+
},
26+
"loader": {
27+
"description": "The loader that ExtractTextPlugin will attempt to load through.",
28+
"modes": {
29+
"type": "string",
30+
"type": "object",
31+
"type": "array"
32+
}
33+
},
34+
"publicPath": {
35+
"description": "",
36+
"type": "string"
37+
}
38+
}
39+
}

schema/valid.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"allChunks": { "type": "boolean"},
3+
"disable": { "type": "boolean" },
4+
"fallbackLoader": { "type": "string" },
5+
"filename": { "type": "string" },
6+
"loader": { "type": "string" },
7+
"publicPath": { "type": "string" }
8+
}

schema/validator.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var Ajv = require('ajv');
2+
var ajv = new Ajv({allErrors: true});
3+
var json = require('./schema.json');
4+
5+
module.exports = function validate(data) {
6+
var validSchema = ajv.compile(json);
7+
var valid = validSchema(data);
8+
9+
if(!valid) {
10+
throw new Error(ajv.errorsText());
11+
}
12+
}

test/extract.test.js

+20
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,26 @@ describe("ExtractTextPlugin.extract()", function() {
1010
});
1111
});
1212

13+
context("json schema validation", function() {
14+
it("does not throw if a filename is specified", function() {
15+
should.doesNotThrow(function() {
16+
ExtractTextPlugin.extract("file.css");
17+
});
18+
});
19+
20+
it("does not throw if a correct config object is passed in", function() {
21+
should.doesNotThrow(function() {
22+
ExtractTextPlugin.extract({loader: 'css-loader'});
23+
});
24+
});
25+
26+
it("throws if an incorrect config is passed in", function() {
27+
should.throws(function() {
28+
ExtractTextPlugin.extract({style: 'file.css'});
29+
});
30+
});
31+
});
32+
1333
context("specifying loader", function() {
1434
it("accepts a loader string", function() {
1535
ExtractTextPlugin.extract("css-loader").should.deepEqual([

0 commit comments

Comments
 (0)