
Research
Malicious npm Packages Impersonate Flashbots SDKs, Targeting Ethereum Wallet Credentials
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
koa-validator
Advanced tools
a koa port of express-validator
npm install koa-validator
var koa = require('koa')
, validator = require('koa-validator')
, bodyParser = require('koa-bodyparser')
, app = koa()
;
app
.use(bodyParser())
.use(validator({
onValidationError: function(errMsg){
console.log('Validation error:', errMsg);
}
}))
.use(functon *(next){
this.checkParams('testparam', 'Invalid number').isInt();
yield next;
})
.listen(3000)
;
onValidationError - function(errMsg)
, default to null. The errMsg
is the errMsg you defined when you check one variable
You can define the function like this
function(errMsg){
throw new Error('Validation error: ' + errMsg);
}
validationErrorFormatter - function(paramName, errMsg, value)
, the default function is below
function(paramName, errMsg, value){
return {
param: paramName
, msg: errMsg
, value: value
};
}
If you will use checkBody
or assertBody
or sanitizeBody
, you should use one bodyparse middleware before validator.
npm test
true
if have any validationErrorvalidationErrorFormatter
You can use all check methods in validator.js checks
.
In addition, you can use below methods extended to validator.js.
notEmpty
empty
eq
, use ==
eqeq
, use ===
neq
, use !=
neqeq
, use !==
gt
lt
ge
le
notContains
isTime
len
, an alias to validator.js' isLength
in
, an alias to validator.js' isIn
byteLength
, a function support set charset.isUrl
, an alias to validator.js' isURL
isIp
, an alias to validator.js' isIP
isLowerCase
, an alias to validator.js' isLowercase
isUpperCase
, an alias to validator.js' isUppercase
You can use all sanitize methods in validator.js
.
In addition, you can use below methods extended to validator.js.
default
, set default valuetoLowerCase
toUpperCase
encodeURI
encodeURIComponent
decodeURI
decodeURIComponent
replace
toLow
, an alias to toLowerCase
toUp
, an alias to toUpperCase
If you want to extend validator.js, you can use extendCheck
and extendSanitize
like below examples.
When you extend validator.js, your check function name best match is*
, and your sanitize function name best match to*
.
This is not force, but recommended.
If you extend one function that validator.js have already had, the function will be ignored, and an error will be thrown.
var koa = require('koa')
, bodyParser = require('koa-bodyparser')
, validator = require('koa-validator')
;
validator.extendCheck('isFinite', function(str){
return isFinite(str);
});
validator.extendCheck({
isFinite: function(str){
return isFinite(str);
}
, isFinite2: function(str){
return isFinite(str);
}
});
validator.extendSanitize('toZero', function(str){
return 0;
});
validator.extendSanitize({
toOne: function(str){
return 1;
}
, toTwo: function(str){
return 2;
}
});
koa()
.use(bodyParser())
.use(validator())
.use(function *(next){
this.checkParams('test', 'Invalid test value').isFinite();
yield next;
})
.listen(3000)
;
Thanks to koa-validate
, some extended methods and test suites are based on koa-validate.
MIT
FAQs
a koa port of express-validator
The npm package koa-validator receives a total of 22 weekly downloads. As such, koa-validator popularity was classified as not popular.
We found that koa-validator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
Security News
Ruby maintainers from Bundler and rbenv teams are building rv to bring Python uv's speed and unified tooling approach to Ruby development.
Security News
Following last week’s supply chain attack, Nx published findings on the GitHub Actions exploit and moved npm publishing to Trusted Publishers.