@autofleet/sheilta 1.2.5 → 1.2.6
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/lib/middleware/index.js +13 -0
- package/lib/validations/index.js +18 -9
- package/package.json +8 -7
package/lib/middleware/index.js
CHANGED
|
@@ -14,11 +14,24 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.queryFormatMiddleware = exports.queryValidationMiddleware = exports.newQueryFormatMiddleware = exports.newQueryValidationMiddleware = void 0;
|
|
16
16
|
const errors_1 = require("@autofleet/errors");
|
|
17
|
+
const joi_1 = require("joi");
|
|
17
18
|
const formatter_1 = __importDefault(require("../formatter"));
|
|
18
19
|
const validations_1 = require("../validations");
|
|
20
|
+
const querySchema = joi_1.object({
|
|
21
|
+
query: joi_1.object(),
|
|
22
|
+
attributes: joi_1.array().items(joi_1.string()),
|
|
23
|
+
order: joi_1.array().items(joi_1.string()),
|
|
24
|
+
page: joi_1.number(),
|
|
25
|
+
perPage: joi_1.number(),
|
|
26
|
+
include: joi_1.array().items(joi_1.any()),
|
|
27
|
+
});
|
|
19
28
|
exports.newQueryValidationMiddleware = (inner = 'body') => model => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
20
29
|
const { query, attributes, order, page, perPage, include, } = req[inner];
|
|
21
30
|
try {
|
|
31
|
+
const result = querySchema.validate(req[inner]);
|
|
32
|
+
if (result.error) {
|
|
33
|
+
throw new errors_1.BadRequest([result.error], null);
|
|
34
|
+
}
|
|
22
35
|
validations_1.validatePayload({
|
|
23
36
|
query,
|
|
24
37
|
attributes,
|
package/lib/validations/index.js
CHANGED
|
@@ -69,18 +69,24 @@ const validatePagination = ({ page, perPage, }) => {
|
|
|
69
69
|
const validateIncludePayload = (include, associations) => {
|
|
70
70
|
const associationsKeys = Object.keys(associations);
|
|
71
71
|
include.forEach((i) => {
|
|
72
|
+
var _a;
|
|
72
73
|
validateQueryAttribute(i.model, associationsKeys);
|
|
73
|
-
const
|
|
74
|
-
if (
|
|
75
|
-
|
|
74
|
+
const target = (_a = associations[i.model]) === null || _a === void 0 ? void 0 : _a.target;
|
|
75
|
+
if (!target) {
|
|
76
|
+
utils_1.throwBadRequestError('model not found in associations');
|
|
76
77
|
}
|
|
77
|
-
|
|
78
|
-
|
|
78
|
+
const { rawAttributes } = target;
|
|
79
|
+
const attributeKeys = Object.keys(rawAttributes);
|
|
80
|
+
if (i.where) {
|
|
81
|
+
validateQueryPayload(i.where, attributeKeys);
|
|
79
82
|
}
|
|
80
|
-
if (
|
|
81
|
-
|
|
83
|
+
if (i.order) {
|
|
84
|
+
validateOrderAttributes(i.order, attributeKeys);
|
|
82
85
|
}
|
|
83
|
-
if (
|
|
86
|
+
if (i.attributes) {
|
|
87
|
+
validateAttributes(i.attributes, attributeKeys);
|
|
88
|
+
}
|
|
89
|
+
if (!lodash_1.default.isNil(i.required) && !lodash_1.default.isBoolean(i.required)) {
|
|
84
90
|
utils_1.throwBadRequestError('include.required must be a boolean');
|
|
85
91
|
}
|
|
86
92
|
});
|
|
@@ -97,9 +103,12 @@ exports.validatePayload = ({ query = {}, order = [], attributes = [], include =
|
|
|
97
103
|
}
|
|
98
104
|
validateOrderAttributes(order, rawAttributes, associationModels);
|
|
99
105
|
validateQueryPayload(query, rawAttributes, associationModels);
|
|
100
|
-
if (include.length) {
|
|
106
|
+
if (include.length && typeof include === 'object') {
|
|
101
107
|
validateIncludePayload(include, model === null || model === void 0 ? void 0 : model.associations);
|
|
102
108
|
}
|
|
109
|
+
else if (include && typeof include !== 'object') {
|
|
110
|
+
utils_1.throwBadRequestError('include must be an array');
|
|
111
|
+
}
|
|
103
112
|
validatePagination({
|
|
104
113
|
page,
|
|
105
114
|
perPage,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/sheilta",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.6",
|
|
4
4
|
"description": "manage cache",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -29,19 +29,20 @@
|
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"@autofleet/errors": "^1.0.10",
|
|
31
31
|
"@types/node": "^14.14.20",
|
|
32
|
+
"joi": "^17.9.2",
|
|
32
33
|
"lodash": "^4.17.21"
|
|
33
34
|
},
|
|
34
35
|
"devDependencies": {
|
|
35
|
-
"
|
|
36
|
+
"@types/jest": "^24.9.0",
|
|
37
|
+
"@typescript-eslint/eslint-plugin": "^2.34.0",
|
|
38
|
+
"@typescript-eslint/parser": "^2.23.0",
|
|
36
39
|
"eslint": "^6.8.0",
|
|
37
40
|
"eslint-config-airbnb": "^16.1.0",
|
|
38
41
|
"eslint-plugin-import": "^2.20.1",
|
|
39
|
-
"@typescript-eslint/eslint-plugin": "^2.34.0",
|
|
40
|
-
"@typescript-eslint/parser": "^2.23.0",
|
|
41
|
-
"typescript-eslint": "0.0.1-alpha.0",
|
|
42
|
-
"ts-jest": "^25.0.0",
|
|
43
42
|
"jest": "^25.1.0",
|
|
44
|
-
"
|
|
43
|
+
"ts-jest": "^25.0.0",
|
|
44
|
+
"typescript": "^3.9.5",
|
|
45
|
+
"typescript-eslint": "0.0.1-alpha.0"
|
|
45
46
|
},
|
|
46
47
|
"files": [
|
|
47
48
|
"lib/**/*"
|