Skip to content

Commit

Permalink
refactor: code (webpack#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Jul 16, 2019
1 parent fc8c2de commit 0512f32
Show file tree
Hide file tree
Showing 12 changed files with 8,805 additions and 2,382 deletions.
135 changes: 120 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ npm install schema-utils

## API

### validateOptions

**schema.json**

```json
Expand All @@ -43,20 +41,117 @@ npm install schema-utils
"type": ["boolean"]
}
},
"errorMessage": {
"option": "should be {Boolean} (https:/github.com/org/repo#anchor)"
},
"additionalProperties": false
}
```

```js
import schema from './path/to/schema.json';
import validateOptions from 'schema-utils';
import validate from 'schema-utils';

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

validateOptions(schema, options, 'Loader/Plugin Name');
validate(schema, options, configuration);
```

### `schema`

Type: `String`

JSON schema.

Simple example of schema:

```json
{
"type": "object",
"properties": {
"name": {
"description": "This is description of option.",
"type": "string"
}
},
"additionalProperties": false
}
```

### `options`

Type: `Object`

Object with options.

```js
validate(
schema,
{
name: 123,
},
{ name: 'MyPlugin' }
);
```

### `configuration`

Allow to configure validator.

#### `name`

Type: `Object`
Default: `"Object"`

Allow to setup name in validation errors.

```js
validate(schema, options, { name: 'MyPlugin' });
```

```shell
Invalid configuration object. MyPlugin has been initialised using a configuration object that does not match the API schema.
- configuration.optionName should be a integer.
```

#### `baseDataPath`

Type: `String`
Default: `"configuration"`

Allow to setup base data path in validation errors.

```js
validate(schema, options, { name: 'MyPlugin', baseDataPath: 'options' });
```

```shell
Invalid options object. MyPlugin has been initialised using an options object that does not match the API schema.
- options.optionName should be a integer.
```

#### `postFormatter`

Type: `Function`
Default: `undefined`

Allow to reformat errors.

```js
validate(schema, options, {
name: 'MyPlugin',
postFormatter: (formattedError, error) => {
if (error.keyword === 'type') {
return `${formattedError}\nAdditional Information.`;
}

return formattedError;
},
});
```

```shell
Invalid options object. MyPlugin has been initialised using an options object that does not match the API schema.
- options.optionName should be a integer.
Additional Information.
```

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

validateOptions(schema, options, 'Loader Name');
validateOptions(schema, options, {
name: 'Loader Name',
baseDataPath: 'options',
});

// Code...
}

export default loader;
```

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

class Plugin {
constructor(options) {
validateOptions(schema, options, 'Plugin Name');
validateOptions(schema, options, {
name: 'Plugin Name',
baseDataPath: 'options',
});

this.options = options;
}
Expand All @@ -123,6 +226,8 @@ class Plugin {
// Code...
}
}

export default Plugin;
```

## Contributing
Expand All @@ -139,12 +244,12 @@ Please take a moment to read our contributing guidelines if you haven't yet done
[npm-url]: https://npmjs.com/package/schema-utils
[node]: https://img.shields.io/node/v/schema-utils.svg
[node-url]: https://nodejs.org
[deps]: https://david-dm.org/webpack-contrib/schema-utils.svg
[deps-url]: https://david-dm.org/webpack-contrib/schema-utils
[tests]: https://dev.azure.com/webpack-contrib/schema-utils/_apis/build/status/webpack-contrib.schema-utils?branchName=master
[tests-url]: https://dev.azure.com/webpack-contrib/schema-utils/_build/latest?definitionId=2&branchName=master
[cover]: https://codecov.io/gh/webpack-contrib/schema-utils/branch/master/graph/badge.svg
[cover-url]: https://codecov.io/gh/webpack-contrib/schema-utils
[deps]: https://david-dm.org/webpack/schema-utils.svg
[deps-url]: https://david-dm.org/webpack/schema-utils
[tests]: https://dev.azure.com/webpack/schema-utils/_apis/build/status/webpack.schema-utils?branchName=master
[tests-url]: https://dev.azure.com/webpack/schema-utils/_build/latest?definitionId=9&branchName=master
[cover]: https://codecov.io/gh/webpack/schema-utils/branch/master/graph/badge.svg
[cover-url]: https://codecov.io/gh/webpack/schema-utils
[chat]: https://badges.gitter.im/webpack/webpack.svg
[chat-url]: https://gitter.im/webpack/webpack
[size]: https://packagephobia.now.sh/badge?p=schema-utils
Expand Down
6 changes: 3 additions & 3 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
pool:
vmImage: ubuntu-16.04
strategy:
maxParallel: 4
maxParallel: 3
matrix:
node-12:
node_version: ^12.0.0
Expand Down Expand Up @@ -79,7 +79,7 @@ jobs:
pool:
vmImage: macOS-10.14
strategy:
maxParallel: 4
maxParallel: 3
matrix:
node-12:
node_version: ^12.0.0
Expand Down Expand Up @@ -122,7 +122,7 @@ jobs:
pool:
vmImage: windows-2019
strategy:
maxParallel: 4
maxParallel: 3
matrix:
node-12:
node_version: ^12.0.0
Expand Down
7 changes: 2 additions & 5 deletions lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
module.exports = {
ignore: ['package-lock.json', 'CHANGELOG.md'],
linters: {
'*.js': ['prettier --write', 'eslint --fix', 'git add'],
'*.{json,md,yml,css}': ['prettier --write', 'git add'],
},
'*.js': ['prettier --write', 'eslint --fix', 'git add'],
'*.{json,md,yml,css}': ['prettier --write', 'git add'],
};
Loading

0 comments on commit 0512f32

Please sign in to comment.