[DEPRECATION NOTICE]
sails-hook-req-validate-promise
sails-hook-req-validate for the updated version! There is a brand new version that supports both PROMISE and Non-PROMISE versions with many new features.
Please useI will no longer support this package but I will continue to update the sails-hook-req-validate package.
Sails hook for overwrite req.validate request with Promise.
Non-Promise version: https://www.npmjs.com/package/sails-hook-req-validate
npm install sails-hook-req-validate-promise --save
req.validate();
Requirements:
Sails v1.x.x and lodash enabled as global (lodash is enabled by default).
Default Value (when a parameter is not set)
const params = await req; // if 'fruit' doesn't exists, it will be set as 'apple' console;
const params = await req; console;
Enumeration check
const params = await req; console;
const params = await req; console;
const params = await req; console;
Simple Single & Multple Parameter(s)
Validates req.params
for expecting parameter keys and returns req.badRequest
(400 status code) if any parameter key is missing.
try const params = await req; console; // {id: '1234'} // if the validation fails, "req.badRequest" will be called and returns Promise.reject catch err // wil catch Promise.reject here.
If you prefer non async-await promise method
req; ;
Disable req.badRequest
on error and enable Promise.reject
const params = await req; // <--- if you set the second value as FALSE, req.badRequest will NOT be call when error but it will just return Promise.reject
NOTE: To disable the default error response, set false
as the second passing variable.
const params = await req; console; // {id: '1234', firstname: "John", lastname: "Doe"} // if the validation fails, "req.badRequest" will be called and returns Promise.reject
Optional Parameter
Validates req.params
for expecting parameter keys and returns req.badRequest
(400 status code) if any parameter key is missing except optional parameters.
const params = await req; // lastname is an OPTIONAL field console; // {id: '1234', firstname: "John", lastname: "Doe"} // if the validation fails, "req.badRequest" will be called and returns Promise.reject
NOTE: For an optional parameter, just add ?
at the end of the passing parameter key.
Multple Parameters with TYPE filters
Validates req.params
for expecting parameter keys and returns req.badRequest
(400 status code) if any missing parameter key.
const params = await req; console; // {id: '1234', firstname: "John", lastname: "Doe"}
See Validation Filters for more information.
OR Operation
OR |
operarion is a new addition to 0.2.x version. It can be applied to either required or optional parameter.
const params = await rreq;```<br> ### Multple Parameters with TYPE filters & CONVERTION filtersValidates `reqparams` for expecting parameter keys and returns `reqbadRequest` (400 status code) if any missing parameter key. ```javascript const params = await rreq; console; // {id: '1234', firstname: "John", lastname: "Doe"}
NOTE: All CONVERTION filters start with to
, for example: toUppercase, toBoolean.
See Validation Filters and Conversion Filters for more information.
- Additional Example (Combining All Above Examples in One)
Validates req.params
for expecting parameter keys and returns req.badRequest
(400 status code) if any missing parameter key.
const params = await req; console; // {id: '1234', firstname: "John", lastname: "Doe"} // if the validation fails, "req.badRequest" will be called and will NOT returns Promise.reject
See Validation Filters and Conversion Filters for more information.
Disable Default Error Response
When the validation fails, res.badRequest
will not be sent instead 'false' will be returned.
try const params = await req; console; // {id: '1234', firstname: "John", lastname: "Doe"} // ..process catch err console; return res; // Make sure to handle badRequest response
NOTE: To disable the default error response, set false
as the second passing variable.
Validation Filters
any email url ip alpha numeric base64 hex hexColor lowercase uppercase string boolean int float date json array object ascii mongoId alphanumeric creditCard
Conversion Filters
toLowercase toUppercase toEmail // Normalize email string toBoolean toDate toInt toFloat