Gathering detailed insights and metrics for zod-express-guard
Gathering detailed insights and metrics for zod-express-guard
Gathering detailed insights and metrics for zod-express-guard
Gathering detailed insights and metrics for zod-express-guard
Small package intended to use zod to make express request type-safe.
npm install zod-express-guard
Typescript
Module System
Min. Node Version
Node Version
NPM Version
TypeScript (95.89%)
JavaScript (4.11%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
GPL-3.0 License
24 Stars
35 Commits
1 Forks
1 Watchers
1 Branches
2 Contributors
Updated on Aug 23, 2024
Latest Version
1.0.7
Package Id
zod-express-guard@1.0.7
Unpacked Size
45.76 kB
Size
15.61 kB
File Count
7
NPM Version
9.6.3
Node Version
19.9.0
Published on
Oct 09, 2023
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
3
22
Small package intended to use zod to make express request type-safe.
Just install with your favorite package manager:
npm i zod-express-guard
Zod, Express and @types/express are peer dependencies, so you can change versions without having to wait for me to update this library. Newer versions of npm resolve these by default, but you can do it manually like this:
npm i -D @types/express && npm i express zod
This library exposes four validation functions, and three of them are shortcuts that end up calling the first one.
All functions support async middlewares and will catch async errors.
This is the main function. It receives an object with the Zod schemas for what you want to validate,
together with the middleware you want to guard. If validation passes, your middleware will be called
with type-safe req
properties. If validation fails, next
will be called containg and instance of
ZodError
Example:
1// routes/login.ts 2import z from 'zod' 3import { validate } from 'zod-express-guard' 4 5export default validate( 6 { 7 body: z.object({ 8 login: z.string().nonempty(), 9 password: z.string().nonempty() 10 }) 11 }, 12 (req, res) => { 13 // req.body has the type { login: string, password: string } 14 // req.query and req.params have type unknown, since they were not validated 15 res.status(200).json({ message: 'Validation passed' }) 16 } 17)
You can pass up to three properties to the first parameter: body
, query
and params
, each one
validates its respective property inside req
validateBody
, validateQuery
and validateParams
These three functions serve as shortcuts when you only want to validate a single property inside
req
. This is useful for our login middleware in the previous example:
1// routes/login.ts 2 3import z from 'zod' 4import { validateBody } from 'zod-express-guard' 5 6export default validateBody( 7 z.object({ 8 login: z.string().nonempty(), 9 password: z.string().nonempty() 10 }), 11 (req, res) => { 12 // req.body has the type { login: string, password: string } 13 // req.query and req.params have type unknown, since they were not validated 14 res.status(200).json({ message: 'Validation passed' }) 15 } 16)
validateQuery
and validateParams
work similarly, but for req.query
and req.params
,
respectively.
Note that, when using a shortcut function, only one of the three properties will be validated and
made acessible, and the other two will have type unknown
Clone this and run npm install
on the root.
Please make sure you modify the tests to reflect any changes you add.
Use npx gitmoji -c
to commit, and follow existing commit patterns
Simply run npm test
after running npm install
.
The package.json
already specifies a pretest
script that will run a clean build before testing.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
Found 1/21 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
24 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
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