Skip to content

Commit 5bb81fc

Browse files
committed
feat - Add stricter plugin option validation
1 parent c9744fc commit 5bb81fc

File tree

7 files changed

+96
-6
lines changed

7 files changed

+96
-6
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## purifycss-webpack
22

3+
0.2.0 / 2017-01-24
4+
==================
5+
6+
* Feature - Add stricter plugin option validation.
7+
38
0.1.1 / 2017-01-23
49
==================
510

__tests__/test-validate-options.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const assert = require('assert');
2+
const validateOptions = require('../src/validate-options').default;
3+
4+
describe('Validate options', function () {
5+
it('fails empty data', function () {
6+
const result = validateOptions();
7+
8+
assert.ok(!result.isValid);
9+
assert.ok(result.error);
10+
});
11+
12+
it('fails empty data', function () {
13+
const result = validateOptions({});
14+
15+
assert.ok(!result.isValid);
16+
assert.ok(result.error);
17+
});
18+
19+
it('does not fail if paths are provided', function () {
20+
const result = validateOptions({ paths: ['./foo'] });
21+
22+
assert.ok(result.isValid);
23+
assert.ok(!result.error);
24+
});
25+
26+
it('does not allow arbitrary properties', function () {
27+
const result = validateOptions({ paths: ['./foo'], foobar: ['./foo'] });
28+
29+
assert.ok(!result.isValid);
30+
assert.ok(result.error);
31+
});
32+
});

examples/webpack.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module.exports = [
2525
parts.purifyCSS({
2626
verbose: true,
2727
paths: glob.sync(`${PATHS.app}/*`),
28-
extensions: ['.css', '.html']
28+
fileExtensions: ['.css']
2929
})
3030
),
3131
merge(
@@ -45,7 +45,7 @@ module.exports = [
4545
first: glob.sync(`${PATHS.app}/*`),
4646
second: glob.sync(`${PATHS.another}/*`)
4747
},
48-
extensions: ['.css', '.html']
48+
fileExtensions: ['.css']
4949
})
5050
)
5151
];

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@
4646
"webpack": "^1.9 || 2 || ^2.1.0-beta || ^2.2.0-rc"
4747
},
4848
"dependencies": {
49-
"webpack-sources": "^0.1.4",
50-
"purify-css": "^1.1.9"
49+
"ajv": "^4.11.2",
50+
"purify-css": "^1.1.9",
51+
"webpack-sources": "^0.1.4"
5152
},
5253
"devDependencies": {
5354
"babel-cli": "^6.18.0",

src/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ const purify = require('purify-css');
22
const ConcatSource = require('webpack-sources').ConcatSource;
33
const parse = require('./parse');
44
const search = require('./search');
5+
const validateOptions = require('./validate-options').default;
56

67
module.exports = function PurifyPlugin(options) {
7-
if (typeof options !== 'object' || !options.paths) {
8-
throw new Error('You should pass an options object containing an array of paths at least');
8+
const validation = validateOptions(options);
9+
10+
if (!validation.isValid) {
11+
throw new Error(validation.error);
912
}
1013

1114
return {

src/schema.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
export default {
2+
$schema: 'http://json-schema.org/draft-04/schema#',
3+
additionalProperties: false,
4+
type: 'object',
5+
properties: {
6+
fileExtensions: {
7+
type: 'array',
8+
items: {
9+
type: 'string'
10+
}
11+
},
12+
moduleExtensions: {
13+
type: 'array',
14+
items: {
15+
type: 'string'
16+
}
17+
},
18+
paths: {
19+
type: ['array', 'object'],
20+
items: {
21+
type: 'string'
22+
}
23+
},
24+
purifyOptions: {
25+
type: 'object',
26+
properties: {}
27+
},
28+
verbose: {
29+
type: 'boolean'
30+
}
31+
},
32+
required: [
33+
'paths'
34+
]
35+
};

src/validate-options.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Ajv from 'ajv';
2+
import schema from './schema';
3+
4+
function validateOptions(data) {
5+
const ajv = new Ajv();
6+
const isValid = ajv.validate(schema, data);
7+
8+
return {
9+
isValid,
10+
error: ajv.errors && ajv.errorsText()
11+
};
12+
}
13+
14+
export default validateOptions;

0 commit comments

Comments
 (0)