@dvsa/appdev-api-common 0.1.1 → 0.2.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/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +2 -1
- package/validation/request-body.d.ts +10 -0
- package/validation/request-body.js +52 -0
package/index.d.ts
CHANGED
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.
|
|
3
|
+
"version": "0.2.1",
|
|
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,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decorator tp validate an express request body against a specified schema
|
|
3
|
+
* @param {object} schema - the json schema you wish to use as the validator
|
|
4
|
+
* @param isArray - whether the body is expected to be an array
|
|
5
|
+
* @param errorDetails - whether to return detailed error messages (Note: errors are logged regardless of this setting)
|
|
6
|
+
*/
|
|
7
|
+
export declare function ValidateRequestBody<T>(schema: object, { isArray, errorDetails }?: {
|
|
8
|
+
isArray: boolean;
|
|
9
|
+
errorDetails: boolean;
|
|
10
|
+
}): (_target: T, _propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
@@ -0,0 +1,52 @@
|
|
|
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 - the json schema you wish to use as the validator
|
|
14
|
+
* @param isArray - whether the body is expected to be an array
|
|
15
|
+
* @param errorDetails - whether to return detailed error messages (Note: errors are logged regardless of this setting)
|
|
16
|
+
*/
|
|
17
|
+
function ValidateRequestBody(schema, { isArray, errorDetails } = { isArray: false, errorDetails: false }) {
|
|
18
|
+
return (_target, _propertyKey, descriptor) => {
|
|
19
|
+
const originalMethod = descriptor.value;
|
|
20
|
+
descriptor.value = async function (body, res, next) {
|
|
21
|
+
// just to be safe, check the bodies existence before attempting to validate it
|
|
22
|
+
if (!body) {
|
|
23
|
+
return res
|
|
24
|
+
.status(http_status_codes_1.HttpStatus.BAD_REQUEST)
|
|
25
|
+
.json({ message: "No request body detected" });
|
|
26
|
+
}
|
|
27
|
+
const payload = Buffer.isBuffer(body)
|
|
28
|
+
? JSON.parse(body.toString("utf-8"))
|
|
29
|
+
: body;
|
|
30
|
+
// Create the appropriate schema based on whether we're validating an array
|
|
31
|
+
const schemaToValidate = isArray
|
|
32
|
+
? { type: "array", items: schema }
|
|
33
|
+
: schema;
|
|
34
|
+
const validateFunction = ajv.compile(schemaToValidate);
|
|
35
|
+
// validate the request body against the schema passed in
|
|
36
|
+
const isValid = validateFunction(payload);
|
|
37
|
+
// if an error exists, then return a 400 with details
|
|
38
|
+
if (!isValid) {
|
|
39
|
+
console.error(validateFunction.errors);
|
|
40
|
+
const response = {
|
|
41
|
+
message: "Validation error",
|
|
42
|
+
};
|
|
43
|
+
if (errorDetails) {
|
|
44
|
+
Object.assign(response, { errors: validateFunction.errors });
|
|
45
|
+
}
|
|
46
|
+
return res.status(http_status_codes_1.HttpStatus.BAD_REQUEST).json(response);
|
|
47
|
+
}
|
|
48
|
+
// proceed with attached method if schema is valid
|
|
49
|
+
return originalMethod.apply(this, [body, res, next]);
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
}
|