Installations
npm install koa-req-validator
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
8.9.4
NPM Version
5.7.1
Score
68.5
Supply Chain
99.2
Quality
75.5
Maintenance
50
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
buunguyen
Download Statistics
Total Downloads
36,244
Last Day
10
Last Week
51
Last Month
392
Last Year
4,994
GitHub Statistics
19 Stars
23 Commits
11 Forks
4 Watching
5 Branches
3 Contributors
Package Meta Information
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
Total Downloads
Cumulative downloads
Total Downloads
36,244
Last day
233.3%
10
Compared to previous day
Last week
-57.5%
51
Compared to previous week
Last month
-22.2%
392
Compared to previous month
Last year
83.9%
4,994
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
koa-req-validator
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)
Usage
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
(aliasheaders
),query
,body
andparams
. 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 theisLength
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}
Test
1npm install 2npm test
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
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
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 6 are checked with a SAST tool
Reason
50 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-832h-xg76-4gv6
- Warn: Project is vulnerable to: GHSA-cwfw-4gq5-mrqx
- Warn: Project is vulnerable to: GHSA-g95f-p29q-9xw4
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-9vvw-cc9w-f27h
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-hr2v-3952-633q
- Warn: Project is vulnerable to: GHSA-h6ch-v84p-w6p9
- Warn: Project is vulnerable to: GHSA-qrmc-fj45-qfc2
- Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3
- Warn: Project is vulnerable to: MAL-2023-462
- Warn: Project is vulnerable to: GHSA-xf7w-r453-m56c
- Warn: Project is vulnerable to: GHSA-qh2h-chj9-jffq
- Warn: Project is vulnerable to: GHSA-44pw-h2cw-w3vq
- Warn: Project is vulnerable to: GHSA-jp4x-w63m-7wgm
- Warn: Project is vulnerable to: GHSA-c429-5p7v-vgjp
- Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37
- Warn: Project is vulnerable to: GHSA-4hpf-3wq7-5rpr
- Warn: Project is vulnerable to: GHSA-f522-ffg8-j8r6
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-282f-qqgm-c34q
- Warn: Project is vulnerable to: GHSA-fvqr-27wr-82fm
- Warn: Project is vulnerable to: GHSA-4xc9-xhrj-v574
- Warn: Project is vulnerable to: GHSA-x5rq-j2xg-h7qm
- Warn: Project is vulnerable to: GHSA-jf85-cpcp-j695
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-hxm2-r34f-qmc5
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m / GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-w9mr-4mfr-499f
- Warn: Project is vulnerable to: GHSA-gqgv-6jq5-jjj9
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-6g33-f262-xjp4
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-2m39-62fm-q8r3
- Warn: Project is vulnerable to: GHSA-mf6x-7mm4-x2g7
- Warn: Project is vulnerable to: GHSA-j44m-qm6p-hp7m
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-g7q5-pjjr-gqvp
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-xc7v-wxcw-j472
- Warn: Project is vulnerable to: GHSA-qgmg-gppg-76g5
Score
2
/10
Last Scanned on 2024-12-16
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