@autofleet/sheilta 1.3.1 → 1.3.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/lib/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  import { formatOperators } from './operators';
2
2
  import { queryFormatMiddleware, queryValidationMiddleware } from './middleware';
3
- export { formatOperators, queryFormatMiddleware, queryValidationMiddleware, };
3
+ import { MiddlewareValidationOption } from './interface';
4
+ export { formatOperators, queryFormatMiddleware, queryValidationMiddleware, MiddlewareValidationOption, };
@@ -0,0 +1,3 @@
1
+ export declare type MiddlewareValidationOption = {
2
+ literalAttribute?: string[];
3
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,5 +1,6 @@
1
1
  import { FormatPayloadOptions } from '../formatter';
2
- export declare const newQueryValidationMiddleware: (inner?: string) => (model: any) => (req: any, res: any, next: any) => Promise<void>;
2
+ import { MiddlewareValidationOption } from '../interface';
3
+ export declare const newQueryValidationMiddleware: (inner?: string) => (model: any, options?: MiddlewareValidationOption) => (req: any, res: any, next: any) => Promise<void>;
3
4
  export declare const newQueryFormatMiddleware: (inner?: string) => (model: any, options?: FormatPayloadOptions) => (req: any, res: any, next: any) => Promise<void>;
4
- export declare const queryValidationMiddleware: (model: any) => (req: any, res: any, next: any) => Promise<void>;
5
+ export declare const queryValidationMiddleware: (model: any, options?: MiddlewareValidationOption) => (req: any, res: any, next: any) => Promise<void>;
5
6
  export declare const queryFormatMiddleware: (model: any, options?: FormatPayloadOptions) => (req: any, res: any, next: any) => Promise<void>;
@@ -26,7 +26,7 @@ const querySchema = joi_1.object({
26
26
  include: joi_1.array().items(joi_1.any()),
27
27
  searchTerm: joi_1.string(),
28
28
  });
29
- exports.newQueryValidationMiddleware = (inner = 'body') => model => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
29
+ exports.newQueryValidationMiddleware = (inner = 'body') => (model, options = {}) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
30
30
  const { query, attributes, order, page, perPage, include, } = req[inner];
31
31
  try {
32
32
  const result = querySchema.validate(req[inner]);
@@ -40,7 +40,7 @@ exports.newQueryValidationMiddleware = (inner = 'body') => model => (req, res, n
40
40
  page,
41
41
  perPage,
42
42
  include,
43
- }, model);
43
+ }, model, options);
44
44
  return next();
45
45
  }
46
46
  catch (error) {
@@ -1,3 +1,4 @@
1
+ import { MiddlewareValidationOption } from '../interface';
1
2
  export declare const validatePayload: ({ query, order, attributes, include, page, perPage, }: {
2
3
  query?: {};
3
4
  order?: any[];
@@ -5,4 +6,4 @@ export declare const validatePayload: ({ query, order, attributes, include, page
5
6
  include?: any[];
6
7
  page?: number;
7
8
  perPage?: number;
8
- }, model?: any) => boolean;
9
+ }, model?: any, options?: MiddlewareValidationOption) => boolean;
@@ -11,7 +11,8 @@ const validateOperator = (operator) => operators_1.OPERATORS.includes(operator.s
11
11
  const validateQueryAttribute = (attribute, modelAttributes = [], associationModels = []) => [...modelAttributes, ...associationModels]
12
12
  .includes(attribute.includes(utils_1.ASSOCIATION_PREFIX)
13
13
  ? attribute.split(utils_1.ASSOCIATION_PREFIX)[0] : attribute);
14
- const validateSingleOrder = (currentOrder, rawAttributes, associationModels) => {
14
+ const validateSingleOrder = (currentOrder, rawAttributes, associationModels, options = {}) => {
15
+ var _a;
15
16
  const isOrderDescOrder = utils_1.isOrderDesc(currentOrder);
16
17
  if (isOrderDescOrder && currentOrder[0] !== utils_1.ORDER_PREFIX) {
17
18
  utils_1.throwBadRequestError(`${utils_1.ORDER_PREFIX} must be only at the beginning of the word`);
@@ -20,10 +21,11 @@ const validateSingleOrder = (currentOrder, rawAttributes, associationModels) =>
20
21
  currentOrder.split(utils_1.ORDER_PREFIX)[1] : currentOrder;
21
22
  const isOrderAssociation = utils_1.isAttributeByAssociation(orderStringWithoutDesc, associationModels);
22
23
  let formattedOrderString = utils_1.extractAttributeNameFromOrder(currentOrder, associationModels);
24
+ const isLiteralAttribute = (_a = options === null || options === void 0 ? void 0 : options.literalAttribute) === null || _a === void 0 ? void 0 : _a.includes(orderStringWithoutDesc);
23
25
  if (!isOrderAssociation && formattedOrderString.includes(utils_1.ASSOCIATION_PREFIX)) {
24
26
  [formattedOrderString] = formattedOrderString.split(utils_1.ASSOCIATION_PREFIX);
25
27
  }
26
- if (!(rawAttributes.includes(formattedOrderString) || isOrderAssociation)) {
28
+ if (!(rawAttributes.includes(formattedOrderString) || isOrderAssociation || isLiteralAttribute)) {
27
29
  utils_1.throwBadRequestError(`${currentOrder} is invalid`);
28
30
  }
29
31
  };
@@ -32,8 +34,8 @@ const validateSingleAttribute = (currentAttribute, rawAttributes) => {
32
34
  utils_1.throwBadRequestError(`${currentAttribute} is invalid`);
33
35
  }
34
36
  };
35
- const validateOrderAttributes = (order, rawAttributes, associationModels = []) => {
36
- order.map(o => validateSingleOrder(o, rawAttributes, associationModels));
37
+ const validateOrderAttributes = (order, rawAttributes, associationModels = [], options = {}) => {
38
+ order.map(o => validateSingleOrder(o, rawAttributes, associationModels, options));
37
39
  };
38
40
  const validateAttributes = (attributes, rawAttributes) => {
39
41
  attributes.map(a => validateSingleAttribute(a, rawAttributes));
@@ -91,7 +93,7 @@ const validateIncludePayload = (include, associations) => {
91
93
  }
92
94
  });
93
95
  };
94
- exports.validatePayload = ({ query = {}, order = [], attributes = [], include = [], page = utils_1.PAGE_DEFAULT, perPage = utils_1.PER_PAGE_DEFAULT, }, model) => {
96
+ exports.validatePayload = ({ query = {}, order = [], attributes = [], include = [], page = utils_1.PAGE_DEFAULT, perPage = utils_1.PER_PAGE_DEFAULT, }, model, options = {}) => {
95
97
  const rawAttributes = Object.keys(model.rawAttributes);
96
98
  const associationModels = Object.keys((model === null || model === void 0 ? void 0 : model.associations) || {});
97
99
  if (!attributes || attributes.length === 0) {
@@ -101,7 +103,7 @@ exports.validatePayload = ({ query = {}, order = [], attributes = [], include =
101
103
  else {
102
104
  validateAttributes(attributes, rawAttributes);
103
105
  }
104
- validateOrderAttributes(order, rawAttributes, associationModels);
106
+ validateOrderAttributes(order, rawAttributes, associationModels, options);
105
107
  validateQueryPayload(query, rawAttributes, associationModels);
106
108
  if (include.length && typeof include === 'object') {
107
109
  validateIncludePayload(include, model === null || model === void 0 ? void 0 : model.associations);
@@ -180,5 +180,28 @@ describe('validations test', () => {
180
180
  expect(error.statusCode).toBe(400);
181
181
  }
182
182
  });
183
+ it('try order by literal field that exist', () => {
184
+ const literalAttribute = 'genericField';
185
+ const payload = _1.validatePayload({ order: [literalAttribute] }, {
186
+ rawAttributes: {
187
+ startTime: '', endTime: '', currencySymbol: '', currencyCode: '', startStationId: '', status: '',
188
+ },
189
+ }, {
190
+ literalAttribute: [literalAttribute],
191
+ });
192
+ expect(payload).toBe(true);
193
+ });
194
+ it('try order by literal field that doesnt exist', () => {
195
+ try {
196
+ _1.validatePayload({ order: ['here'] }, {
197
+ rawAttributes: {
198
+ startTime: '', endTime: '', currencySymbol: '', currencyCode: '', startStationId: '', status: '',
199
+ },
200
+ }, { literalAttribute: ['not'] });
201
+ }
202
+ catch (error) {
203
+ expect(error.statusCode).toBe(400);
204
+ }
205
+ });
183
206
  });
184
207
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sheilta",
3
- "version": "1.3.1",
3
+ "version": "1.3.2-1",
4
4
  "description": "manage cache",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -48,4 +48,4 @@
48
48
  "files": [
49
49
  "lib/**/*"
50
50
  ]
51
- }
51
+ }