hi-secure 1.0.6 → 1.0.7
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/core/HiSecure.d.ts +17 -2
- package/dist/core/HiSecure.d.ts.map +1 -1
- package/dist/core/HiSecure.js +135 -30
- package/dist/core/HiSecure.js.map +1 -1
- package/dist/core/useSecure.d.ts +7 -0
- package/dist/core/useSecure.d.ts.map +1 -1
- package/dist/core/useSecure.js +23 -114
- package/dist/core/useSecure.js.map +1 -1
- package/dist/index.d.ts +6 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -19
- package/dist/index.js.map +1 -1
- package/dist/utils/normalizeOptions.d.ts.map +1 -1
- package/dist/utils/normalizeOptions.js +8 -8
- package/dist/utils/normalizeOptions.js.map +1 -1
- package/package.json +1 -1
- package/src/core/HiSecure.ts +12 -12
- package/src/managers/ValidatorManager.ts +138 -138
|
@@ -1,74 +1,74 @@
|
|
|
1
|
-
// src/managers/ValidatorManager.ts - COMPLETE FIXED
|
|
2
|
-
import { logger } from "../logging";
|
|
3
|
-
import { ValidationError } from "../core/errors/ValidationError.js";
|
|
4
|
-
import { HiSecureConfig } from "../core/types/HiSecureConfig.js"; // ✅ FIXED IMPORT
|
|
1
|
+
// // src/managers/ValidatorManager.ts - COMPLETE FIXED
|
|
2
|
+
// import { logger } from "../logging";
|
|
3
|
+
// import { ValidationError } from "../core/errors/ValidationError.js";
|
|
4
|
+
// import { HiSecureConfig } from "../core/types/HiSecureConfig.js"; // ✅ FIXED IMPORT
|
|
5
5
|
|
|
6
|
-
interface ValidatorAdapter {
|
|
7
|
-
|
|
8
|
-
}
|
|
6
|
+
// interface ValidatorAdapter {
|
|
7
|
+
// validate: (schema?: any) => any;
|
|
8
|
+
// }
|
|
9
9
|
|
|
10
|
-
export class ValidatorManager {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
constructor(
|
|
16
|
-
config: HiSecureConfig["validation"],
|
|
17
|
-
primaryAdapter: ValidatorAdapter,
|
|
18
|
-
fallbackAdapter: ValidatorAdapter | null
|
|
19
|
-
) {
|
|
20
|
-
this.config = config;
|
|
21
|
-
this.primaryAdapter = primaryAdapter;
|
|
22
|
-
this.fallbackAdapter = fallbackAdapter;
|
|
23
|
-
}
|
|
10
|
+
// export class ValidatorManager {
|
|
11
|
+
// private config: HiSecureConfig["validation"];
|
|
12
|
+
// private primaryAdapter: ValidatorAdapter;
|
|
13
|
+
// private fallbackAdapter: ValidatorAdapter | null;
|
|
24
14
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
15
|
+
// constructor(
|
|
16
|
+
// config: HiSecureConfig["validation"],
|
|
17
|
+
// primaryAdapter: ValidatorAdapter,
|
|
18
|
+
// fallbackAdapter: ValidatorAdapter | null
|
|
19
|
+
// ) {
|
|
20
|
+
// this.config = config;
|
|
21
|
+
// this.primaryAdapter = primaryAdapter;
|
|
22
|
+
// this.fallbackAdapter = fallbackAdapter;
|
|
23
|
+
// }
|
|
24
|
+
|
|
25
|
+
// validate(schema?: any) {
|
|
26
|
+
// return (req: any, res: any, next: any) => {
|
|
27
|
+
// // Execute primary adapter middleware
|
|
28
|
+
// const primaryMiddleware = this.primaryAdapter.validate(schema);
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
// // Run middleware and handle errors properly
|
|
31
|
+
// primaryMiddleware(req, res, (err?: any) => {
|
|
32
|
+
// if (!err) {
|
|
33
|
+
// return next(); // Validation passed
|
|
34
|
+
// }
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
36
|
+
// // If error is a ValidationError, pass it through (don't fallback!)
|
|
37
|
+
// if (err instanceof ValidationError) {
|
|
38
|
+
// logger.warn("⚠ Validation failed", {
|
|
39
|
+
// path: req.path,
|
|
40
|
+
// method: req.method,
|
|
41
|
+
// error: err.message
|
|
42
|
+
// });
|
|
43
|
+
// return next(err);
|
|
44
|
+
// }
|
|
45
45
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
46
|
+
// // Only use fallback for ADAPTER errors, not validation errors
|
|
47
|
+
// logger.warn("⚠ Primary validator adapter failed", {
|
|
48
|
+
// error: err?.message,
|
|
49
|
+
// path: req.path,
|
|
50
|
+
// method: req.method
|
|
51
|
+
// });
|
|
52
|
+
|
|
53
|
+
// if (!this.fallbackAdapter) {
|
|
54
|
+
// return next(new ValidationError("Validation system error"));
|
|
55
|
+
// }
|
|
52
56
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
});
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
}
|
|
57
|
+
// // Try fallback adapter
|
|
58
|
+
// const fallbackMiddleware = this.fallbackAdapter.validate(schema);
|
|
59
|
+
// fallbackMiddleware(req, res, (fallbackErr?: any) => {
|
|
60
|
+
// if (fallbackErr) {
|
|
61
|
+
// logger.error("❌ Fallback validator also failed", {
|
|
62
|
+
// error: fallbackErr?.message
|
|
63
|
+
// });
|
|
64
|
+
// return next(new ValidationError("Validation system unavailable"));
|
|
65
|
+
// }
|
|
66
|
+
// next(); // Fallback validation passed
|
|
67
|
+
// });
|
|
68
|
+
// });
|
|
69
|
+
// };
|
|
70
|
+
// }
|
|
71
|
+
// }
|
|
72
72
|
|
|
73
73
|
|
|
74
74
|
|
|
@@ -130,78 +130,78 @@ export class ValidatorManager {
|
|
|
130
130
|
|
|
131
131
|
|
|
132
132
|
|
|
133
|
-
//
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
//
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
//
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
//
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
133
|
+
// src/managers/ValidatorManager.ts
|
|
134
|
+
import { logger } from "../logging";
|
|
135
|
+
import { ValidationError } from "../core/errors/ValidationError.js";
|
|
136
|
+
|
|
137
|
+
interface ValidatorAdapter {
|
|
138
|
+
validate: (schema?: any) => any;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export class ValidatorManager {
|
|
142
|
+
private zodAdapter: ValidatorAdapter;
|
|
143
|
+
private expressAdapter: ValidatorAdapter;
|
|
144
|
+
|
|
145
|
+
constructor(zodAdapter: ValidatorAdapter, expressAdapter: ValidatorAdapter) {
|
|
146
|
+
this.zodAdapter = zodAdapter;
|
|
147
|
+
this.expressAdapter = expressAdapter;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
validate(schema?: any) {
|
|
151
|
+
// const isZod = schema && typeof schema.safeParse === "function";
|
|
152
|
+
const isZod =
|
|
153
|
+
schema &&
|
|
154
|
+
typeof schema === "object" &&
|
|
155
|
+
typeof schema._def === "object" &&
|
|
156
|
+
typeof schema.safeParse === "function";
|
|
157
|
+
|
|
158
|
+
const isExpressValidator = Array.isArray(schema);
|
|
159
|
+
|
|
160
|
+
return (req: any, res: any, next: any) => {
|
|
161
|
+
let middleware;
|
|
162
|
+
|
|
163
|
+
if (isZod) {
|
|
164
|
+
logger.debug("📌 Using Zod adapter");
|
|
165
|
+
middleware = this.zodAdapter.validate(schema);
|
|
166
|
+
}
|
|
167
|
+
else if (isExpressValidator) {
|
|
168
|
+
logger.debug("📌 Using express-validator adapter");
|
|
169
|
+
middleware = this.expressAdapter.validate(schema);
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
return next(); // no schema found
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// CASE 1 — express-validator returns ARRAY
|
|
176
|
+
if (Array.isArray(middleware)) {
|
|
177
|
+
let idx = 0;
|
|
178
|
+
|
|
179
|
+
const run = (err?: any) => {
|
|
180
|
+
if (err) return next(err);
|
|
181
|
+
|
|
182
|
+
const fn = middleware[idx++];
|
|
183
|
+
if (!fn) return next(); // done
|
|
184
|
+
|
|
185
|
+
try {
|
|
186
|
+
fn(req, res, run);
|
|
187
|
+
} catch (error: any) {
|
|
188
|
+
next(new ValidationError(error.message));
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
return run();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// CASE 2 — Zod returns SINGLE MIDDLEWARE
|
|
196
|
+
try {
|
|
197
|
+
middleware(req, res, (err?: any) => {
|
|
198
|
+
if (err) return next(err);
|
|
199
|
+
next();
|
|
200
|
+
});
|
|
201
|
+
} catch (err: any) {
|
|
202
|
+
next(new ValidationError(err.message));
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
207
|
|