@autofleet/sadot 0.6.4-beta.1 → 0.6.5

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.
Files changed (62) hide show
  1. package/dist/api/v1/definition/validations.js +21 -4
  2. package/dist/hooks/create.d.ts +1 -1
  3. package/dist/hooks/enrich.d.ts +1 -1
  4. package/dist/hooks/update.d.ts +1 -1
  5. package/dist/index.d.ts +2 -3
  6. package/dist/index.js +2 -4
  7. package/dist/models/CustomFieldDefinition.d.ts +1 -1
  8. package/dist/models/CustomFieldDefinition.js +11 -6
  9. package/dist/models/CustomFieldValue.js +3 -8
  10. package/dist/repository/definition.d.ts +2 -2
  11. package/dist/repository/value.d.ts +2 -2
  12. package/dist/scopes/filter.d.ts +8 -3
  13. package/dist/scopes/filter.js +36 -27
  14. package/dist/tests/mocks/definition.mock.d.ts +3 -3
  15. package/dist/tests/mocks/definition.mock.js +8 -10
  16. package/dist/types/index.d.ts +2 -2
  17. package/dist/utils/constants/index.d.ts +13 -0
  18. package/dist/utils/constants/index.js +15 -2
  19. package/dist/utils/helpers/index.d.ts +3 -3
  20. package/dist/utils/helpers/index.js +3 -3
  21. package/dist/utils/validations/index.d.ts +2 -2
  22. package/dist/utils/validations/index.js +15 -15
  23. package/dist/utils/validations/{custom-fields.js → schema/custom-fields.js} +0 -1
  24. package/dist/utils/validations/type.d.ts +10 -14
  25. package/dist/utils/validations/type.js +0 -48
  26. package/dist/utils/validations/validators/index.d.ts +14 -0
  27. package/dist/utils/validations/validators/index.js +33 -0
  28. package/dist/utils/validations/validators/select.validator.d.ts +5 -0
  29. package/dist/utils/validations/validators/select.validator.js +9 -0
  30. package/dist/utils/validations/validators/status.validator.d.ts +12 -0
  31. package/dist/utils/validations/validators/status.validator.js +9 -0
  32. package/package.json +9 -8
  33. package/src/api/v1/definition/index.ts +1 -1
  34. package/src/api/v1/definition/validations.ts +20 -1
  35. package/src/hooks/create.ts +1 -1
  36. package/src/hooks/enrich.ts +4 -4
  37. package/src/hooks/update.ts +1 -1
  38. package/src/index.ts +2 -3
  39. package/src/models/CustomFieldDefinition.ts +7 -5
  40. package/src/models/CustomFieldValue.ts +1 -5
  41. package/src/repository/definition.ts +2 -5
  42. package/src/repository/value.ts +2 -2
  43. package/src/scopes/filter.ts +43 -30
  44. package/src/tests/functional/searching/index.ts +1 -1
  45. package/src/tests/mocks/definition.mock.ts +6 -8
  46. package/src/types/index.ts +2 -2
  47. package/src/utils/constants/index.ts +14 -1
  48. package/src/utils/helpers/index.ts +5 -5
  49. package/src/utils/init.ts +1 -1
  50. package/src/utils/validations/index.ts +18 -15
  51. package/src/utils/validations/{custom-fields.ts → schema/custom-fields.ts} +0 -1
  52. package/src/utils/validations/type.ts +14 -40
  53. package/src/utils/validations/validators/index.ts +31 -0
  54. package/src/utils/validations/validators/select.validator.ts +11 -0
  55. package/src/utils/validations/validators/status.validator.ts +18 -0
  56. package/dist/utils/validations/custom.d.ts +0 -15
  57. package/dist/utils/validations/custom.js +0 -42
  58. package/dist/utils/validations/validators.d.ts +0 -12
  59. package/dist/utils/validations/validators.js +0 -33
  60. package/src/utils/validations/custom.ts +0 -39
  61. package/src/utils/validations/validators.ts +0 -34
  62. /package/dist/utils/validations/{custom-fields.d.ts → schema/custom-fields.d.ts} +0 -0
@@ -0,0 +1,31 @@
1
+ import Joi from 'joi';
2
+ import { CustomFieldDefinitionType } from '../../constants';
3
+ import type { Validators } from '../type';
4
+ import { validateSelect } from './select.validator';
5
+ import { validateStatus } from './status.validator';
6
+
7
+ type CustomValidationTypes = Extract<CustomFieldDefinitionType, 'select' | 'status'>;
8
+ /**
9
+ * Custom field types that must have custom validation.
10
+ * TODO: remove this after moving to schema-based validation
11
+ */
12
+ export const CustomValidationTypes = {
13
+ [CustomFieldDefinitionType.SELECT]: CustomFieldDefinitionType.SELECT,
14
+ [CustomFieldDefinitionType.STATUS]: CustomFieldDefinitionType.STATUS,
15
+ } as const;
16
+
17
+ /**
18
+ * Validators for custom fields
19
+ */
20
+ export const validators: Validators = {
21
+ [CustomFieldDefinitionType.SELECT]: validateSelect,
22
+ [CustomFieldDefinitionType.STATUS]: validateStatus,
23
+ [CustomFieldDefinitionType.TEXT]: (value) => (typeof value === 'string'),
24
+ [CustomFieldDefinitionType.NUMBER]: (value) => (typeof value === 'number'),
25
+ [CustomFieldDefinitionType.BOOLEAN]: (value) => (typeof value === 'boolean'),
26
+ [CustomFieldDefinitionType.DATE]: (value) => (!Joi.date().validate(value).error),
27
+ [CustomFieldDefinitionType.DATETIME]: (value) => (!Joi.date().validate(value).error),
28
+ [CustomFieldDefinitionType.IMAGE]: (value) => (!Joi.array().min(1).unique()
29
+ .items(Joi.string().uri())
30
+ .validate(value).error),
31
+ };
@@ -0,0 +1,11 @@
1
+ import type { Validator } from '../type';
2
+
3
+ /**
4
+ * Validate that the value is one of the select values
5
+ */
6
+ export const validateSelect: Validator<string, string[]> = (
7
+ value,
8
+ selectValues,
9
+ ) => (Array.isArray(selectValues)
10
+ && selectValues.includes(value)
11
+ );
@@ -0,0 +1,18 @@
1
+ import type { Validator } from '../type';
2
+
3
+ type StatusColor = string | null; // TODO: Takes from @autofleet/colors ?
4
+ type StatusValue = string;
5
+ type StatusOption = {
6
+ value: StatusValue;
7
+ color: StatusColor;
8
+ }
9
+
10
+ /**
11
+ * Validate that the value is one of the status values
12
+ */
13
+ export const validateStatus: Validator<StatusValue, StatusOption[]> = (
14
+ value,
15
+ statusValues,
16
+ ) => (Array.isArray(statusValues)
17
+ && statusValues.some((status) => status.value === value)
18
+ );
@@ -1,15 +0,0 @@
1
- export declare const mustHaveCustomValidation: {
2
- select: boolean;
3
- };
4
- /**
5
- * Validates the given validations object against the supported field types and their validators.
6
- * @return true if the validation is valid, false otherwise.
7
- */
8
- export declare const validateValidation: (valueType: any, validation: any) => boolean;
9
- /**
10
- * Validates the given value against the custom validation rules for the specified field type.
11
- * If no custom validation rules are provided, it falls back to the default validation.
12
- * @returns true if the value is valid according to the validation rules, false otherwise.
13
- */
14
- declare const customValidation: (value: any, valueType: any, validation: any) => boolean;
15
- export default customValidation;
@@ -1,42 +0,0 @@
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.validateValidation = exports.mustHaveCustomValidation = void 0;
7
- /* eslint-disable no-shadow */
8
- const logger_1 = __importDefault(require("../logger"));
9
- const type_1 = require("./type");
10
- const validators_1 = __importDefault(require("./validators"));
11
- exports.mustHaveCustomValidation = {
12
- [type_1.CustomFieldDefinitionType.SELECT]: true,
13
- };
14
- /**
15
- * Validates the given validations object against the supported field types and their validators.
16
- * @return true if the validation is valid, false otherwise.
17
- */
18
- const validateValidation = (valueType, validation) => {
19
- if (!validation) {
20
- if (exports.mustHaveCustomValidation[valueType]) {
21
- logger_1.default.error(`No custom validation for custom field type ${valueType} found`);
22
- return false;
23
- }
24
- return true;
25
- }
26
- return true;
27
- };
28
- exports.validateValidation = validateValidation;
29
- /**
30
- * Validates the given value against the custom validation rules for the specified field type.
31
- * If no custom validation rules are provided, it falls back to the default validation.
32
- * @returns true if the value is valid according to the validation rules, false otherwise.
33
- */
34
- const customValidation = (value, valueType, validation) => {
35
- const validator = validators_1.default?.[valueType];
36
- if (!validation || !validator) {
37
- return (0, exports.validateValidation)(valueType, validation);
38
- }
39
- // Always allow null values
40
- return value === null || validator(value, validation);
41
- };
42
- exports.default = customValidation;
@@ -1,12 +0,0 @@
1
- export declare enum CustomValidations {
2
- SELECT = "select",
3
- RANGE = "between"
4
- }
5
- type Validator = (value: any, validation: any) => boolean;
6
- /**
7
- * Validators for custom fields
8
- */
9
- declare const validators: {
10
- [key: string]: Validator;
11
- };
12
- export default validators;
@@ -1,33 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CustomValidations = void 0;
4
- // eslint-disable-next-line no-shadow
5
- var CustomValidations;
6
- (function (CustomValidations) {
7
- CustomValidations["SELECT"] = "select";
8
- CustomValidations["RANGE"] = "between";
9
- })(CustomValidations = exports.CustomValidations || (exports.CustomValidations = {}));
10
- /**
11
- * Validate {@link CustomValidations.ENUM Enum}
12
- */
13
- const validateEnum = (value, enumValues) => (Array.isArray(enumValues)
14
- && enumValues.length > 0
15
- && enumValues.includes(value));
16
- /**
17
- * Validate {@link CustomValidations.RANGE Range}
18
- */
19
- const validateRange = (value, range) => {
20
- const [min, max] = range;
21
- if (min === undefined || max === undefined) {
22
- return false;
23
- }
24
- return value >= range.min && value <= range.max;
25
- };
26
- /**
27
- * Validators for custom fields
28
- */
29
- const validators = {
30
- [CustomValidations.SELECT]: validateEnum,
31
- [CustomValidations.RANGE]: validateRange,
32
- };
33
- exports.default = validators;
@@ -1,39 +0,0 @@
1
- /* eslint-disable no-shadow */
2
- import logger from '../logger';
3
- import { CustomFieldDefinitionType } from './type';
4
- import validators from './validators';
5
-
6
- export const mustHaveCustomValidation = {
7
- [CustomFieldDefinitionType.SELECT]: true,
8
- };
9
-
10
- /**
11
- * Validates the given validations object against the supported field types and their validators.
12
- * @return true if the validation is valid, false otherwise.
13
- */
14
- export const validateValidation = (valueType, validation) => {
15
- if (!validation) {
16
- if (mustHaveCustomValidation[valueType]) {
17
- logger.error(`No custom validation for custom field type ${valueType} found`);
18
- return false;
19
- }
20
- return true;
21
- }
22
- return true;
23
- };
24
-
25
- /**
26
- * Validates the given value against the custom validation rules for the specified field type.
27
- * If no custom validation rules are provided, it falls back to the default validation.
28
- * @returns true if the value is valid according to the validation rules, false otherwise.
29
- */
30
- const customValidation = (value, valueType, validation) => {
31
- const validator = validators?.[valueType];
32
- if (!validation || !validator) {
33
- return validateValidation(valueType, validation);
34
- }
35
- // Always allow null values
36
- return value === null || validator(value, validation);
37
- };
38
-
39
- export default customValidation;
@@ -1,34 +0,0 @@
1
- // eslint-disable-next-line no-shadow
2
- export enum CustomValidations {
3
- SELECT = 'select',
4
- RANGE = 'between'
5
- }
6
-
7
- type Validator = (value, validation) => boolean;
8
- /**
9
- * Validate {@link CustomValidations.ENUM Enum}
10
- */
11
- const validateEnum: Validator = (value, enumValues) => (Array.isArray(enumValues)
12
- && enumValues.length > 0
13
- && enumValues.includes(value)
14
- );
15
- /**
16
- * Validate {@link CustomValidations.RANGE Range}
17
- */
18
- const validateRange: Validator = (value, range) => {
19
- const [min, max] = range;
20
- if (min === undefined || max === undefined) {
21
- return false;
22
- }
23
- return value >= range.min && value <= range.max;
24
- };
25
-
26
- /**
27
- * Validators for custom fields
28
- */
29
- const validators: { [key: string]: Validator } = {
30
- [CustomValidations.SELECT]: validateEnum,
31
- [CustomValidations.RANGE]: validateRange,
32
- };
33
-
34
- export default validators;