1
0
Fork 0
management/front/dkha-web-sz-main/node_modules/schema-utils/README.md

277 lines
5.4 KiB
Markdown
Raw Permalink Normal View History

2023-12-18 13:12:25 +08:00
<div align="center">
<a href="http://json-schema.org">
<img width="160" height="160"
2024-01-16 21:26:16 +08:00
src="https://raw.githubusercontent.com/webpack-contrib/schema-utils/master/.github/assets/logo.png">
2023-12-18 13:12:25 +08:00
</a>
<a href="https://github.com/webpack/webpack">
<img width="200" height="200"
src="https://webpack.js.org/assets/icon-square-big.svg">
</a>
</div>
2024-01-16 21:26:16 +08:00
[![npm][npm]][npm-url]
[![node][node]][node-url]
[![deps][deps]][deps-url]
[![tests][tests]][tests-url]
[![coverage][cover]][cover-url]
[![chat][chat]][chat-url]
[![size][size]][size-url]
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
# schema-utils
Package for validate options in loaders and plugins.
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
## Getting Started
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
To begin, you'll need to install `schema-utils`:
```console
npm install schema-utils
```
## API
2023-12-18 13:12:25 +08:00
**schema.json**
2024-01-16 21:26:16 +08:00
```json
{
"type": "object",
"properties": {
"option": {
"type": "boolean"
}
},
"additionalProperties": false
}
```
```js
import schema from './path/to/schema.json';
import validate from 'schema-utils';
const options = { option: true };
const configuration = { name: 'Loader Name/Plugin Name/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.
2023-12-18 13:12:25 +08:00
```js
2024-01-16 21:26:16 +08:00
validate(
schema,
{
name: 123,
},
{ name: 'MyPlugin' }
);
```
### `configuration`
Allow to configure validator.
There is an alternative method to configure the `name` and`baseDataPath` options via the `title` property in the schema.
For example:
```json
2023-12-18 13:12:25 +08:00
{
2024-01-16 21:26:16 +08:00
"title": "My Loader options",
2023-12-18 13:12:25 +08:00
"type": "object",
"properties": {
2024-01-16 21:26:16 +08:00
"name": {
"description": "This is description of option.",
"type": "string"
}
2023-12-18 13:12:25 +08:00
},
"additionalProperties": false
}
```
2024-01-16 21:26:16 +08:00
The last word used for the `baseDataPath` option, other words used for the `name` option.
Based on the example above the `name` option equals `My Loader`, the `baseDataPath` option equals `options`.
#### `name`
Type: `Object`
Default: `"Object"`
Allow to setup name in validation errors.
2023-12-18 13:12:25 +08:00
```js
2024-01-16 21:26:16 +08:00
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"`
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
Allow to setup base data path in validation errors.
```js
validate(schema, options, { name: 'MyPlugin', baseDataPath: 'options' });
2023-12-18 13:12:25 +08:00
```
2024-01-16 21:26:16 +08:00
```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 initialized using an options object that does not match the API schema.
- options.optionName should be a integer.
Additional Information.
```
## Examples
2023-12-18 13:12:25 +08:00
**schema.json**
2024-01-16 21:26:16 +08:00
2023-12-18 13:12:25 +08:00
```json
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"test": {
"anyOf": [
{ "type": "array" },
{ "type": "string" },
{ "instanceof": "RegExp" }
]
},
"transform": {
"instanceof": "Function"
},
"sourceMap": {
"type": "boolean"
}
},
"additionalProperties": false
}
```
### `Loader`
```js
2024-01-16 21:26:16 +08:00
import { getOptions } from 'loader-utils';
import validateOptions from 'schema-utils';
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
import schema from 'path/to/schema.json';
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
function loader(src, map) {
const options = getOptions(this) || {};
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
validateOptions(schema, options, {
name: 'Loader Name',
baseDataPath: 'options',
});
2023-12-18 13:12:25 +08:00
// Code...
}
2024-01-16 21:26:16 +08:00
export default loader;
2023-12-18 13:12:25 +08:00
```
### `Plugin`
```js
2024-01-16 21:26:16 +08:00
import validateOptions from 'schema-utils';
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
import schema from 'path/to/schema.json';
2023-12-18 13:12:25 +08:00
class Plugin {
2024-01-16 21:26:16 +08:00
constructor(options) {
validateOptions(schema, options, {
name: 'Plugin Name',
baseDataPath: 'options',
});
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
this.options = options;
2023-12-18 13:12:25 +08:00
}
2024-01-16 21:26:16 +08:00
apply(compiler) {
2023-12-18 13:12:25 +08:00
// Code...
}
}
2024-01-16 21:26:16 +08:00
export default Plugin;
```
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
## Contributing
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
Please take a moment to read our contributing guidelines if you haven't yet done so.
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
[CONTRIBUTING](./.github/CONTRIBUTING.md)
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
## License
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
[MIT](./LICENSE)
2023-12-18 13:12:25 +08:00
2024-01-16 21:26:16 +08:00
[npm]: https://img.shields.io/npm/v/schema-utils.svg
[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/schema-utils.svg
[deps-url]: https://david-dm.org/webpack/schema-utils
[tests]: https://github.com/webpack/schema-utils/workflows/schema-utils/badge.svg
[tests-url]: https://github.com/webpack/schema-utils/actions
[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
2023-12-18 13:12:25 +08:00
[chat-url]: https://gitter.im/webpack/webpack
2024-01-16 21:26:16 +08:00
[size]: https://packagephobia.com/badge?p=schema-utils
[size-url]: https://packagephobia.com/result?p=schema-utils