@uzelux/validator
v0.0.2
Published
Simple schema based validator
Downloads
3
Readme
#Validator
A simple Schema based Validator
Uzelux's note
This project is WIP, derived from one of my projects' submodule
Please kindly report any issue/bugs discovered when use
Usage
const { Validator, ValidatorTypes, ValidatorError } = require('@uzelux/validator');
const validator = new Validator({
iAmString: {
type: ValidatorTypes.STRING,
length: 10,
}
});
const iAmString = 'sure i am';
try {
const result = validator.validate({iAmString});
console.log(result); // true
} catch (e) {
console.log(e.name); // ValidatorError;
consoel.log(e.message) // Error message
}
Options Available
Currently the schema only provides single layer structure. With the parameter name as the key and the criteria as the body.
const validator = new Validator({
parameterName: {
type: ValidatorTypes.STRING, // see ValidatorTypes
contains: 'must have', // value must contain the data specified
regex: /\w+/g, // match regular expression
length: 10, // length after toString()
within: ['possible1', 'possible2'], // value exist in array provided
required: false // all parameters are treated as required by default,
// i.e. must be provided in validate()
}
})
Types Supported
Currently the following types are provided under the class ValidatorTypes
Name | Type | Alias --- | --- | --- BOOLEAN | boolean | BOOL NULL | null | UNDEFINED | undefined | NUMBER | number | FLOAT, DOUBLE BIGINT | bigint | STRING | string | SYMBOL | symbol | OBJECT | object | FUNCTION | function | ARRAY | array | DATE | date | ERROR | error | INTEGER | integer | NAN | NaN |
Errors
Name | Type | Thrown By --- | --- | --- MissingParameter | ValidatorError | Assert Require InvalidType | ValidatorError | Assert Type DoesNotContain | ValidatorError | Assert Contain NotInRange | ValidatorError | Assert Within RegexMismatch | ValidatorError | Assert Regex LengthMismatch | ValidatorError | Assert Length