@autofleet/sadot 0.5.3 → 0.5.4-beta.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.
Files changed (84) hide show
  1. package/package.json +1 -1
  2. package/src/repository/value.ts +1 -0
  3. package/dist/api/index.d.ts +0 -2
  4. package/dist/api/index.js +0 -11
  5. package/dist/api/v1/definition/index.d.ts +0 -2
  6. package/dist/api/v1/definition/index.js +0 -118
  7. package/dist/api/v1/definition/validations.d.ts +0 -2
  8. package/dist/api/v1/definition/validations.js +0 -36
  9. package/dist/api/v1/errors.d.ts +0 -2
  10. package/dist/api/v1/errors.js +0 -15
  11. package/dist/api/v1/index.d.ts +0 -2
  12. package/dist/api/v1/index.js +0 -10
  13. package/dist/errors/index.d.ts +0 -16
  14. package/dist/errors/index.js +0 -45
  15. package/dist/events/index.d.ts +0 -4
  16. package/dist/events/index.js +0 -47
  17. package/dist/hooks/create.d.ts +0 -9
  18. package/dist/hooks/create.js +0 -70
  19. package/dist/hooks/enrich.d.ts +0 -6
  20. package/dist/hooks/enrich.js +0 -113
  21. package/dist/hooks/find.d.ts +0 -1
  22. package/dist/hooks/find.js +0 -29
  23. package/dist/hooks/index.d.ts +0 -6
  24. package/dist/hooks/index.js +0 -18
  25. package/dist/hooks/update.d.ts +0 -9
  26. package/dist/hooks/update.js +0 -58
  27. package/dist/hooks/workaround.d.ts +0 -10
  28. package/dist/hooks/workaround.js +0 -37
  29. package/dist/index.d.ts +0 -12
  30. package/dist/index.js +0 -52
  31. package/dist/models/CustomFieldDefinition.d.ts +0 -23
  32. package/dist/models/CustomFieldDefinition.js +0 -165
  33. package/dist/models/CustomFieldValue.d.ts +0 -15
  34. package/dist/models/CustomFieldValue.js +0 -148
  35. package/dist/models/index.d.ts +0 -8
  36. package/dist/models/index.js +0 -87
  37. package/dist/models/tests/AssociatedTestModel.d.ts +0 -12
  38. package/dist/models/tests/AssociatedTestModel.js +0 -71
  39. package/dist/models/tests/TestModel.d.ts +0 -12
  40. package/dist/models/tests/TestModel.js +0 -69
  41. package/dist/repository/definition.d.ts +0 -17
  42. package/dist/repository/definition.js +0 -80
  43. package/dist/repository/value.d.ts +0 -24
  44. package/dist/repository/value.js +0 -107
  45. package/dist/scopes/filter.d.ts +0 -23
  46. package/dist/scopes/filter.js +0 -55
  47. package/dist/scopes/index.d.ts +0 -2
  48. package/dist/scopes/index.js +0 -6
  49. package/dist/tests/api/test-api.d.ts +0 -2
  50. package/dist/tests/api/test-api.js +0 -56
  51. package/dist/tests/helpers/database-config.d.ts +0 -15
  52. package/dist/tests/helpers/database-config.js +0 -16
  53. package/dist/tests/helpers/index.d.ts +0 -2
  54. package/dist/tests/helpers/index.js +0 -18
  55. package/dist/tests/mocks/definition.mock.d.ts +0 -37
  56. package/dist/tests/mocks/definition.mock.js +0 -64
  57. package/dist/tests/mocks/events.mock.d.ts +0 -3
  58. package/dist/tests/mocks/events.mock.js +0 -19
  59. package/dist/tests/mocks/testModel.d.ts +0 -12
  60. package/dist/tests/mocks/testModel.js +0 -35
  61. package/dist/types/definition/index.d.ts +0 -23
  62. package/dist/types/definition/index.js +0 -2
  63. package/dist/types/index.d.ts +0 -13
  64. package/dist/types/index.js +0 -2
  65. package/dist/types/value/index.d.ts +0 -15
  66. package/dist/types/value/index.js +0 -2
  67. package/dist/utils/constants/index.d.ts +0 -5
  68. package/dist/utils/constants/index.js +0 -8
  69. package/dist/utils/db/index.d.ts +0 -4
  70. package/dist/utils/db/index.js +0 -24
  71. package/dist/utils/init.d.ts +0 -4
  72. package/dist/utils/init.js +0 -100
  73. package/dist/utils/logger/index.d.ts +0 -2
  74. package/dist/utils/logger/index.js +0 -6
  75. package/dist/utils/validations/custom-fields.d.ts +0 -3
  76. package/dist/utils/validations/custom-fields.js +0 -10
  77. package/dist/utils/validations/custom.d.ts +0 -15
  78. package/dist/utils/validations/custom.js +0 -42
  79. package/dist/utils/validations/index.d.ts +0 -2
  80. package/dist/utils/validations/index.js +0 -20
  81. package/dist/utils/validations/type.d.ts +0 -18
  82. package/dist/utils/validations/type.js +0 -50
  83. package/dist/utils/validations/validators.d.ts +0 -12
  84. package/dist/utils/validations/validators.js +0 -33
@@ -1,64 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createDefinitions = exports.createDefinition = exports.rangeField = exports.enumField = exports.booleanField = exports.coolFieldDefinition3 = exports.coolFieldDefinition2 = exports.coolFieldDefinition = void 0;
4
- const uuid_1 = require("uuid");
5
- exports.coolFieldDefinition = {
6
- name: 'cool field',
7
- modelType: 'TestModel',
8
- fieldType: 'number',
9
- entityId: (0, uuid_1.v4)(),
10
- entityType: 'fleetId',
11
- };
12
- exports.coolFieldDefinition2 = {
13
- ...exports.coolFieldDefinition,
14
- name: 'cool field2',
15
- };
16
- exports.coolFieldDefinition3 = {
17
- ...exports.coolFieldDefinition,
18
- name: 'cool field3',
19
- };
20
- const booleanField = (modelType) => ({
21
- name: 'shapeless',
22
- modelType,
23
- fieldType: 'boolean',
24
- entityId: (0, uuid_1.v4)(),
25
- entityType: 'fleetId',
26
- });
27
- exports.booleanField = booleanField;
28
- const enumField = (modelType, options) => ({
29
- name: 'choices',
30
- modelType,
31
- fieldType: 'select',
32
- validation: options,
33
- entityId: (0, uuid_1.v4)(),
34
- entityType: 'fleetId',
35
- });
36
- exports.enumField = enumField;
37
- const rangeField = (modelType) => ({
38
- name: 'ranges',
39
- modelType,
40
- fieldType: 'number',
41
- validation: {
42
- between: [10, 12],
43
- },
44
- entityId: (0, uuid_1.v4)(),
45
- entityType: 'fleetId',
46
- });
47
- exports.rangeField = rangeField;
48
- // eslint-disable-next-line max-len
49
- const createDefinition = (defaults) => ({
50
- name: defaults?.name || `def_${(0, uuid_1.v4)()}`,
51
- modelType: defaults?.modelType || 'TestModel',
52
- fieldType: defaults?.fieldType || 'boolean',
53
- entityId: defaults?.entityId || (0, uuid_1.v4)(),
54
- entityType: defaults?.entityType || 'fleetId',
55
- });
56
- exports.createDefinition = createDefinition;
57
- const createDefinitions = (defaults, length = 1) => (Array(length).fill({}).map((_) => ({
58
- name: defaults?.name || `def_${(0, uuid_1.v4)()}`,
59
- modelType: defaults?.modelType || 'TestModel',
60
- fieldType: defaults?.fieldType || 'boolean',
61
- entityId: defaults?.entityId || (0, uuid_1.v4)(),
62
- entityType: defaults?.entityType || 'fleetId',
63
- })));
64
- exports.createDefinitions = createDefinitions;
@@ -1,3 +0,0 @@
1
- export declare const mockEvent: (events: any, eventName: any, numberOfEvents: any) => any[];
2
- export declare const mockDimCustomFieldDefinitionEvent: (events: any, numberOfEvents: any) => any[];
3
- export declare const mockDimCustomFieldValueEvent: (events: any, numberOfEvents: any) => any[];
@@ -1,19 +0,0 @@
1
- "use strict";
2
- /* eslint-disable no-param-reassign */
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.mockDimCustomFieldValueEvent = exports.mockDimCustomFieldDefinitionEvent = exports.mockEvent = void 0;
5
- const mockEvent = (events, eventName, numberOfEvents) => {
6
- events.sendObject = jest.fn();
7
- return [
8
- events.sendObject,
9
- () => {
10
- const matchingEvents = events.sendObject.mock.calls.filter((call) => call[0] === eventName);
11
- expect(matchingEvents.length).toEqual(numberOfEvents);
12
- },
13
- ];
14
- };
15
- exports.mockEvent = mockEvent;
16
- const mockDimCustomFieldDefinitionEvent = (events, numberOfEvents) => (0, exports.mockEvent)(events, 'dim_custom_field_definition', numberOfEvents);
17
- exports.mockDimCustomFieldDefinitionEvent = mockDimCustomFieldDefinitionEvent;
18
- const mockDimCustomFieldValueEvent = (events, numberOfEvents) => (0, exports.mockEvent)(events, 'dim_custom_field_value', numberOfEvents);
19
- exports.mockDimCustomFieldValueEvent = mockDimCustomFieldValueEvent;
@@ -1,12 +0,0 @@
1
- import { TestModel } from '../../models';
2
- export declare const createTestModel: (payload?: {}) => Promise<TestModel>;
3
- export declare const createTestModels: (fleetId: any, total: number) => Promise<TestModel[]>;
4
- export declare const upsertTestModel: (payload: any) => Promise<[TestModel, boolean]>;
5
- export declare const destroyTestModels: () => Promise<number>;
6
- export declare const getTestModel: (id: string, options?: {}) => Promise<TestModel | null>;
7
- export declare const getSomeTestModels: (options?: {
8
- limit: number;
9
- }) => Promise<TestModel[]>;
10
- export declare const updateTestModel: (payload: any, query: any) => Promise<[affectedCount: number, affectedRows: TestModel[]]>;
11
- export declare const createTestModelWithAssociation: () => Promise<TestModel>;
12
- export declare const getTestModelWithAssociation: (limit?: number) => Promise<TestModel[]>;
@@ -1,35 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getTestModelWithAssociation = exports.createTestModelWithAssociation = exports.updateTestModel = exports.getSomeTestModels = exports.getTestModel = exports.destroyTestModels = exports.upsertTestModel = exports.createTestModels = exports.createTestModel = void 0;
4
- const models_1 = require("../../models");
5
- const createTestModel = (payload = {}) => models_1.TestModel.create(payload);
6
- exports.createTestModel = createTestModel;
7
- const createTestModels = (fleetId, total) => {
8
- const models = [];
9
- for (let i = 0; i < total; i += 1) {
10
- models.push((0, exports.createTestModel)({ fleetId }));
11
- }
12
- return Promise.all(models);
13
- };
14
- exports.createTestModels = createTestModels;
15
- const upsertTestModel = (payload) => models_1.TestModel.upsert(payload);
16
- exports.upsertTestModel = upsertTestModel;
17
- const destroyTestModels = () => models_1.TestModel.destroy({ truncate: true });
18
- exports.destroyTestModels = destroyTestModels;
19
- const getTestModel = (id, options = {}) => models_1.TestModel.findByPk(id, options);
20
- exports.getTestModel = getTestModel;
21
- const getSomeTestModels = (options = { limit: 1 }) => models_1.TestModel.findAll(options);
22
- exports.getSomeTestModels = getSomeTestModels;
23
- const updateTestModel = (payload, query) => models_1.TestModel.update(payload, query);
24
- exports.updateTestModel = updateTestModel;
25
- // Associations
26
- const createTestModelWithAssociation = async () => {
27
- const model = await models_1.TestModel.create({});
28
- await models_1.AssociatedTestModel.create({ testModelId: model.id });
29
- return model;
30
- };
31
- exports.createTestModelWithAssociation = createTestModelWithAssociation;
32
- const getTestModelWithAssociation = (limit = 1) => models_1.TestModel.findAll({
33
- limit, include: [models_1.AssociatedTestModel],
34
- });
35
- exports.getTestModelWithAssociation = getTestModelWithAssociation;
@@ -1,23 +0,0 @@
1
- export interface CustomFieldDefinitionDTO {
2
- id: string;
3
- name: string;
4
- displayName?: string;
5
- validation?: any;
6
- fieldType: string;
7
- entityId: string;
8
- entityType: string;
9
- modelType: string;
10
- required?: boolean;
11
- description?: string;
12
- disabled?: boolean;
13
- createdAt?: Date;
14
- updatedAt?: Date;
15
- deletedAt?: Date;
16
- }
17
- export type CreateCustomFieldDefinition = Omit<CustomFieldDefinitionDTO, 'id'>;
18
- export type UpdateCustomFieldDefinition = Partial<CreateCustomFieldDefinition>;
19
- export type SerializedCustomFields = {
20
- [name: string]: CustomFieldDefinitionDTO & {
21
- value: any;
22
- };
23
- };
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,13 +0,0 @@
1
- export type ModelFetcher = (name: string) => any;
2
- export type ModelOptions = {
3
- name: string;
4
- scopeAttributes: string[];
5
- creationWebhookHandler?: (instance: any) => any;
6
- updateWebhookHandler?: (instance: any) => any;
7
- deletionWebhookHandler?: (instance: any) => any;
8
- };
9
- export type CustomFieldOptions = {
10
- models: ModelOptions[];
11
- databaseConfig: any;
12
- getUser: () => any;
13
- };
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,15 +0,0 @@
1
- export interface CustomFieldValueDTO {
2
- id?: string;
3
- modelId: string;
4
- customFieldDefinitionId: string;
5
- value: any;
6
- createdAt?: Date;
7
- updatedAt?: Date;
8
- deletedAt?: Date;
9
- }
10
- export type ValuesToUpdate = {
11
- [name: string]: any;
12
- };
13
- export type CreateCustomFieldValue = Omit<CustomFieldValueDTO, 'id'>;
14
- export type UpdateCustomFieldValue = ValuesToUpdate;
15
- export type BulkUpdateCustomFieldValue = Partial<CustomFieldValueDTO>[];
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,5 +0,0 @@
1
- declare const CUSTOM_FIELDS_FILTER_SCOPE: string;
2
- export declare const supportedEntities: string[];
3
- export {
4
- /** @deprecated Use the value from `@autofleet/common-types` instead */
5
- CUSTOM_FIELDS_FILTER_SCOPE, };
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CUSTOM_FIELDS_FILTER_SCOPE = exports.supportedEntities = void 0;
4
- const common_types_1 = require("@autofleet/common-types");
5
- const { CUSTOM_FIELDS_FILTER_SCOPE } = common_types_1.customFields;
6
- exports.CUSTOM_FIELDS_FILTER_SCOPE = CUSTOM_FIELDS_FILTER_SCOPE;
7
- // eslint-disable-next-line import/prefer-default-export
8
- exports.supportedEntities = ['businessModelId', 'fleetId', 'demandSourceId'];
@@ -1,4 +0,0 @@
1
- import { Sequelize } from 'sequelize-typescript';
2
- declare const _default: (databaseConfig: any) => Sequelize;
3
- export default _default;
4
- export declare const createSequelizeMeta: (sequelize: Sequelize) => Promise<object[]>;
@@ -1,24 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createSequelizeMeta = void 0;
4
- const sequelize_typescript_1 = require("sequelize-typescript");
5
- const sequelize_1 = require("sequelize");
6
- exports.default = (databaseConfig) => {
7
- const ENV_DEV = 'test';
8
- const env = process.env.NODE_ENV || ENV_DEV;
9
- const config = databaseConfig[env];
10
- let sequelize;
11
- if (config.use_env_variable) {
12
- sequelize = new sequelize_typescript_1.Sequelize(process.env[config.use_env_variable], config);
13
- }
14
- else {
15
- sequelize = new sequelize_typescript_1.Sequelize(config.database, config.username, config.password, config);
16
- }
17
- return sequelize;
18
- };
19
- const createSequelizeMeta = (sequelize) => sequelize.query(`
20
- CREATE TABLE IF NOT EXISTS "SequelizeMeta" (
21
- name character varying(255) PRIMARY KEY
22
- );
23
- `, { type: sequelize_1.QueryTypes.SELECT });
24
- exports.createSequelizeMeta = createSequelizeMeta;
@@ -1,4 +0,0 @@
1
- import type { ModelFetcher, ModelOptions } from '../types';
2
- export declare const addHooks: (models: ModelOptions[], getModel: ModelFetcher) => void;
3
- export declare const removeHooks: (models: ModelOptions[], getModel: ModelFetcher) => void;
4
- export declare const addScopes: (models: ModelOptions[], getModel: ModelFetcher) => void;
@@ -1,100 +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.addScopes = exports.removeHooks = exports.addHooks = void 0;
7
- const sequelize_1 = require("sequelize");
8
- const common_types_1 = require("@autofleet/common-types");
9
- const models_1 = require("../models");
10
- const hooks_1 = require("../hooks");
11
- const scopes_1 = require("../scopes");
12
- const logger_1 = __importDefault(require("./logger"));
13
- const { CUSTOM_FIELDS_FILTER_SCOPE } = common_types_1.customFields;
14
- const addHooks = (models, getModel) => {
15
- models.forEach(async ({ name, scopeAttributes }) => {
16
- try {
17
- const model = getModel(name);
18
- if (!model) {
19
- logger_1.default.warn('sadot - tried to addHooks to a model that does not exist yet', {
20
- name,
21
- scopeAttributes,
22
- });
23
- return;
24
- }
25
- model.rawAttributes.customFields = {
26
- type: sequelize_1.DataTypes.VIRTUAL,
27
- };
28
- model.refreshAttributes();
29
- // TODO: Uncomment after tests are passed
30
- // model.addHook('afterFind', workaround);
31
- model.addHook('beforeFind', 'sadot-beforeFind', (0, hooks_1.beforeFind)(scopeAttributes));
32
- model.addHook('beforeBulkCreate', 'sadot-beforeBulkCreate', hooks_1.beforeBulkCreate);
33
- model.addHook('beforeBulkUpdate', 'sadot-beforeBulkUpdate', hooks_1.beforeBulkUpdate);
34
- model.addHook('beforeCreate', 'sadot-beforeCreate', (0, hooks_1.beforeCreate)(scopeAttributes));
35
- model.addHook('beforeUpdate', 'sadot-beforeUpdate', (0, hooks_1.beforeUpdate)(scopeAttributes));
36
- model.addHook('afterFind', 'sadot-afterFind', (0, hooks_1.enrichResults)(name, scopeAttributes, 'afterFind'));
37
- model.addHook('afterUpdate', 'sadot-afterUpdate', (0, hooks_1.enrichResults)(name, scopeAttributes));
38
- model.addHook('afterCreate', 'sadot-afterCreate', (0, hooks_1.enrichResults)(name, scopeAttributes));
39
- }
40
- catch (e) {
41
- logger_1.default.error(`Could not add custom fields hook to model ${name}. `, e);
42
- }
43
- });
44
- };
45
- exports.addHooks = addHooks;
46
- const removeHooks = (models, getModel) => {
47
- models.forEach(async ({ name }) => {
48
- try {
49
- const model = getModel(name);
50
- if (!model)
51
- return;
52
- if (model.rawAttributes.customFields) {
53
- delete model.rawAttributes.customFields;
54
- model.refreshAttributes();
55
- }
56
- // model.removeHook('afterFind', 'sadot-workaround');
57
- model.removeHook('beforeFind', 'sadot-beforeFind');
58
- model.removeHook('beforeBulkCreate', 'sadot-beforeBulkCreate');
59
- model.removeHook('beforeBulkUpdate', 'sadot-beforeBulkUpdate');
60
- model.removeHook('beforeCreate', 'sadot-beforeCreate');
61
- model.removeHook('beforeUpdate', 'sadot-beforeUpdate');
62
- model.removeHook('afterFind', 'sadot-afterFind');
63
- model.removeHook('afterUpdate', 'sadot-afterUpdate');
64
- }
65
- catch (e) {
66
- logger_1.default.error(`Could not add custom fields hook to model ${name}. `, e);
67
- }
68
- });
69
- };
70
- exports.removeHooks = removeHooks;
71
- /**
72
- * Necessary associations for the {@link customFieldsFilterScope} scope
73
- */
74
- const addAssociations = (model, modelName) => {
75
- model.hasMany(models_1.CustomFieldValue, { foreignKey: 'modelId', as: 'customFieldValue' });
76
- // TBC: maybe can be removed
77
- models_1.CustomFieldValue.belongsTo(model, { foreignKey: 'modelId', as: modelName });
78
- };
79
- const addScopes = (models, getModel) => {
80
- models.forEach(async ({ name, scopeAttributes }) => {
81
- try {
82
- const model = getModel(name);
83
- if (!model) {
84
- logger_1.default.warn('sadot - tried to addScopes to a model that does not exist yet', {
85
- name,
86
- scopeAttributes,
87
- });
88
- return;
89
- }
90
- // Necessary associations for the filtering scope
91
- addAssociations(model, name);
92
- // Add filter scope
93
- model.addScope(CUSTOM_FIELDS_FILTER_SCOPE, (0, scopes_1.customFieldsFilterScope)(name));
94
- }
95
- catch (e) {
96
- logger_1.default.error(`Could not add custom fields scopes to model ${name}. `, e);
97
- }
98
- });
99
- };
100
- exports.addScopes = addScopes;
@@ -1,2 +0,0 @@
1
- declare const logger: any;
2
- export default logger;
@@ -1,6 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- // eslint-disable-next-line @typescript-eslint/no-var-requires
4
- const Logger = require('@autofleet/logger');
5
- const logger = Logger();
6
- exports.default = logger;
@@ -1,3 +0,0 @@
1
- import Joi from 'joi';
2
- declare const CustomFieldsSchema: Joi.ObjectSchema<any>;
3
- export { CustomFieldsSchema };
@@ -1,10 +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.CustomFieldsSchema = void 0;
7
- /* eslint-disable import/prefer-default-export */
8
- const joi_1 = __importDefault(require("joi"));
9
- const CustomFieldsSchema = joi_1.default.object().pattern(joi_1.default.string(), joi_1.default.any());
10
- exports.CustomFieldsSchema = CustomFieldsSchema;
@@ -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,2 +0,0 @@
1
- declare const validateValue: (value: any, valueType: any, customValidations: any) => boolean;
2
- export default validateValue;
@@ -1,20 +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
- const custom_1 = __importDefault(require("./custom"));
7
- const type_1 = __importDefault(require("./type"));
8
- const validateValue = (value, valueType, customValidations) => {
9
- if (!(0, type_1.default)(value, valueType)) {
10
- return false;
11
- }
12
- if (customValidations) {
13
- return (0, custom_1.default)(value, valueType, customValidations);
14
- }
15
- // if (validations.required && !value) {
16
- // return false;
17
- // }
18
- return true;
19
- };
20
- exports.default = validateValue;
@@ -1,18 +0,0 @@
1
- /**
2
- * Supported custom field types
3
- */
4
- export declare enum CustomFieldDefinitionType {
5
- NUMBER = "number",
6
- BOOLEAN = "boolean",
7
- DATE = "date",
8
- DATETIME = "datetime",
9
- TEXT = "text",
10
- IMAGE = "image",
11
- SELECT = "select"
12
- }
13
- /**
14
- * Validate that the given value is really of type "valueType"
15
- * TODO: verify that required field not set to null
16
- */
17
- declare const validateValueType: (value: unknown, valueType: CustomFieldDefinitionType) => boolean;
18
- export default validateValueType;
@@ -1,50 +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.CustomFieldDefinitionType = void 0;
7
- const joi_1 = __importDefault(require("joi"));
8
- /**
9
- * Supported custom field types
10
- */
11
- // eslint-disable-next-line no-shadow
12
- var CustomFieldDefinitionType;
13
- (function (CustomFieldDefinitionType) {
14
- CustomFieldDefinitionType["NUMBER"] = "number";
15
- CustomFieldDefinitionType["BOOLEAN"] = "boolean";
16
- CustomFieldDefinitionType["DATE"] = "date";
17
- CustomFieldDefinitionType["DATETIME"] = "datetime";
18
- CustomFieldDefinitionType["TEXT"] = "text";
19
- CustomFieldDefinitionType["IMAGE"] = "image";
20
- CustomFieldDefinitionType["SELECT"] = "select";
21
- })(CustomFieldDefinitionType = exports.CustomFieldDefinitionType || (exports.CustomFieldDefinitionType = {}));
22
- /**
23
- * Validate that the given value is really of type "valueType"
24
- * TODO: verify that required field not set to null
25
- */
26
- const validateValueType = (value, valueType) => {
27
- if (value === null) {
28
- // Null is always allowed
29
- return true;
30
- }
31
- switch (valueType) {
32
- case CustomFieldDefinitionType.TEXT:
33
- return typeof value === 'string';
34
- case CustomFieldDefinitionType.NUMBER:
35
- return typeof value === 'number';
36
- case CustomFieldDefinitionType.BOOLEAN:
37
- return typeof value === 'boolean';
38
- case CustomFieldDefinitionType.DATE:
39
- case CustomFieldDefinitionType.DATETIME:
40
- return !joi_1.default.date().validate(value).error;
41
- case CustomFieldDefinitionType.SELECT:
42
- return true; // custom validation
43
- case CustomFieldDefinitionType.IMAGE:
44
- return !joi_1.default.array().min(1).unique().items(joi_1.default.string().uri())
45
- .validate(value).error;
46
- default:
47
- return false;
48
- }
49
- };
50
- exports.default = validateValueType;
@@ -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;