Skip to content

Commit d48f0fb

Browse files
michael-ciniawskyjoshwiens
authored andcommitted
feat: add ValidationError (#8)
* refactor(src/validateOptions): add schema && Error handling * feat(src/ValidationError): add Options Validation Error * test: simplify tests * docs(README): document validateOptions, add examples * chore: update webpack-defaults && dependencies * docs(README): add && update logo, fix typo * test: run tests on src files * refactor(ValidationError): hide err.stack
1 parent 7152276 commit d48f0fb

14 files changed

+1185
-1653
lines changed

.babelrc

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,38 @@
1515
"transform-regenerator"
1616
]
1717
}
18+
],
19+
[
20+
"env",
21+
{
22+
"useBuiltIns": true,
23+
"targets": {
24+
"node": 4.3
25+
},
26+
"exclude": [
27+
"transform-async-to-generator",
28+
"transform-regenerator"
29+
]
30+
}
1831
]
1932
],
2033
"ignore": [],
2134
"env": {
2235
"test": {
2336
"presets": [
2437
"env"
38+
],
39+
"plugins": [
40+
"transform-object-rest-spread"
2541
]
2642
}
27-
}
43+
},
44+
"plugins": [
45+
[
46+
"transform-object-rest-spread",
47+
{
48+
"useBuiltIns": true
49+
}
50+
]
51+
]
2852
}

README.md

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,66 @@
11
[![npm][npm]][npm-url]
2+
[![node][node]][node-url]
23
[![deps][deps]][deps-url]
34
[![test][test]][test-url]
45
[![coverage][cover]][cover-url]
5-
[![quality][quality]][quality-url]
66
[![chat][chat]][chat-url]
77

88
<div align="center">
9-
<!-- replace with accurate logo e.g from https://worldvectorlogo.com/ -->
9+
<a href="http://json-schema.org">
10+
<!-- src="https://webpack-contrib.github.io/schema-utils/logo.png" -->
11+
<img width="180" height="180"
12+
src="https://raw.githubusercontent.com/json-schema-org/json-schema-org.github.io/master/img/logo.png">
13+
</a>
1014
<a href="https://github.com/webpack/webpack">
11-
<img width="200" height="200" vspace="" hspace="25"
12-
src="https://worldvectorlogo.com/logos/webpack.svg">
15+
<img width="200" height="200" hspace="10"
16+
src="https://webpack.js.org/assets/icon-square-big.svg">
1317
</a>
14-
<h1>Webpack Schema Utilities</h1>
15-
<p>Schema validation utilities for Ajv<p>
18+
<h1>Schema Utils</h1>
1619
</div>
1720

1821
<h2 align="center">Install</h2>
1922

2023
```bash
21-
npm i schema-utils@latest
24+
npm install --save schema-utils
2225
```
2326

2427
<h2 align="center">Usage</h2>
2528

26-
### Validate Options
29+
### `validateOptions`
30+
31+
```js
32+
import validateOptions from 'schema-utils'
2733

34+
validateOptions('path/to/schema.json', options, 'Loader/Plugin Name')
35+
```
2836

2937
<h2 align="center">Examples</h2>
3038

31-
```javascript
32-
const { validateSchema } = require('schema-utils');
39+
### Loader
3340

34-
...
41+
```js
42+
import { getOptions } from 'loader-utils'
43+
import validateOptions from 'schema-utils'
3544

36-
// This injects possible default values to options
37-
const validation = validateSchema(jsonSchema, options);
45+
function loader (src, map) {
46+
const options = getOptions(this) || {}
3847

39-
if(validation.isValid) {
40-
throw new Error(validation.error);
48+
validateOptions('path/to/schema.json', options, 'Loader Name')
4149
}
42-
43-
// options are ok to use now
4450
```
4551

52+
### Plugin
53+
54+
```js
55+
import Tapable from 'tapable'
56+
import validateOptions from 'schema-utils'
57+
58+
class Plugin extends Tapable {
59+
constructor (options) {
60+
validateOptions('path/to/schema.json', options, 'Plugin Name')
61+
}
62+
}
63+
```
4664

4765
<h2 align="center">Maintainers</h2>
4866

@@ -51,34 +69,41 @@ if(validation.isValid) {
5169
<tr>
5270
<td align="center">
5371
<img width="150" height="150"
54-
src="https://avatars3.githubusercontent.com/u/166921?v=3&s=150">
72+
src="https://github.com/bebraw.png?v=3&s=150">
5573
</br>
5674
<a href="https://github.com/bebraw">Juho Vepsäläinen</a>
5775
</td>
5876
<td align="center">
5977
<img width="150" height="150"
60-
src="https://avatars2.githubusercontent.com/u/8420490?v=3&s=150">
78+
src="https://github.com/d3viant0ne.png?v=3&s=150">
6179
</br>
6280
<a href="https://github.com/d3viant0ne">Joshua Wiens</a>
6381
</td>
82+
<td align="center">
83+
<img width="150" height="150"
84+
src="https://github.com/michael-ciniawsky.png?v=3&s=150">
85+
</br>
86+
<a href="https://github.com/michael-ciniawsky">Michael Ciniawsky</a>
87+
</td>
6488
</tr>
6589
<tbody>
6690
</table>
6791

92+
6893
[npm]: https://img.shields.io/npm/v/schema-utils.svg
6994
[npm-url]: https://npmjs.com/package/schema-utils
7095

96+
[node]: https://img.shields.io/node/v/schema-utils.svg
97+
[node-url]: https://nodejs.org
98+
7199
[deps]: https://david-dm.org/webpack-contrib/schema-utils.svg
72100
[deps-url]: https://david-dm.org/webpack-contrib/schema-utils
73101

74-
[chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
75-
[chat-url]: https://gitter.im/webpack/webpack
76-
77102
[test]: http://img.shields.io/travis/webpack-contrib/schema-utils.svg
78103
[test-url]: https://travis-ci.org/webpack-contrib/schema-utils
79104

80105
[cover]: https://codecov.io/gh/webpack-contrib/schema-utils/branch/master/graph/badge.svg
81106
[cover-url]: https://codecov.io/gh/webpack-contrib/schema-utils
82107

83-
[quality]: https://www.bithound.io/github/webpack-contrib/schema-utils/badges/score.svg
84-
[quality-url]: https://www.bithound.io/github/webpack-contrib/schema-utils
108+
[chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
109+
[chat-url]: https://gitter.im/webpack/webpack

lib/post_install.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

logo.png

16.7 KB
Loading

package.json

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,60 @@
22
"name": "schema-utils",
33
"version": "0.2.1",
44
"description": "Webpack Schema Validation Utilities",
5-
"main": "dist/index.js",
5+
"main": "dist/cjs.js",
66
"files": [
7-
"dist",
8-
"lib",
9-
"src",
10-
".babelrc"
7+
"dist"
118
],
9+
"engines": {
10+
"node": ">= 4.3 < 5.0.0 || >= 5.10"
11+
},
1212
"scripts": {
13-
"prebuild": "yarn run clean:dist",
14-
"build": "cross-env NODE_ENV=production babel -s true src -d dist --ignore 'src/**/*.test.js'",
15-
"clean:dist": "del-cli dist",
16-
"start": "yarn run serve:dev src",
17-
"serve:dev": "nodemon $2 --exec babel-node",
13+
"start": "yarn run build -- -w",
14+
"prebuild": "yarn run clean",
15+
"build": "cross-env NODE_ENV=production babel src -d dist --ignore 'src/**/*.test.js'",
16+
"clean": "del-cli dist",
1817
"lint": "eslint --cache src test",
18+
"lint-staged": "lint-staged",
1919
"security": "nsp check",
2020
"test": "jest",
21+
"test:watch": "jest --watch",
2122
"test:coverage": "jest --collectCoverageFrom='src/**/*.js' --coverage",
22-
"travis:test": "yarn run test",
2323
"travis:coverage": "yarn run test:coverage",
2424
"travis:lint": "yarn run lint && yarn run security",
25+
"travis:test": "yarn run test",
2526
"webpack-defaults": "webpack-defaults",
26-
"lint-staged": "lint-staged",
27-
"postinstall": "node lib/post_install.js",
2827
"prepublish": "yarn run build",
2928
"release": "yarn run standard-version"
3029
},
3130
"dependencies": {
32-
"ajv": "^4.11.2"
31+
"ajv": "^5.0.0"
3332
},
3433
"devDependencies": {
35-
"babel-cli": "^6.22.2",
36-
"babel-jest": "^18.0.0",
37-
"babel-polyfill": "^6.22.0",
38-
"babel-preset-env": "^1.2.1",
34+
"babel-cli": "^6.24.1",
35+
"babel-jest": "^19.0.0",
36+
"babel-plugin-transform-object-rest-spread": "^6.23.0",
37+
"babel-polyfill": "^6.23.0",
38+
"babel-preset-env": "^1.4.0",
3939
"babel-preset-webpack": "^1.0.0",
40-
"chai": "^3.5.0",
41-
"codecov": "^1.0.1",
42-
"cross-env": "^3.1.4",
40+
"codecov": "^2.0.1",
41+
"cross-env": "^4.0.0",
4342
"del-cli": "^0.2.1",
44-
"eslint": "^3.14.1",
45-
"eslint-config-webpack": "^1.0.0",
43+
"eslint": "^3.19.0",
44+
"eslint-config-webpack": "^1.2.1",
4645
"eslint-plugin-import": "^2.2.0",
47-
"greenkeeper-postpublish": "^1.0.1",
48-
"jest": "^18.1.0",
49-
"lint-staged": "^3.3.1",
50-
"nodemon": "^1.11.0",
51-
"nsp": "^2.6.2",
46+
"jest": "^19.0.2",
47+
"lint-staged": "^3.4.0",
48+
"nsp": "^2.6.3",
5249
"pre-commit": "^1.2.2",
53-
"shx": "^0.2.2",
5450
"standard-version": "^4.0.0",
55-
"webpack-defaults": "^0.2.1"
56-
},
57-
"engines": {
58-
"node": ">=4.3 <5.0.0 || >=5.10"
51+
"webpack-defaults": "^0.4.5"
5952
},
60-
"repository": {
61-
"type": "git",
62-
"url": "git+https://github.com/webpack-contrib/schema-utils.git"
53+
"pre-commit": "lint-staged",
54+
"lint-staged": {
55+
"*.js": [
56+
"eslint --fix",
57+
"git add"
58+
]
6359
},
6460
"eslintConfig": {
6561
"extends": "webpack",
@@ -77,18 +73,18 @@
7773
},
7874
{
7975
"name": "Joshua Wiens <@d3viant0ne>"
76+
},
77+
{
78+
"name": "Michael Ciniawsky <@michael-ciniawsky>"
8079
}
8180
],
82-
"license": "MIT",
81+
"repository": {
82+
"type": "git",
83+
"url": "git+https://github.com/webpack-contrib/schema-utils.git"
84+
},
8385
"bugs": {
8486
"url": "https://github.com/webpack-contrib/schema-utils/issues"
8587
},
8688
"homepage": "https://github.com/webpack-contrib/schema-utils#readme",
87-
"pre-commit": "lint-staged",
88-
"lint-staged": {
89-
"*.js": [
90-
"eslint --fix",
91-
"git add"
92-
]
93-
}
89+
"license": "MIT"
9490
}

src/ValidationError.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class ValidationError extends Error {
2+
constructor(err, name) {
3+
super(err);
4+
5+
this.err = err;
6+
this.stack = false;
7+
8+
this.message = `Validation Error\n\n${name || ''} Invalid Options\n\n`;
9+
10+
err.forEach((msg) => {
11+
this.message += `options${msg.dataPath} ${msg.message}\n`;
12+
});
13+
14+
return this;
15+
}
16+
}
17+
18+
export default ValidationError;

src/cjs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./index').default;

src/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import validateOptions from './lib/validate-options';
1+
import validateOptions from './validateOptions';
22

3-
export {
4-
validateOptions, // eslint-disable-line import/prefer-default-export
5-
};
3+
export default validateOptions;

src/lib/validate-options.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)