@autofleet/sheilta 1.2.5-beta → 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.
@@ -24,17 +24,19 @@ const formatOrder = ({ order = [], associationModels = [], }) => (order.length =
24
24
  const formatPage = page => page || utils_1.PAGE_DEFAULT;
25
25
  const formatPerPage = perPage => perPage || utils_1.PER_PAGE_DEFAULT;
26
26
  const formatInclude = (include, associationsMap = {}) => {
27
- let formattedInclude = include.map(i => (Object.assign(Object.assign(Object.assign({}, i), { association: associationsMap[i.association || i.model], required: i.required !== 'false' }), (include.order && { order: formatOrder(include.order) }))));
27
+ let formattedInclude = include.map(i => (Object.assign(Object.assign({}, i), { association: associationsMap[i.association || i.model], required: i.required !== 'false' })));
28
28
  formattedInclude = formattedInclude.map(i => lodash_1.default.omit(i, ['model']));
29
29
  return formattedInclude;
30
30
  };
31
31
  const formatQuery = (query, associationModels) => Object.keys(query).reduce((acc, queryItemKey) => (Object.assign(Object.assign({}, acc), { [utils_1.isAttributeByAssociation(queryItemKey, associationModels)
32
32
  ? utils_1.wrapAttributeWithOperator(queryItemKey) : queryItemKey]: query[queryItemKey] })), {});
33
33
  const formatSearchTerm = (searchTerm, attributesToSend, rawAttributes) => ({
34
- $or: attributesToSend.filter(attrKey => rawAttributes[attrKey].type.key === 'STRING').map(attr => ({
35
- [attr]: {
36
- $iLike: `%${searchTerm}%`,
37
- },
34
+ $and: searchTerm.split(' ').map(term => ({
35
+ $or: attributesToSend.filter(attrKey => rawAttributes[attrKey].type.key === 'STRING').map(attr => ({
36
+ [attr]: {
37
+ $iLike: `%${term}%`,
38
+ },
39
+ })),
38
40
  })),
39
41
  });
40
42
  const formatPayload = ({ order, page = utils_1.PAGE_DEFAULT, perPage = utils_1.PER_PAGE_DEFAULT, include = [], query = {}, attributes = null, searchTerm = null, }, model) => {
@@ -65,13 +65,67 @@ describe('formatting test', () => {
65
65
  });
66
66
  expect(JSON.stringify(query)).toBe(JSON.stringify({
67
67
  $and: [{ 'json.field': { $gt: 4 } }, {
68
- $or: [
69
- {
70
- startTime: {
71
- $iLike: `%${searchTerm}%`,
72
- },
68
+ $and: [{
69
+ $or: [
70
+ {
71
+ startTime: {
72
+ $iLike: `%${searchTerm}%`,
73
+ },
74
+ },
75
+ ],
76
+ }],
77
+ }],
78
+ }));
79
+ });
80
+ it('query with searchTerm with spaces', () => {
81
+ const searchTerm = 'aviv good guy';
82
+ const splitSearch = searchTerm.split(' ');
83
+ const { query } = _1.default({
84
+ order: [],
85
+ query: {
86
+ 'json.field': {
87
+ $gt: 4,
88
+ },
89
+ },
90
+ searchTerm,
91
+ }, {
92
+ rawAttributes: {
93
+ startTime: {
94
+ type: {
95
+ key: 'STRING',
96
+ },
97
+ },
98
+ endTime: {
99
+ type: {
100
+ key: 'NUMBER',
101
+ },
102
+ },
103
+ },
104
+ });
105
+ expect(JSON.stringify(query)).toBe(JSON.stringify({
106
+ $and: [{ 'json.field': { $gt: 4 } }, {
107
+ $and: [{
108
+ $or: [
109
+ {
110
+ startTime: {
111
+ $iLike: `%${splitSearch[0]}%`,
112
+ },
113
+ }
114
+ ],
115
+ }, {
116
+ $or: [{
117
+ startTime: {
118
+ $iLike: `%${splitSearch[1]}%`,
119
+ },
120
+ }],
73
121
  },
74
- ],
122
+ {
123
+ $or: [{
124
+ startTime: {
125
+ $iLike: `%${splitSearch[2]}%`,
126
+ },
127
+ }],
128
+ }],
75
129
  }],
76
130
  }));
77
131
  });
@@ -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,
@@ -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 { rawAttributes } = associations[i.model].target;
74
- if (include.where) {
75
- validateQueryPayload(i.where, rawAttributes);
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
- if (include.order) {
78
- validateOrderAttributes(i.order, rawAttributes);
78
+ const { rawAttributes } = target;
79
+ const attributeKeys = Object.keys(rawAttributes);
80
+ if (i.where) {
81
+ validateQueryPayload(i.where, attributeKeys);
79
82
  }
80
- if (include.attributes) {
81
- validateAttributes(i.attributes, rawAttributes);
83
+ if (i.order) {
84
+ validateOrderAttributes(i.order, attributeKeys);
82
85
  }
83
- if (!lodash_1.default.isNil(include.required) && !lodash_1.default.isBoolean(include.required)) {
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.5-beta",
3
+ "version": "1.2.6",
4
4
  "description": "manage cache",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -29,21 +29,22 @@
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
- "typescript": "^3.9.5",
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
- "@types/jest": "^24.9.0"
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/**/*"
48
49
  ]
49
- }
50
+ }