express-zod-safe 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
- import { NextFunction, Request, RequestHandler, Response } from 'express';
2
- import { ZodError, z, ZodTypeAny, ZodRawShape } from 'zod';
1
+ import type { NextFunction, Request, RequestHandler, Response } from 'express';
2
+ import { type ZodError, type ZodRawShape, type ZodTypeAny, z } from 'zod';
3
3
  declare const types: readonly ["query", "params", "body"];
4
+ declare const emptyObjectSchema: z.ZodObject<{}, "strict", ZodTypeAny, {}, {}>;
5
+ type Empty = typeof emptyObjectSchema;
4
6
  /**
5
7
  * Generates a middleware function for Express.js that validates request params, query, and body.
6
8
  * This function uses Zod schemas to perform validation against the provided schema definitions.
@@ -39,7 +41,7 @@ declare const types: readonly ["query", "params", "body"];
39
41
  *
40
42
  * app.listen(3000, () => console.log('Server running on port 3000'));
41
43
  */
42
- declare function validate<TParams extends Validation = {}, TQuery extends Validation = {}, TBody extends Validation = {}>(schemas: ExtendedValidationSchemas<TParams, TQuery, TBody>): RequestHandler<ZodOutput<TParams>, any, ZodOutput<TBody>, ZodOutput<TQuery>>;
44
+ declare function validate<TParams extends Validation = Empty, TQuery extends Validation = Empty, TBody extends Validation = Empty>(schemas: ExtendedValidationSchemas<TParams, TQuery, TBody>): RequestHandler<ZodOutput<TParams>, any, ZodOutput<TBody>, ZodOutput<TQuery>>;
43
45
  /**
44
46
  * Describes the types of data that can be validated: 'query', 'params', or 'body'.
45
47
  */
@@ -50,7 +52,7 @@ type DataType = (typeof types)[number];
50
52
  */
51
53
  interface ErrorListItem {
52
54
  type: DataType;
53
- errors: ZodError<any>;
55
+ errors: ZodError;
54
56
  }
55
57
  /**
56
58
  * Represents a QueryString object parsed by the qs library.
@@ -61,7 +63,7 @@ interface ParsedQs {
61
63
  /**
62
64
  * Represents an Express.js error request handler.
63
65
  */
64
- type ErrorRequestHandler<P = Record<string, string>, ResBody = any, ReqBody = any, ReqQuery = ParsedQs, LocalsObj extends Record<string, any> = Record<string, any>> = (err: ErrorListItem[], req: Request<P, ResBody, ReqBody, ReqQuery, LocalsObj>, res: Response<ResBody, LocalsObj>, next: NextFunction) => void;
66
+ type ErrorRequestHandler<P = Record<string, string>, ResBody = any, ReqBody = any, ReqQuery = ParsedQs, LocalsObj extends Record<string, any> = Record<string, any>> = (err: ErrorListItem[], req: Request<P, ResBody, ReqBody, ReqQuery, LocalsObj>, res: Response<ResBody, LocalsObj>, next: NextFunction) => void | Promise<void>;
65
67
  /**
66
68
  * Represents a generic type for route validation, which can be applied to params, query, or body.
67
69
  * Each key-value pair represents a field and its corresponding Zod validation schema.
package/dist/index.js CHANGED
@@ -1,13 +1,14 @@
1
1
  "use strict";
2
2
  const zod_1 = require("zod");
3
3
  const types = ['query', 'params', 'body'];
4
+ const emptyObjectSchema = zod_1.z.object({}).strict();
4
5
  /**
5
6
  * A ZodSchema type guard.
6
7
  * @param schema The Zod schema to check.
7
8
  * @returns Whether the provided schema is a ZodSchema.
8
9
  */
9
10
  function isZodSchema(schema) {
10
- return schema && typeof schema.safeParse === 'function';
11
+ return !!schema && typeof schema.safeParse === 'function';
11
12
  }
12
13
  /**
13
14
  * Generates a middleware function for Express.js that validates request params, query, and body.
@@ -71,7 +72,8 @@ function validate(schemas) {
71
72
  // If a custom error handler is provided, use it
72
73
  if (schemas.handler)
73
74
  return schemas.handler(errors, req, res, next);
74
- return res.status(400).send(errors.map(error => ({ type: error.type, errors: error.errors })));
75
+ res.status(400).send(errors.map(error => ({ type: error.type, errors: error.errors })));
76
+ return;
75
77
  }
76
78
  return next();
77
79
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-zod-safe",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "TypeScript-friendly middleware designed for Express applications, leveraging the robustness of Zod schemas to validate incoming request bodies, parameters, and queries.",
5
5
  "main": "dist/index.js",
6
6
  "repository": {
@@ -22,23 +22,21 @@
22
22
  },
23
23
  "homepage": "https://github.com/AngaBlue/express-zod-safe#readme",
24
24
  "devDependencies": {
25
- "@angablue/eslint-config": "^1.4.5",
26
- "@types/node": "^20.14.6",
27
- "eslint": "^8.57.0",
28
- "express": "^4.19.2",
29
- "prettier": "^3.3.2",
30
- "rimraf": "^5.0.7",
31
- "typescript": "^5.4.5",
32
- "zod": "^3.23.8"
25
+ "@angablue/biome-config": "^1.0.1",
26
+ "@biomejs/biome": "^1.9.4",
27
+ "@types/node": "^22.10.2",
28
+ "rimraf": "^6.0.1",
29
+ "typescript": "^5.7.2",
30
+ "zod": "^3.24.1"
33
31
  },
34
32
  "peerDependencies": {
35
- "@types/express": "^4.0.0",
36
- "express": "^4.0.0",
33
+ "@types/express": "^4.0.0 || ^5.0.0",
34
+ "express": "^4.0.0 || ^5.0.0",
37
35
  "zod": "^3.0.0"
38
36
  },
39
37
  "scripts": {
40
38
  "clean": "rimraf dist",
41
39
  "build": "pnpm run clean && tsc",
42
- "lint": "eslint . --ext js,jsx,ts,tsx --fix"
40
+ "lint": "biome check --fix --error-on-warnings"
43
41
  }
44
42
  }