@dvsa/appdev-api-common 0.1.1 → 0.2.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/index.d.ts CHANGED
@@ -2,3 +2,4 @@ export * from "./api/http-status-codes";
2
2
  export * from "./auth/auth-checker";
3
3
  export * from "./auth/auth-errors";
4
4
  export * from "./auth/verify-jwt";
5
+ export * from "./validation/request-body";
package/index.js CHANGED
@@ -18,3 +18,4 @@ __exportStar(require("./api/http-status-codes"), exports);
18
18
  __exportStar(require("./auth/auth-checker"), exports);
19
19
  __exportStar(require("./auth/auth-errors"), exports);
20
20
  __exportStar(require("./auth/verify-jwt"), exports);
21
+ __exportStar(require("./validation/request-body"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dvsa/appdev-api-common",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "keywords": [
5
5
  "dvsa",
6
6
  "nodejs",
@@ -23,6 +23,7 @@
23
23
  "postpublish": "git clean -fd && npm run clean:temp"
24
24
  },
25
25
  "dependencies": {
26
+ "ajv": "^8.17.1",
26
27
  "jose": "^5.9.6"
27
28
  },
28
29
  "devDependencies": {
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Decorator tp validate an express request body against a specified schema
3
+ * @param {object} schema
4
+ * @param errorDetails - whether to return detailed error messages (Note: errors are logged regardless of this setting)
5
+ */
6
+ export declare function ValidateRequestBody<T>(schema: object, { errorDetails }?: {
7
+ errorDetails: boolean;
8
+ }): (_target: T, _propertyKey: string, descriptor: PropertyDescriptor) => void;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ValidateRequestBody = ValidateRequestBody;
7
+ const ajv_1 = __importDefault(require("ajv"));
8
+ const http_status_codes_1 = require("../api/http-status-codes");
9
+ const ajv = new ajv_1.default({ removeAdditional: true, allErrors: true });
10
+ ajv.addKeyword("tsEnumNames");
11
+ /**
12
+ * Decorator tp validate an express request body against a specified schema
13
+ * @param {object} schema
14
+ * @param errorDetails - whether to return detailed error messages (Note: errors are logged regardless of this setting)
15
+ */
16
+ function ValidateRequestBody(schema, { errorDetails } = { errorDetails: false }) {
17
+ return (_target, _propertyKey, descriptor) => {
18
+ const originalMethod = descriptor.value;
19
+ descriptor.value = async function (body, res, next) {
20
+ // just to be safe, check the bodies existence before attempting to validate it
21
+ if (!body) {
22
+ return res
23
+ .status(http_status_codes_1.HttpStatus.BAD_REQUEST)
24
+ .json({ message: "No request body detected" });
25
+ }
26
+ const payload = Buffer.isBuffer(body)
27
+ ? JSON.parse(body.toString("utf-8"))
28
+ : body;
29
+ const validateFunction = ajv.compile(schema);
30
+ // validate the request body against the schema passed in
31
+ const isValid = validateFunction(payload);
32
+ // if an error exists, then return a 400 with details
33
+ if (!isValid) {
34
+ console.error(validateFunction.errors);
35
+ const response = {
36
+ message: "Validation error",
37
+ };
38
+ if (errorDetails) {
39
+ Object.assign(response, { errors: validateFunction.errors });
40
+ }
41
+ return res.status(http_status_codes_1.HttpStatus.BAD_REQUEST).json(response);
42
+ }
43
+ // proceed with attached method if schema is valid
44
+ return originalMethod.apply(this, [body, res, next]);
45
+ };
46
+ };
47
+ }