• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

apowers313 / aiforge / 20962792399

13 Jan 2026 03:39PM UTC coverage: 84.707%. First build
20962792399

push

github

apowers313
feat: initial commit

787 of 905 branches covered (86.96%)

Branch coverage included in aggregate %.

4248 of 5039 new or added lines in 70 files covered. (84.3%)

4248 of 5039 relevant lines covered (84.3%)

13.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

67.8
/src/server/api/middleware/validation.ts
1
/**
2
 * Request validation middleware
3
 */
4
import type { Request, Response, NextFunction, RequestHandler } from 'express';
5
import { z } from 'zod';
6
import { ApiError } from './error.js';
1✔
7

8
/**
9
 * Validate request body against a Zod schema
10
 */
11
export function validateBody<T>(schema: z.ZodSchema<T>): RequestHandler {
1✔
12
  return (req: Request, _res: Response, next: NextFunction): void => {
36✔
13
    const result = schema.safeParse(req.body);
55✔
14

15
    if (!result.success) {
55✔
16
      const errors = result.error.errors.map((e) => ({
3✔
17
        path: e.path.join('.'),
3✔
18
        message: e.message,
3✔
19
      }));
3✔
20

21
      const firstError = errors[0];
3✔
22
      next(new ApiError(400, `Validation error: ${firstError?.message ?? 'Invalid input'}`, 'VALIDATION_ERROR'));
3!
23
      return;
3✔
24
    }
3✔
25

26
    req.body = result.data;
52✔
27
    next();
52✔
28
  };
55✔
29
}
36✔
30

31
/**
32
 * Validate request query parameters against a Zod schema
33
 * Note: Does not modify req.query as it may be read-only in some Express configurations.
34
 * Route handlers should cast req.query to the expected type after validation.
35
 */
36
export function validateQuery<T>(schema: z.ZodSchema<T>): RequestHandler {
1✔
37
  return (req: Request, _res: Response, next: NextFunction): void => {
12✔
38
    const result = schema.safeParse(req.query);
7✔
39

40
    if (!result.success) {
7!
NEW
41
      const errors = result.error.errors.map((e) => ({
×
NEW
42
        path: e.path.join('.'),
×
NEW
43
        message: e.message,
×
NEW
44
      }));
×
45

NEW
46
      const firstError = errors[0];
×
NEW
47
      next(new ApiError(400, `Validation error: ${firstError?.message ?? 'Invalid input'}`, 'VALIDATION_ERROR'));
×
NEW
48
      return;
×
NEW
49
    }
×
50

51
    // Don't reassign req.query as it may be read-only
52
    // Route handlers cast req.query to the schema type
53
    next();
7✔
54
  };
7✔
55
}
12✔
56

57
/**
58
 * Validate request params against a Zod schema
59
 * Note: Does not modify req.params as it may be read-only in some Express configurations.
60
 * Route handlers should cast req.params to the expected type after validation.
61
 */
62
export function validateParams<T>(schema: z.ZodSchema<T>): RequestHandler {
1✔
63
  return (req: Request, _res: Response, next: NextFunction): void => {
66✔
64
    const result = schema.safeParse(req.params);
17✔
65

66
    if (!result.success) {
17!
NEW
67
      const errors = result.error.errors.map((e) => ({
×
NEW
68
        path: e.path.join('.'),
×
NEW
69
        message: e.message,
×
NEW
70
      }));
×
71

NEW
72
      const firstError = errors[0];
×
NEW
73
      next(new ApiError(400, `Validation error: ${firstError?.message ?? 'Invalid input'}`, 'VALIDATION_ERROR'));
×
NEW
74
      return;
×
NEW
75
    }
×
76

77
    // Don't reassign req.params as it may be read-only
78
    // Route handlers cast req.params to the schema type
79
    next();
17✔
80
  };
17✔
81
}
66✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc