Gathering detailed insights and metrics for koa-req-validator
Gathering detailed insights and metrics for koa-req-validator
Gathering detailed insights and metrics for koa-req-validator
Gathering detailed insights and metrics for koa-req-validator
npm install koa-req-validator
Typescript
Module System
Node Version
NPM Version
68.3
Supply Chain
99.2
Quality
75.5
Maintenance
50
Vulnerability
100
License
JavaScript (100%)
Total
36,047
Last Day
26
Last Week
86
Last Month
483
Last Year
5,014
19 Stars
23 Commits
11 Forks
4 Watching
5 Branches
3 Contributors
Latest Version
1.0.0
Package Id
koa-req-validator@1.0.0
Unpacked Size
9.50 kB
Size
4.06 kB
File Count
4
NPM Version
5.7.1
Node Version
8.9.4
Publised On
09 Dec 2024
Cumulative downloads
Total Downloads
Last day
766.7%
26
Compared to previous day
Last week
-23.2%
86
Compared to previous week
Last month
2.8%
483
Compared to previous month
Last year
96.2%
5,014
Compared to previous year
A node-validator based request validation middleware for Koa. Unlike other libraries, this middleware provides a declarative API and enables access to the full power of node-validator. Here is a taste of it:
1router.post('/users', validate({ 2 email: ['require', 'isEmail', 'Invalid email address'], 3 password: ['require', 'isLength(6, 32)', 'Password must be between 6 and 32 characters'] 4 }), function *(next) { 5 ... 6 } 7) 8 9// To validate properties of an object 10router.post('/users', validate({ 11 'user.name': ['require', 'Name is required'], 12 'user.address.state': ['require', 'State is required'] 13 }), function *(next) { 14 ... 15 } 16)
Basic
1import validate from 'koa-req-validator' 2 3router.post(path, validate(opts), ...)
Options
opts
is an object specifying the fields and their validation rules.
Each key is a field name in the post data (e.g. 'name', 'user.name') with optional search scopes: header
(alias headers
), query
, body
and params
. Field name and scopes are separated by :
. If no scope is specified, all scopes are searched.
Value is a rule array with the last element being an error message. A rule can be any of the supported methods of node-validator or a custom validator function(value: *, ...args: Array<*>, ctx: KoaContext): Promise<boolean>|boolean
. value
is the value to be validated from one of the scopes. args
are additional arguments that can be declared for the validator (see the isLength
example above). ctx
is the Koa context.
If a field has no value, it won't be validated. To make a field required, add the special required
rule (or its alias isRequired
). If there are validation failures, the middleware invokes ctx.throw()
with status code 400
and all error messages.
Examples
1import validator from 'validator' 2 3// Add custom validator 4validator['validateUserName'] = async (username, group, ctx) => { 5 // 1st arg: username is the value to be validate 6 // 2nd...2nd to last args: group is the extra value passed to isNewUserName 7 // last arg: ctx Koa context 8 return boolean | Promise<boolean> 9} 10 11validate({ 12 // Find email from request.body and validate 13 'email:body': ['require', 'isEmail', 'Invalid email address'], 14 15 // Find password in all scopes, use the first non-empty value to validate 16 'password': ['require', 'Password is required'], 17 18 // Find birthday from request.query or request.body 19 'birthday:query:body': ['isDate', 'Invalid birthday'], 20 21 // Find username in all scopes 22 'username': ['validateUserName', 'Invalid username'], 23})
Route decorators
You can combine this middleware with route decorators, for example:
1import validate from 'koa-req-validator' 2import bodyParser from 'koa-bodyparser' 3 4@controller('/users', convert(bodyParser())) 5export default class extends Ctrl { 6 7 @post('', validate(opts)) 8 async register(ctx, next) { 9 ... 10 } 11}
1npm install 2npm test
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 6/23 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
50 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-12-02
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More