Skip to content

Commit

Permalink
added documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
K1NXZ committed Jan 10, 2023
1 parent 32dd380 commit 69888a3
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/zodError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,28 @@
import type { Request, Response, NextFunction } from 'express'
import { ZodError } from 'zod'

/**
* Zod error handler for express middleware
* @param handler Custom error handler function to override default error handler
* @returns Express middleware function to handle zod errors
*
* @example
* // Example usage with express
* import express from 'express'
* import { zodError } from 'express-zod'
*
* const app = express()
*
* app.use(zodError())
*
* // If you want to handle zod errors differently, you can provide a custom error handler
* // This custom error handler will override the default error handler
* app.use(
* zodError((err, req, res, next) => {
* // Handle zod errors here
* })
* )
*/
export const zodError = (
handler?: (
error: ZodError,
Expand All @@ -17,9 +39,13 @@ export const zodError = (
res: Response,
next: NextFunction
) => {
// Check if error is a zod error and handle it accordingly
if (err instanceof ZodError) {
// If custom error handler is provided, use it instead of default error handler
if (handler) return handler(err, req, res, next)

// Default error handler for zod errors (returns 400 status code)
// You can customize this to return a different status code or error message format if you want
return res.status(400).json({
error: {
message: err.flatten().fieldErrors,
Expand Down
49 changes: 49 additions & 0 deletions src/zodValidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@
import type { Request, Response, NextFunction, RequestHandler } from 'express'
import { z, AnyZodObject } from 'zod'

/**
* Validateable interface for express request object
*/
interface Validatable {
body?: Record<string, any>
params?: Record<string, any>
query?: Record<string, any>
}

/**
* Validation interface
* @template B Zod object for body
* @template P Zod object for params
* @template Q Zod object for query
*/
interface Validation<
B extends AnyZodObject,
P extends AnyZodObject,
Expand All @@ -19,6 +28,13 @@ interface Validation<
query?: Q
}

/**
* Validate input with zod object and return parsed object if validation is successful or throw error if validation fails
* @param input the input to validate
* @param validation the zod object to validate input with´
* @returns parsed object if validation is successful
* @throws error if validation fails
*/
const _validate = async <
B extends AnyZodObject,
P extends AnyZodObject,
Expand All @@ -40,6 +56,39 @@ const _validate = async <
}
}

/**
* Express middleware to validate request body, params, and query with zod object and return parsed object if validation is successful or throw error if validation fails
*
* @param validation Validation object containing zod object for body, params, and query
* @returns Express middleware function
*
* @example
* // Example usage with express
* import express from 'express'
* import { validate } from 'express-zod'
* import { z } from 'zod'
*
* const app = express()
*
* // Example validation object
* const validation = {
* body: z.object({
* name: z.string(),
* }),
* params: z.object({
* id: z.string(),
* }),
* query: z.object({
* limit: z.number(),
* }),
* }
*
* app.post('/users/:id', validate(validation), (req, res) => {
* // req.body.name is now a string
* // req.params.id is now a string
* // req.query.limit is now a number
* })
*/
export const validate = <
B extends AnyZodObject,
P extends AnyZodObject,
Expand Down

0 comments on commit 69888a3

Please sign in to comment.