@erwininteractive/mvc 0.6.3 → 0.6.5

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.
@@ -0,0 +1,34 @@
1
+ import type { ZodError, ZodSchema } from "zod";
2
+ export type ValidationStrategyType = "redirect" | "json";
3
+ /**
4
+ * Zod validation middleware
5
+ * @param schema - Zod schema to validate against
6
+ * @param strategy - 'redirect' (web) or 'json' (API)
7
+ */
8
+ export declare function validate<T>(schema: ZodSchema<T>, strategy?: ValidationStrategyType): (req: any, res: any, next: any) => Promise<any>;
9
+ /**
10
+ * Extract field errors from Zod ZodError
11
+ */
12
+ export declare function getFieldErrors(error: ZodError): Array<{
13
+ field: string;
14
+ message: string;
15
+ }>;
16
+ /**
17
+ * Preserves form input when validation fails
18
+ */
19
+ export declare function getOldInput(req: any): any;
20
+ /**
21
+ * Get validation errors for display
22
+ */
23
+ export declare function getErrors(req: any): Array<{
24
+ field: string;
25
+ message: string;
26
+ }>;
27
+ /**
28
+ * Check if a field has errors
29
+ */
30
+ export declare function hasFieldError(field: string, errors: any): boolean;
31
+ /**
32
+ * Get error message for a specific field
33
+ */
34
+ export declare function getFieldError(field: string, errors: any): string | undefined;
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validate = validate;
4
+ exports.getFieldErrors = getFieldErrors;
5
+ exports.getOldInput = getOldInput;
6
+ exports.getErrors = getErrors;
7
+ exports.hasFieldError = hasFieldError;
8
+ exports.getFieldError = getFieldError;
9
+ /**
10
+ * Zod validation middleware
11
+ * @param schema - Zod schema to validate against
12
+ * @param strategy - 'redirect' (web) or 'json' (API)
13
+ */
14
+ function validate(schema, strategy = "redirect") {
15
+ return async (req, res, next) => {
16
+ const result = await schema.safeParseAsync(req.body);
17
+ if (!result.success) {
18
+ const errors = getFieldErrors(result.error);
19
+ const oldInput = req.body;
20
+ if (strategy === "json") {
21
+ return res.status(422).json({ errors, oldInput });
22
+ }
23
+ // Redirect with errors and old input for web forms
24
+ req.flash("errors", errors);
25
+ req.flash("oldInput", oldInput);
26
+ // Get referrer URL or use default
27
+ const redirectUrl = req.headers.referer || req.headers.referrer || "/";
28
+ return res.redirect(redirectUrl);
29
+ }
30
+ req.validatedBody = result.data;
31
+ next();
32
+ };
33
+ }
34
+ /**
35
+ * Extract field errors from Zod ZodError
36
+ */
37
+ function getFieldErrors(error) {
38
+ return error.errors.map((err) => ({
39
+ field: err.path.join("."),
40
+ message: err.message,
41
+ }));
42
+ }
43
+ /**
44
+ * Preserves form input when validation fails
45
+ */
46
+ function getOldInput(req) {
47
+ return req.flash("oldInput")[0] || {};
48
+ }
49
+ /**
50
+ * Get validation errors for display
51
+ */
52
+ function getErrors(req) {
53
+ return req.flash("errors") || [];
54
+ }
55
+ /**
56
+ * Check if a field has errors
57
+ */
58
+ function hasFieldError(field, errors) {
59
+ return errors.some((err) => err.field === field);
60
+ }
61
+ /**
62
+ * Get error message for a specific field
63
+ */
64
+ function getFieldError(field, errors) {
65
+ const error = errors.find((err) => err.field === field);
66
+ return error?.message;
67
+ }
@@ -3,4 +3,5 @@ export type { MvcAppOptions, MvcApp } from "./App";
3
3
  export { getPrismaClient, disconnectPrisma } from "./db";
4
4
  export { hashPassword, verifyPassword, signToken, verifyToken, authenticate, } from "./Auth";
5
5
  export { startRegistration, completeRegistration, startAuthentication, completeAuthentication, getRPConfig, } from "./WebAuthn";
6
+ export { validate, getFieldErrors, getErrors, getOldInput } from "./Validation";
6
7
  export { registerControllers, registerController } from "./Router";
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.registerController = exports.registerControllers = exports.getRPConfig = exports.completeAuthentication = exports.startAuthentication = exports.completeRegistration = exports.startRegistration = exports.authenticate = exports.verifyToken = exports.signToken = exports.verifyPassword = exports.hashPassword = exports.disconnectPrisma = exports.getPrismaClient = exports.startServer = exports.createMvcApp = void 0;
3
+ exports.registerController = exports.registerControllers = exports.getOldInput = exports.getErrors = exports.getFieldErrors = exports.validate = exports.getRPConfig = exports.completeAuthentication = exports.startAuthentication = exports.completeRegistration = exports.startRegistration = exports.authenticate = exports.verifyToken = exports.signToken = exports.verifyPassword = exports.hashPassword = exports.disconnectPrisma = exports.getPrismaClient = exports.startServer = exports.createMvcApp = void 0;
4
4
  // App factory and server
5
5
  var App_1 = require("./App");
6
6
  Object.defineProperty(exports, "createMvcApp", { enumerable: true, get: function () { return App_1.createMvcApp; } });
@@ -23,6 +23,12 @@ Object.defineProperty(exports, "completeRegistration", { enumerable: true, get:
23
23
  Object.defineProperty(exports, "startAuthentication", { enumerable: true, get: function () { return WebAuthn_1.startAuthentication; } });
24
24
  Object.defineProperty(exports, "completeAuthentication", { enumerable: true, get: function () { return WebAuthn_1.completeAuthentication; } });
25
25
  Object.defineProperty(exports, "getRPConfig", { enumerable: true, get: function () { return WebAuthn_1.getRPConfig; } });
26
+ // Validation
27
+ var Validation_1 = require("./Validation");
28
+ Object.defineProperty(exports, "validate", { enumerable: true, get: function () { return Validation_1.validate; } });
29
+ Object.defineProperty(exports, "getFieldErrors", { enumerable: true, get: function () { return Validation_1.getFieldErrors; } });
30
+ Object.defineProperty(exports, "getErrors", { enumerable: true, get: function () { return Validation_1.getErrors; } });
31
+ Object.defineProperty(exports, "getOldInput", { enumerable: true, get: function () { return Validation_1.getOldInput; } });
26
32
  // Routing
27
33
  var Router_1 = require("./Router");
28
34
  Object.defineProperty(exports, "registerControllers", { enumerable: true, get: function () { return Router_1.registerControllers; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@erwininteractive/mvc",
3
- "version": "0.6.3",
3
+ "version": "0.6.5",
4
4
  "description": "A lightweight, full-featured MVC framework for Node.js with Express, Prisma, and EJS",
5
5
  "main": "dist/framework/index.js",
6
6
  "types": "dist/framework/index.d.ts",
@@ -52,7 +52,8 @@
52
52
  "helmet": "^8.0.0",
53
53
  "jsonwebtoken": "^9.0.2",
54
54
  "prisma": "^6.0.0",
55
- "redis": "^4.7.0"
55
+ "redis": "^4.7.0",
56
+ "zod": "^3.23.8"
56
57
  },
57
58
  "devDependencies": {
58
59
  "@types/bcryptjs": "^2.4.6",
@@ -12,11 +12,13 @@
12
12
  "db:push": "prisma db push"
13
13
  },
14
14
  "dependencies": {
15
- "@erwininteractive/mvc": "^0.4.0",
16
- "cookie-parser": "^1.4.6"
15
+ "@erwininteractive/mvc": "^0.6.0",
16
+ "cookie-parser": "^1.4.6",
17
+ "zod": "^3.23.8"
17
18
  },
18
19
  "devDependencies": {
19
20
  "@types/cookie-parser": "^1.4.7",
21
+ "@types/ejs": "^3.1.5",
20
22
  "@types/express": "^5.0.0",
21
23
  "@types/node": "^22.7.5",
22
24
  "tsx": "^4.19.1",