https://github.com/zhw2590582/option-validator
A simple option validator
https://github.com/zhw2590582/option-validator
option scheme validator
Last synced: 2 months ago
JSON representation
A simple option validator
- Host: GitHub
- URL: https://github.com/zhw2590582/option-validator
- Owner: zhw2590582
- License: mit
- Created: 2018-10-28T02:51:28.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2020-10-07T14:31:15.000Z (almost 5 years ago)
- Last Synced: 2024-08-10T23:16:00.755Z (about 1 year ago)
- Topics: option, scheme, validator
- Language: JavaScript
- Homepage:
- Size: 45.9 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# option-validator
[](https://travis-ci.com/zhw2590582/option-validator)



[](https://www.npmjs.com/package/option-validator)
[](https://david-dm.org/zhw2590582/option-validator)> A simple option validator
## Install
```
$ npm install option-validator
``````js
import optionValidator from 'option-validator';
```OR umd builds are also available
```html
```
Will expose the global variable to `window.optionValidator`.
## Usage
- Only one api is to receive a option object and a scheme.
- If the verification is passed, the original option object will be returned.
- If the verification fails, an exception will be thrown.
- Support all js type detection [JS Types](./__test__/testData.js)
- Support for custom validator functions```js
const option = {
a: 1,
b: '2',
c: {
d: () => null,
},
g: {
h: new Error('error'),
},
i: [1, '2', () => 3],
j: [1, 2, 3, 4, 5, 6],
k: '123456',
};const scheme = {
// Shallow verification
a: 'number',
b: 'string',// Deep verification
c: {
d: 'function',
},// Validator functions
g: {
h: (value, type, path) => {
// value --> new Error('error')
// type --> 'error'
// path --> ['option', 'g', 'h']// Returns string mean validation failed, and the string will thrown
return 'I will throw an exception';// Returns error also mean validation failed, and the error will thrown
return new Error('I will throw an exception');// Returns true mean verification passed
return type === 'error';
},
},// Verify array elements
i: ['number', 'string', 'function'],// If there is no corresponding validator, the first one is taken by default.
j: ['number'],// Verify one of them
k: 'number|string',
};optionValidator(option, scheme);
```## License
MIT © [Harvey Zack](https://sleepy.im/)