express-zod-safe 1.0.7 → 1.1.1

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,5 +1,5 @@
1
1
  import { RequestHandler } from 'express';
2
- import { z } from 'zod';
2
+ import { z, ZodTypeAny, ZodRawShape } from 'zod';
3
3
  /**
4
4
  * Generates a middleware function for Express.js that validates request params, query, and body.
5
5
  * This function uses Zod schemas to perform validation against the provided schema definitions.
@@ -38,12 +38,12 @@ import { z } from 'zod';
38
38
  *
39
39
  * app.listen(3000, () => console.log('Server running on port 3000'));
40
40
  */
41
- declare function validate<TParams extends Validation = {}, TQuery extends Validation = {}, TBody extends Validation = {}>(schemas: ValidationSchemas<TParams, TQuery, TBody>): RequestHandler<ZodOutput<TParams>, any, ZodOutput<TBody>, ZodOutput<TQuery>>;
41
+ 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>>;
42
42
  /**
43
43
  * Represents a generic type for route validation, which can be applied to params, query, or body.
44
44
  * Each key-value pair represents a field and its corresponding Zod validation schema.
45
45
  */
46
- type Validation = Record<string, z.ZodTypeAny>;
46
+ type Validation = ZodTypeAny | ZodRawShape;
47
47
  /**
48
48
  * Defines the structure for the schemas provided to the validate middleware.
49
49
  * Each property corresponds to a different part of the request (params, query, body)
@@ -53,7 +53,7 @@ type Validation = Record<string, z.ZodTypeAny>;
53
53
  * @template TQuery - Type definition for query schema.
54
54
  * @template TBody - Type definition for body schema.
55
55
  */
56
- interface ValidationSchemas<TParams extends Validation, TQuery extends Validation, TBody extends Validation> {
56
+ interface ExtendedValidationSchemas<TParams, TQuery, TBody> {
57
57
  params?: TParams;
58
58
  query?: TQuery;
59
59
  body?: TBody;
@@ -65,5 +65,5 @@ interface ValidationSchemas<TParams extends Validation, TQuery extends Validatio
65
65
  *
66
66
  * @template T - The validation type (params, query, or body).
67
67
  */
68
- type ZodOutput<T extends Validation> = z.ZodObject<T>['_output'];
68
+ type ZodOutput<T extends Validation> = T extends ZodRawShape ? z.ZodObject<T>['_output'] : T['_output'];
69
69
  export = validate;
package/dist/index.js CHANGED
@@ -1,6 +1,14 @@
1
1
  "use strict";
2
2
  const zod_1 = require("zod");
3
3
  const types = ['query', 'params', 'body'];
4
+ /**
5
+ * A ZodSchema type guard.
6
+ * @param schema The Zod schema to check.
7
+ * @returns Whether the provided schema is a ZodSchema.
8
+ */
9
+ function isZodSchema(schema) {
10
+ return schema && typeof schema.safeParse === 'function';
11
+ }
4
12
  /**
5
13
  * Generates a middleware function for Express.js that validates request params, query, and body.
6
14
  * This function uses Zod schemas to perform validation against the provided schema definitions.
@@ -43,16 +51,15 @@ function validate(schemas) {
43
51
  var _a, _b, _c;
44
52
  // Create validation objects for each type
45
53
  const validation = {
46
- params: zod_1.z.object((_a = schemas.params) !== null && _a !== void 0 ? _a : {}).strict(),
47
- query: zod_1.z.object((_b = schemas.query) !== null && _b !== void 0 ? _b : {}).strict(),
48
- body: zod_1.z.object((_c = schemas.body) !== null && _c !== void 0 ? _c : {}).strict()
54
+ params: isZodSchema(schemas.params) ? schemas.params : zod_1.z.object((_a = schemas.params) !== null && _a !== void 0 ? _a : {}).strict(),
55
+ query: isZodSchema(schemas.query) ? schemas.query : zod_1.z.object((_b = schemas.query) !== null && _b !== void 0 ? _b : {}).strict(),
56
+ body: isZodSchema(schemas.body) ? schemas.body : zod_1.z.object((_c = schemas.body) !== null && _c !== void 0 ? _c : {}).strict()
49
57
  };
50
58
  return (req, res, next) => {
51
59
  const errors = [];
52
60
  // Validate all types (params, query, body)
53
61
  for (const type of types) {
54
62
  const parsed = validation[type].safeParse(req[type]);
55
- // @ts-expect-error This is fine
56
63
  if (parsed.success)
57
64
  req[type] = parsed.data;
58
65
  else
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-zod-safe",
3
- "version": "1.0.7",
3
+ "version": "1.1.1",
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": {