option-validator
A simple option validator
Install
$ npm install option-validator
import optionValidator from 'option-validator';
OR umd builds are also available
<script src="path/to/option-validator.js"></script>
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
- Support for custom validator functions
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