1.1.3 • Published 1 year ago

request-data-validators v1.1.3

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

Dependencies

Express.js

Submited Data validation

You can use this validator by requiring the "request-validator" like below:

let RequestDataValidator = require('./request-data-validators');
let validator = new RequestDataValidator(req,res);

let data = {
    "value1": validator.body(request,true,'Value 1').type('int').val(),
    "value2": validator.body(request,true,'Value 2').type('int').allow([0,1]).val()
}

you have to call isValidate method to check that all data is validate

let is_validated = validator.isValidate()

if you provide true as parameter then it redirect to back by defalut with old data erros in flash. YOu can retrive old data and error like below

let errors = Req.flash('errors')[0],
let old = Req.flash('old')[0]

if you want to get the errors in json format

let errors = validator.errors.details;

Request validator methods:

MethodsParametersDecription
get/paramskey(string)retrive the value url params
body/postkey(string), isRequired(bool), name(string), custom_message(string)retrive value of url params
typetype(string)validate the value type such as int, float,bool,number etc
lengthlength(int), custom_message(string)validate the value is fixed length
minLengthlength(int), custom_message(string)validate minimum length of the value
maxLengthlength(int), custom_message(string)validate maximum length of the value
minNumbervalue(int), custom_message(string)validate minimum number of the value
maxNumbervalue(int),custom_message(string)validate maximum number of the value
disallowvalues(array), custom_message(string)validate if disallow value is provided
allowvalues(array), custom_message(string)validate the value is from allowed list
customcallback function, custom_message(string)return true of false from the callback methods
sameAskey(string), custom_message(string)check the value is same as others value
formattype(string="array_obj"), format(array of object),custom_message(string)validate the provide array is correct format
valN/Afinally return the value
validateauto_response(bool)validate all required validation and return the result

Example of format array :

You have to pass this type of format array in calling the format method

[{ "update_quantity": 'int', "inventory_item_id": "int" }]

List of the value of type in type method:

typeDecription
intAccept only integer type value
stringAccept only string type value
emailAccept only email type value
dateAccept only date type value
numberAccept only number type value
booleanAccept only boolean type value
array_intAccept only array int type value
mobile_bdAccept only valid mobile number of bangladesh