Skip to content

Commit 0512f32

Browse files
refactor: code (#41)
1 parent fc8c2de commit 0512f32

File tree

12 files changed

+8805
-2382
lines changed

12 files changed

+8805
-2382
lines changed

README.md

Lines changed: 120 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ npm install schema-utils
3131

3232
## API
3333

34-
### validateOptions
35-
3634
**schema.json**
3735

3836
```json
@@ -43,20 +41,117 @@ npm install schema-utils
4341
"type": ["boolean"]
4442
}
4543
},
46-
"errorMessage": {
47-
"option": "should be {Boolean} (https:/github.com/org/repo#anchor)"
48-
},
4944
"additionalProperties": false
5045
}
5146
```
5247

5348
```js
5449
import schema from './path/to/schema.json';
55-
import validateOptions from 'schema-utils';
50+
import validate from 'schema-utils';
5651

5752
const options = { option: true };
53+
const configuration = { name: 'Loader Name/Plugin Name/Name' };
5854

59-
validateOptions(schema, options, 'Loader/Plugin Name');
55+
validate(schema, options, configuration);
56+
```
57+
58+
### `schema`
59+
60+
Type: `String`
61+
62+
JSON schema.
63+
64+
Simple example of schema:
65+
66+
```json
67+
{
68+
"type": "object",
69+
"properties": {
70+
"name": {
71+
"description": "This is description of option.",
72+
"type": "string"
73+
}
74+
},
75+
"additionalProperties": false
76+
}
77+
```
78+
79+
### `options`
80+
81+
Type: `Object`
82+
83+
Object with options.
84+
85+
```js
86+
validate(
87+
schema,
88+
{
89+
name: 123,
90+
},
91+
{ name: 'MyPlugin' }
92+
);
93+
```
94+
95+
### `configuration`
96+
97+
Allow to configure validator.
98+
99+
#### `name`
100+
101+
Type: `Object`
102+
Default: `"Object"`
103+
104+
Allow to setup name in validation errors.
105+
106+
```js
107+
validate(schema, options, { name: 'MyPlugin' });
108+
```
109+
110+
```shell
111+
Invalid configuration object. MyPlugin has been initialised using a configuration object that does not match the API schema.
112+
- configuration.optionName should be a integer.
113+
```
114+
115+
#### `baseDataPath`
116+
117+
Type: `String`
118+
Default: `"configuration"`
119+
120+
Allow to setup base data path in validation errors.
121+
122+
```js
123+
validate(schema, options, { name: 'MyPlugin', baseDataPath: 'options' });
124+
```
125+
126+
```shell
127+
Invalid options object. MyPlugin has been initialised using an options object that does not match the API schema.
128+
- options.optionName should be a integer.
129+
```
130+
131+
#### `postFormatter`
132+
133+
Type: `Function`
134+
Default: `undefined`
135+
136+
Allow to reformat errors.
137+
138+
```js
139+
validate(schema, options, {
140+
name: 'MyPlugin',
141+
postFormatter: (formattedError, error) => {
142+
if (error.keyword === 'type') {
143+
return `${formattedError}\nAdditional Information.`;
144+
}
145+
146+
return formattedError;
147+
},
148+
});
149+
```
150+
151+
```shell
152+
Invalid options object. MyPlugin has been initialised using an options object that does not match the API schema.
153+
- options.optionName should be a integer.
154+
Additional Information.
60155
```
61156

62157
## Examples
@@ -99,10 +194,15 @@ import schema from 'path/to/schema.json';
99194
function loader(src, map) {
100195
const options = getOptions(this) || {};
101196

102-
validateOptions(schema, options, 'Loader Name');
197+
validateOptions(schema, options, {
198+
name: 'Loader Name',
199+
baseDataPath: 'options',
200+
});
103201

104202
// Code...
105203
}
204+
205+
export default loader;
106206
```
107207

108208
### `Plugin`
@@ -114,7 +214,10 @@ import schema from 'path/to/schema.json';
114214

115215
class Plugin {
116216
constructor(options) {
117-
validateOptions(schema, options, 'Plugin Name');
217+
validateOptions(schema, options, {
218+
name: 'Plugin Name',
219+
baseDataPath: 'options',
220+
});
118221

119222
this.options = options;
120223
}
@@ -123,6 +226,8 @@ class Plugin {
123226
// Code...
124227
}
125228
}
229+
230+
export default Plugin;
126231
```
127232

128233
## Contributing
@@ -139,12 +244,12 @@ Please take a moment to read our contributing guidelines if you haven't yet done
139244
[npm-url]: https://npmjs.com/package/schema-utils
140245
[node]: https://img.shields.io/node/v/schema-utils.svg
141246
[node-url]: https://nodejs.org
142-
[deps]: https://david-dm.org/webpack-contrib/schema-utils.svg
143-
[deps-url]: https://david-dm.org/webpack-contrib/schema-utils
144-
[tests]: https://dev.azure.com/webpack-contrib/schema-utils/_apis/build/status/webpack-contrib.schema-utils?branchName=master
145-
[tests-url]: https://dev.azure.com/webpack-contrib/schema-utils/_build/latest?definitionId=2&branchName=master
146-
[cover]: https://codecov.io/gh/webpack-contrib/schema-utils/branch/master/graph/badge.svg
147-
[cover-url]: https://codecov.io/gh/webpack-contrib/schema-utils
247+
[deps]: https://david-dm.org/webpack/schema-utils.svg
248+
[deps-url]: https://david-dm.org/webpack/schema-utils
249+
[tests]: https://dev.azure.com/webpack/schema-utils/_apis/build/status/webpack.schema-utils?branchName=master
250+
[tests-url]: https://dev.azure.com/webpack/schema-utils/_build/latest?definitionId=9&branchName=master
251+
[cover]: https://codecov.io/gh/webpack/schema-utils/branch/master/graph/badge.svg
252+
[cover-url]: https://codecov.io/gh/webpack/schema-utils
148253
[chat]: https://badges.gitter.im/webpack/webpack.svg
149254
[chat-url]: https://gitter.im/webpack/webpack
150255
[size]: https://packagephobia.now.sh/badge?p=schema-utils

azure-pipelines.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
pool:
3737
vmImage: ubuntu-16.04
3838
strategy:
39-
maxParallel: 4
39+
maxParallel: 3
4040
matrix:
4141
node-12:
4242
node_version: ^12.0.0
@@ -79,7 +79,7 @@ jobs:
7979
pool:
8080
vmImage: macOS-10.14
8181
strategy:
82-
maxParallel: 4
82+
maxParallel: 3
8383
matrix:
8484
node-12:
8585
node_version: ^12.0.0
@@ -122,7 +122,7 @@ jobs:
122122
pool:
123123
vmImage: windows-2019
124124
strategy:
125-
maxParallel: 4
125+
maxParallel: 3
126126
matrix:
127127
node-12:
128128
node_version: ^12.0.0

lint-staged.config.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
module.exports = {
2-
ignore: ['package-lock.json', 'CHANGELOG.md'],
3-
linters: {
4-
'*.js': ['prettier --write', 'eslint --fix', 'git add'],
5-
'*.{json,md,yml,css}': ['prettier --write', 'git add'],
6-
},
2+
'*.js': ['prettier --write', 'eslint --fix', 'git add'],
3+
'*.{json,md,yml,css}': ['prettier --write', 'git add'],
74
};

0 commit comments

Comments
 (0)