input-vld
v0.5.1
Published
Package for inputs validation
Downloads
2
Readme
First version of my validator for checking inputs.
Now support form and separately: email, password and string length validation.
Changelog
v. 0.1.1 - Change file structure.
v. 0.2.0 - Add form validation.
v. 0.3.0 - Add dynamic creating validation rules
v. 0.3.1 - Add min-max validation for number in input
v. 0.4.0 - config generator
v. 0.5.0 - upload to NPM
v. 0.5.1 - change import
In progress:
v. 0.6 - refactoring with TS
- Change code to more declarative style
- Adding new features
How to use.
Create folders 'helpers/validator' in your project directory.
Copy code (./src and index.js) from this repository to your folder 'validator'.
Import stringValidator or formValidator from ('/helpers/validator').
Create your configuration like example below, then use Generator for creating config. Example:
// config.js
const Generator = require('...../src/Generator.js');
const myRules = {
password: {
maxLength: { value: 7, msg: 'Max 7' },
minLength: { value: 1 },
upperCase: { value: 3, msg: 'Min 3' }
},
email: {
email: { value: 1 },
localLength: { value: 64, msg: '64 no more' },
minLength: { value: 1 },
maxLength: { value: 256, msg: 'Max 256' }
},
price: {
min: { value: 7 },
max: { value: 999 }
}
};
module.exports = Generator(myRules);
Currently you can use this params for validation:
upperCase:int - Minimal amount of upper-case letters in string
lowCase: int - Minimal amount of low-case letters in string
numbers: int, - Minimal amount of numbers in string
min: int, Minimal value of input's number
max: int, Max value of input's number
minLength: int, Minimal length of string
maxLength: int, Max length of string
email: int, Is str should be email (1 it true, either 0 )
localLength: int, Max length of email local part
And than use function stringValidator
or formValidator
.
stringValidator
receive arguments:
str == value for validation :: string,
config == object with validation rules:: object,
type == rule which will be used for validation:: string
and return array.
Array can be empty if it has not errors during validation, or contain objects:{status: boolean, msg: string, params: string}
Status indicate error, message take error message from your config, and params equal to rule value which didn't pass validation.
FormValidator receive arguments
(
form == form :: object,
config == object with validation rules:: object
)
Form must have this structure:{ruleName1: 'str', ruleName2: 'str', ...}
ruleName is a name of form filed which MUST be named same as a validation rule for it.
formValidator() return an array with this structure:
[
{ data: [[Object]], key: 'ruleName1' },
{ data: [[Object]], key: 'ruleName2' }
];
where [Object]
is the same as result for stringValidator()