koa-middle-validator
Koa middleware for the validator module. Support v1 and v2.
Installation
npm install koa-middle-validator
Usage
const util = const Koa = ;const bodyParser = ;const convert = ;const koaValidator = ;const Router = ;const _ = ; const app = ;const router = ; app;app; // this line must be immediately after any of the bodyParser middlewares! router;router;router;appapp { ctx; //ctx.checkParams('urlparam', 'Invalid urlparam').isAlpha(); ctx; ctx; //ctx.sanitizeParams('urlparam').toBoolean(); ctx; ctx; return ctx;} app;
Middleware Options
errorFormatter
function(param,msg,value)
customValidators
{ "validatorName": function(value, [additional arguments]), ... }
customSanitizers
{ "sanitizerName": function(value, [additional arguments]), ... }
Validation
ctx.check();
ctx; ctx; // find nested params ctx; // find nested params
ctx.assert();
Alias for ctx.check().
ctx.validate();
Alias for ctx.check().
ctx.checkBody();
Same as ctx.check(), but only looks in ctx.body
.
ctx.checkQuery();
Same as ctx.check(), but only looks in ctx.query
.
ctx.checkParams();
Same as ctx.check(), but only looks in ctx.params
.
ctx.checkHeaders();
Only checks ctx.headers
. This method is not covered by the general ctx.check()
.
ctx.checkCookies();
Only checks ctx.cookies
. This method is not covered by the general ctx.check()
.
Validation by Schema
ctx;
You can also define a specific location to validate against in the schema by adding in
parameter as shown below:
ctx;
ctx.check(schema); // will check 'password' no matter where it is but 'email' in query params
ctx.checkQuery(schema); // will check 'password' and 'email' in query params
ctx.checkBody(schema); // will check 'password' in body but 'email' in query params
ctx.checkParams(schema);
ctx.checkHeaders(schema); // will check 'password' in headers but 'email' in query params
Validation result
getValidationResult
Runs all validations and returns a validation result object for the errors gathered, for both sync and async validators.
ctx;ctx;ctx; ctx;
getValidationLegalResult (v1.1.0)
Runs all validations and return the validated values;
try ctx const values = await ctx mongoosemodel catch e // $$emit error
Optional input
ctx;//if there is no error, ctx.request.body.email is either undefined or a valid mail.
Sanitizer
ctx.sanitize();
ctxrequestbodycomment = 'a <span>comment</span>';ctxrequestbodyusername = ' a user '; ctx; // returns 'a <span>comment</span>'ctx; // returns 'a user' console; // 'a <span>comment</span>'console; // 'a user'
ctx.filter();
Alias for ctx.sanitize().
ctx.sanitizeBody();
Same as ctx.sanitize(), but only looks in ctx.request.body
.
ctx.sanitizeQuery();
Same as ctx.sanitize(), but only looks in ctx.request.query
.
ctx.sanitizeParams();
Same as ctx.sanitize(), but only looks in ctx.params
.
ctx.sanitizeHeaders();
Only sanitizes ctx.headers
. This method is not covered by the general ctx.sanitize()
.
ctx.sanitizeCookies();
Only sanitizes ctx.cookies
. This method is not covered by the general ctx.sanitize()
.
Sanitizer result
getSanitizerLegalResult (v1.1.0)
Runs all sanitizer and return the sanitized values;
try ctx const values = await ctx mongoosemodel catch e // $$emit error