@autofleet/sadot 1.1.3 → 1.1.5-beta
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/.nvmrc +1 -0
- package/dist/api/index.d.ts +3 -0
- package/dist/api/index.js +12 -2
- package/dist/api/v1/definition/index.d.ts +3 -0
- package/dist/api/v1/definition/index.js +116 -2
- package/dist/api/v1/definition/validations.d.ts +2 -0
- package/dist/api/v1/definition/validations.js +77 -2
- package/dist/api/v1/errors.d.ts +4 -0
- package/dist/api/v1/errors.js +12 -2
- package/dist/api/v1/index.d.ts +3 -0
- package/dist/api/v1/index.js +13 -2
- package/dist/api/v1/validator/index.d.ts +3 -0
- package/dist/api/v1/validator/index.js +143 -2
- package/dist/api/v1/validator/validations.d.ts +6 -23
- package/dist/api/v1/validator/validations.js +38 -2
- package/dist/errors/index.d.ts +24 -0
- package/dist/errors/index.js +66 -3
- package/dist/events/index.d.ts +5 -0
- package/dist/events/index.js +54 -2
- package/dist/hooks/create.d.ts +10 -0
- package/dist/hooks/create.js +95 -0
- package/dist/hooks/enrich.d.ts +25 -0
- package/dist/hooks/enrich.js +198 -2
- package/dist/hooks/find.d.ts +1 -0
- package/dist/hooks/find.js +29 -2
- package/dist/hooks/hooks.d.ts +17 -0
- package/dist/hooks/hooks.js +388 -2
- package/dist/hooks/index.d.ts +5 -0
- package/dist/hooks/index.js +17 -1
- package/dist/hooks/update.d.ts +10 -0
- package/dist/hooks/update.js +49 -0
- package/dist/hooks/utils/updateInstanceValues.d.ts +15 -0
- package/dist/hooks/utils/updateInstanceValues.js +50 -2
- package/dist/hooks/workaround.d.ts +10 -0
- package/dist/hooks/workaround.js +37 -0
- package/dist/index.d.ts +12 -22
- package/dist/index.js +67 -2
- package/dist/models/CustomFieldDefinition.d.ts +23 -29
- package/dist/models/CustomFieldDefinition.js +192 -2
- package/dist/models/CustomFieldEntries.d.ts +13 -14
- package/dist/models/CustomFieldEntries.js +123 -2
- package/dist/models/CustomFieldValue.d.ts +14 -20
- package/dist/models/CustomFieldValue.js +151 -2
- package/dist/models/CustomValidator.d.ts +15 -17
- package/dist/models/CustomValidator.js +98 -2
- package/dist/models/index.d.ts +18 -6
- package/dist/models/index.js +131 -2
- package/dist/models/tests/AssociatedTestModel.d.ts +12 -0
- package/dist/models/tests/AssociatedTestModel.js +71 -2
- package/dist/models/tests/TestModel.d.ts +12 -0
- package/dist/models/tests/TestModel.js +69 -2
- package/dist/models/tests/contextAwareModels/ContextAwareTestModel.d.ts +10 -0
- package/dist/models/tests/contextAwareModels/ContextAwareTestModel.js +53 -2
- package/dist/models/tests/contextAwareModels/ContextTestModel.d.ts +13 -0
- package/dist/models/tests/contextAwareModels/ContextTestModel.js +47 -2
- package/dist/repository/definition.d.ts +36 -0
- package/dist/repository/definition.js +121 -2
- package/dist/repository/entries.d.ts +13 -0
- package/dist/repository/entries.js +92 -2
- package/dist/repository/utils/formatValues.d.ts +3 -0
- package/dist/repository/utils/formatValues.js +16 -2
- package/dist/repository/validator.d.ts +27 -0
- package/dist/repository/validator.js +69 -2
- package/dist/repository/value.d.ts +28 -0
- package/dist/repository/value.js +124 -2
- package/dist/scopes/filter.d.ts +29 -22
- package/dist/scopes/filter.js +75 -2
- package/dist/scopes/helpers/filter.helpers.d.ts +40 -15
- package/dist/scopes/helpers/filter.helpers.js +183 -25
- package/dist/scopes/index.d.ts +2 -0
- package/dist/scopes/index.js +6 -1
- package/dist/tests/api/test-api.d.ts +2 -0
- package/dist/tests/api/test-api.js +38 -0
- package/dist/tests/functional/searching/index.d.ts +8 -0
- package/dist/tests/functional/searching/index.js +44 -0
- package/dist/tests/helpers/commonHooks.d.ts +6 -0
- package/dist/tests/helpers/commonHooks.js +62 -0
- package/dist/tests/helpers/database-config.d.ts +16 -0
- package/dist/tests/helpers/database-config.js +17 -0
- package/dist/tests/helpers/index.d.ts +7 -0
- package/dist/tests/helpers/index.js +33 -0
- package/dist/tests/mocks/definition.mock.d.ts +48 -0
- package/dist/tests/mocks/definition.mock.js +78 -0
- package/dist/tests/mocks/events.mock.d.ts +4 -0
- package/dist/tests/mocks/events.mock.js +21 -0
- package/dist/tests/mocks/testModel.d.ts +12 -0
- package/dist/tests/mocks/testModel.js +35 -0
- package/dist/types/definition/index.d.ts +25 -0
- package/dist/types/definition/index.js +2 -0
- package/dist/types/entries/index.d.ts +25 -0
- package/dist/types/entries/index.js +2 -0
- package/dist/types/index.d.ts +46 -45
- package/dist/types/index.js +2 -0
- package/dist/types/value/index.d.ts +15 -0
- package/dist/types/value/index.js +2 -0
- package/dist/utils/constants/index.d.ts +17 -20
- package/dist/utils/constants/index.js +22 -2
- package/dist/utils/db/index.d.ts +4 -0
- package/dist/utils/db/index.js +24 -2
- package/dist/utils/helpers/index.d.ts +23 -28
- package/dist/utils/helpers/index.js +40 -2
- package/dist/utils/init.d.ts +7 -0
- package/dist/utils/init.js +112 -2
- package/dist/utils/logger/index.d.ts +3 -0
- package/dist/utils/logger/index.js +42 -2
- package/dist/utils/scopeAttributes.d.ts +2 -0
- package/dist/utils/scopeAttributes.js +11 -2
- package/dist/utils/validations/index.d.ts +8 -0
- package/dist/utils/validations/index.js +41 -2
- package/dist/utils/validations/schema/custom-fields.d.ts +2 -6
- package/dist/utils/validations/schema/custom-fields.js +9 -2
- package/dist/utils/validations/schema/validator-schema.d.ts +9 -0
- package/dist/utils/validations/schema/validator-schema.js +95 -2
- package/dist/utils/validations/type.d.ts +15 -0
- package/dist/utils/validations/type.js +2 -0
- package/dist/utils/validations/validators/index.d.ts +14 -0
- package/dist/utils/validations/validators/index.js +40 -2
- package/dist/utils/validations/validators/select.validator.d.ts +5 -0
- package/dist/utils/validations/validators/select.validator.js +12 -2
- package/dist/utils/validations/validators/status.validator.d.ts +12 -0
- package/dist/utils/validations/validators/status.validator.js +15 -2
- package/package.json +39 -40
- package/src/api/index.ts +10 -0
- package/src/api/v1/definition/index.ts +104 -0
- package/src/api/v1/definition/validations.ts +75 -0
- package/src/api/v1/errors.ts +13 -0
- package/src/api/v1/index.ts +11 -0
- package/src/api/v1/validator/index.ts +141 -0
- package/src/api/v1/validator/validations.ts +38 -0
- package/src/errors/index.ts +70 -0
- package/src/events/index.ts +63 -0
- package/src/hooks/create.ts +81 -0
- package/src/hooks/enrich.ts +255 -0
- package/src/hooks/find.ts +27 -0
- package/src/hooks/hooks.ts +482 -0
- package/src/hooks/index.ts +20 -0
- package/src/hooks/update.ts +55 -0
- package/src/hooks/utils/updateInstanceValues.ts +63 -0
- package/src/hooks/workaround.ts +47 -0
- package/src/index.ts +52 -0
- package/src/models/CustomFieldDefinition.ts +162 -0
- package/src/models/CustomFieldEntries.ts +81 -0
- package/src/models/CustomFieldValue.ts +118 -0
- package/src/models/CustomValidator.ts +78 -0
- package/src/models/index.ts +165 -0
- package/src/models/tests/AssociatedTestModel.ts +57 -0
- package/src/models/tests/TestModel.ts +54 -0
- package/src/models/tests/contextAwareModels/ContextAwareTestModel.ts +43 -0
- package/src/models/tests/contextAwareModels/ContextTestModel.ts +38 -0
- package/src/repository/definition.ts +175 -0
- package/src/repository/entries.ts +88 -0
- package/src/repository/utils/formatValues.ts +14 -0
- package/src/repository/validator.ts +104 -0
- package/src/repository/value.ts +116 -0
- package/src/scopes/filter.ts +100 -0
- package/src/scopes/helpers/filter.helpers.ts +227 -0
- package/src/scopes/index.ts +6 -0
- package/src/tests/api/test-api.ts +40 -0
- package/src/tests/functional/searching/index.ts +39 -0
- package/src/tests/helpers/commonHooks.ts +43 -0
- package/src/tests/helpers/database-config.ts +15 -0
- package/src/tests/helpers/index.ts +35 -0
- package/src/tests/mocks/definition.mock.ts +84 -0
- package/src/tests/mocks/events.mock.ts +21 -0
- package/src/tests/mocks/testModel.ts +37 -0
- package/src/types/definition/index.ts +24 -0
- package/src/types/entries/index.ts +27 -0
- package/src/types/index.ts +52 -0
- package/src/types/value/index.ts +14 -0
- package/src/utils/constants/index.ts +25 -0
- package/src/utils/db/index.ts +21 -0
- package/src/utils/helpers/index.ts +66 -0
- package/src/utils/init.ts +120 -0
- package/src/utils/logger/index.ts +14 -0
- package/src/utils/scopeAttributes.ts +12 -0
- package/src/utils/validations/index.ts +46 -0
- package/src/utils/validations/schema/README.md +93 -0
- package/src/utils/validations/schema/custom-fields.ts +8 -0
- package/src/utils/validations/schema/validator-schema.ts +106 -0
- package/src/utils/validations/type.ts +20 -0
- package/src/utils/validations/validators/index.ts +38 -0
- package/src/utils/validations/validators/select.validator.ts +12 -0
- package/src/utils/validations/validators/status.validator.ts +22 -0
- package/tsconfig.build.json +7 -0
- package/tsconfig.json +16 -0
- package/dist/_virtual/_@oxc-project_runtime@0.101.0/helpers/decorate.cjs +0 -1
- package/dist/_virtual/_@oxc-project_runtime@0.101.0/helpers/decorate.js +0 -1
- package/dist/_virtual/_@oxc-project_runtime@0.101.0/helpers/decorateMetadata.cjs +0 -1
- package/dist/_virtual/_@oxc-project_runtime@0.101.0/helpers/decorateMetadata.js +0 -1
- package/dist/_virtual/rolldown_runtime.cjs +0 -1
- package/dist/api/index.cjs +0 -2
- package/dist/api/index.cjs.map +0 -1
- package/dist/api/index.js.map +0 -1
- package/dist/api/v1/definition/index.cjs +0 -2
- package/dist/api/v1/definition/index.cjs.map +0 -1
- package/dist/api/v1/definition/index.js.map +0 -1
- package/dist/api/v1/definition/validations.cjs +0 -2
- package/dist/api/v1/definition/validations.cjs.map +0 -1
- package/dist/api/v1/definition/validations.js.map +0 -1
- package/dist/api/v1/errors.cjs +0 -2
- package/dist/api/v1/errors.cjs.map +0 -1
- package/dist/api/v1/errors.js.map +0 -1
- package/dist/api/v1/index.cjs +0 -2
- package/dist/api/v1/index.cjs.map +0 -1
- package/dist/api/v1/index.js.map +0 -1
- package/dist/api/v1/validator/index.cjs +0 -2
- package/dist/api/v1/validator/index.cjs.map +0 -1
- package/dist/api/v1/validator/index.js.map +0 -1
- package/dist/api/v1/validator/validations.cjs +0 -2
- package/dist/api/v1/validator/validations.cjs.map +0 -1
- package/dist/api/v1/validator/validations.d.cts +0 -23
- package/dist/api/v1/validator/validations.js.map +0 -1
- package/dist/errors/index.cjs +0 -3
- package/dist/errors/index.cjs.map +0 -1
- package/dist/errors/index.js.map +0 -1
- package/dist/events/index.cjs +0 -2
- package/dist/events/index.cjs.map +0 -1
- package/dist/events/index.js.map +0 -1
- package/dist/hooks/enrich.cjs +0 -2
- package/dist/hooks/enrich.cjs.map +0 -1
- package/dist/hooks/enrich.js.map +0 -1
- package/dist/hooks/find.cjs +0 -2
- package/dist/hooks/find.cjs.map +0 -1
- package/dist/hooks/find.js.map +0 -1
- package/dist/hooks/hooks.cjs +0 -2
- package/dist/hooks/hooks.cjs.map +0 -1
- package/dist/hooks/hooks.js.map +0 -1
- package/dist/hooks/index.cjs +0 -1
- package/dist/hooks/utils/updateInstanceValues.cjs +0 -2
- package/dist/hooks/utils/updateInstanceValues.cjs.map +0 -1
- package/dist/hooks/utils/updateInstanceValues.js.map +0 -1
- package/dist/index.cjs +0 -2
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -23
- package/dist/index.js.map +0 -1
- package/dist/models/CustomFieldDefinition.cjs +0 -2
- package/dist/models/CustomFieldDefinition.cjs.map +0 -1
- package/dist/models/CustomFieldDefinition.d.cts +0 -31
- package/dist/models/CustomFieldDefinition.js.map +0 -1
- package/dist/models/CustomFieldEntries.cjs +0 -2
- package/dist/models/CustomFieldEntries.cjs.map +0 -1
- package/dist/models/CustomFieldEntries.d.cts +0 -16
- package/dist/models/CustomFieldEntries.js.map +0 -1
- package/dist/models/CustomFieldValue.cjs +0 -2
- package/dist/models/CustomFieldValue.cjs.map +0 -1
- package/dist/models/CustomFieldValue.d.cts +0 -22
- package/dist/models/CustomFieldValue.js.map +0 -1
- package/dist/models/CustomValidator.cjs +0 -2
- package/dist/models/CustomValidator.cjs.map +0 -1
- package/dist/models/CustomValidator.d.cts +0 -19
- package/dist/models/CustomValidator.js.map +0 -1
- package/dist/models/index.cjs +0 -2
- package/dist/models/index.cjs.map +0 -1
- package/dist/models/index.d.cts +0 -6
- package/dist/models/index.js.map +0 -1
- package/dist/models/tests/AssociatedTestModel.cjs +0 -2
- package/dist/models/tests/AssociatedTestModel.cjs.map +0 -1
- package/dist/models/tests/AssociatedTestModel.js.map +0 -1
- package/dist/models/tests/TestModel.cjs +0 -2
- package/dist/models/tests/TestModel.cjs.map +0 -1
- package/dist/models/tests/TestModel.js.map +0 -1
- package/dist/models/tests/contextAwareModels/ContextAwareTestModel.cjs +0 -2
- package/dist/models/tests/contextAwareModels/ContextAwareTestModel.cjs.map +0 -1
- package/dist/models/tests/contextAwareModels/ContextAwareTestModel.js.map +0 -1
- package/dist/models/tests/contextAwareModels/ContextTestModel.cjs +0 -2
- package/dist/models/tests/contextAwareModels/ContextTestModel.cjs.map +0 -1
- package/dist/models/tests/contextAwareModels/ContextTestModel.js.map +0 -1
- package/dist/repository/definition.cjs +0 -2
- package/dist/repository/definition.cjs.map +0 -1
- package/dist/repository/definition.js.map +0 -1
- package/dist/repository/entries.cjs +0 -2
- package/dist/repository/entries.cjs.map +0 -1
- package/dist/repository/entries.js.map +0 -1
- package/dist/repository/utils/formatValues.cjs +0 -2
- package/dist/repository/utils/formatValues.cjs.map +0 -1
- package/dist/repository/utils/formatValues.js.map +0 -1
- package/dist/repository/validator.cjs +0 -2
- package/dist/repository/validator.cjs.map +0 -1
- package/dist/repository/validator.js.map +0 -1
- package/dist/repository/value.cjs +0 -2
- package/dist/repository/value.cjs.map +0 -1
- package/dist/repository/value.js.map +0 -1
- package/dist/scopes/filter.cjs +0 -2
- package/dist/scopes/filter.cjs.map +0 -1
- package/dist/scopes/filter.d.cts +0 -23
- package/dist/scopes/filter.js.map +0 -1
- package/dist/scopes/helpers/filter.helpers.cjs +0 -46
- package/dist/scopes/helpers/filter.helpers.cjs.map +0 -1
- package/dist/scopes/helpers/filter.helpers.d.cts +0 -17
- package/dist/scopes/helpers/filter.helpers.js.map +0 -1
- package/dist/scopes/index.cjs +0 -1
- package/dist/types/index.d.cts +0 -45
- package/dist/utils/constants/index.cjs +0 -2
- package/dist/utils/constants/index.cjs.map +0 -1
- package/dist/utils/constants/index.d.cts +0 -22
- package/dist/utils/constants/index.js.map +0 -1
- package/dist/utils/db/index.cjs +0 -2
- package/dist/utils/db/index.cjs.map +0 -1
- package/dist/utils/db/index.js.map +0 -1
- package/dist/utils/helpers/index.cjs +0 -2
- package/dist/utils/helpers/index.cjs.map +0 -1
- package/dist/utils/helpers/index.d.cts +0 -31
- package/dist/utils/helpers/index.js.map +0 -1
- package/dist/utils/init.cjs +0 -2
- package/dist/utils/init.cjs.map +0 -1
- package/dist/utils/init.js.map +0 -1
- package/dist/utils/logger/index.cjs +0 -2
- package/dist/utils/logger/index.cjs.map +0 -1
- package/dist/utils/logger/index.js.map +0 -1
- package/dist/utils/scopeAttributes.cjs +0 -2
- package/dist/utils/scopeAttributes.cjs.map +0 -1
- package/dist/utils/scopeAttributes.js.map +0 -1
- package/dist/utils/validations/index.cjs +0 -2
- package/dist/utils/validations/index.cjs.map +0 -1
- package/dist/utils/validations/index.js.map +0 -1
- package/dist/utils/validations/schema/custom-fields.cjs +0 -2
- package/dist/utils/validations/schema/custom-fields.cjs.map +0 -1
- package/dist/utils/validations/schema/custom-fields.d.cts +0 -7
- package/dist/utils/validations/schema/custom-fields.js.map +0 -1
- package/dist/utils/validations/schema/validator-schema.cjs +0 -2
- package/dist/utils/validations/schema/validator-schema.cjs.map +0 -1
- package/dist/utils/validations/schema/validator-schema.js.map +0 -1
- package/dist/utils/validations/validators/index.cjs +0 -2
- package/dist/utils/validations/validators/index.cjs.map +0 -1
- package/dist/utils/validations/validators/index.js.map +0 -1
- package/dist/utils/validations/validators/select.validator.cjs +0 -2
- package/dist/utils/validations/validators/select.validator.cjs.map +0 -1
- package/dist/utils/validations/validators/select.validator.js.map +0 -1
- package/dist/utils/validations/validators/status.validator.cjs +0 -2
- package/dist/utils/validations/validators/status.validator.cjs.map +0 -1
- package/dist/utils/validations/validators/status.validator.js.map +0 -1
package/dist/utils/db/index.js
CHANGED
|
@@ -1,2 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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,31 +1,26 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
//#region src/utils/helpers/index.d.ts
|
|
6
|
-
|
|
1
|
+
import { type WhereOptions, type BindOrReplacements } from 'sequelize';
|
|
2
|
+
import { type ModelStatic } from 'sequelize-typescript';
|
|
3
|
+
import { CustomFieldDefinitionType } from '../constants';
|
|
7
4
|
/**
|
|
8
|
-
* Builds a WHERE clause and replacements for free-text search by custom fields.
|
|
9
|
-
*
|
|
10
|
-
* This function constructs a WHERE clause and replacement bindings that allow searching
|
|
11
|
-
* for a given term within custom fields associated with a specific model type and entity ID.
|
|
12
|
-
* The WHERE clause and replacements are designed to be added to the main query.
|
|
13
|
-
*
|
|
14
|
-
* @param {string} searchTerm - The term to search for within custom fields.
|
|
15
|
-
* @param {ModelStatic} model - The Sequelize model representing the entity type to search for.
|
|
16
|
-
* @param {string} entityId - The entity ID to filter the custom fields by.
|
|
17
|
-
* @param {CustomFieldDefinitionType[]} excludedCustomFieldsTypes - An array of custom field types
|
|
18
|
-
* to exclude from the search
|
|
19
|
-
*
|
|
20
|
-
* @returns {CustomFieldsSearchPayload} - An object containing the WHERE clause and replacements
|
|
21
|
-
* for Sequelize.
|
|
22
|
-
*/
|
|
5
|
+
* Builds a WHERE clause and replacements for free-text search by custom fields.
|
|
6
|
+
*
|
|
7
|
+
* This function constructs a WHERE clause and replacement bindings that allow searching
|
|
8
|
+
* for a given term within custom fields associated with a specific model type and entity ID.
|
|
9
|
+
* The WHERE clause and replacements are designed to be added to the main query.
|
|
10
|
+
*
|
|
11
|
+
* @param {string} searchTerm - The term to search for within custom fields.
|
|
12
|
+
* @param {ModelStatic} model - The Sequelize model representing the entity type to search for.
|
|
13
|
+
* @param {string} entityId - The entity ID to filter the custom fields by.
|
|
14
|
+
* @param {CustomFieldDefinitionType[]} excludedCustomFieldsTypes - An array of custom field types
|
|
15
|
+
* to exclude from the search
|
|
16
|
+
*
|
|
17
|
+
* @returns {CustomFieldsSearchPayload} - An object containing the WHERE clause and replacements
|
|
18
|
+
* for Sequelize.
|
|
19
|
+
*/
|
|
23
20
|
interface CustomFieldsSearchPayload {
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
where: WhereOptions;
|
|
22
|
+
replacements: BindOrReplacements;
|
|
26
23
|
}
|
|
27
|
-
declare const generateRandomString: (length?: number) => string;
|
|
28
|
-
declare const generateCustomFieldSearchQueryPayload: (searchTerm: string, model: ModelStatic, entityId: string, customFieldsTypesToExclude?: CustomFieldDefinitionType[]) => CustomFieldsSearchPayload;
|
|
29
|
-
|
|
30
|
-
export { generateCustomFieldSearchQueryPayload, generateRandomString };
|
|
31
|
-
//# sourceMappingURL=index.d.ts.map
|
|
24
|
+
export declare const generateRandomString: (length?: number) => string;
|
|
25
|
+
export declare const generateCustomFieldSearchQueryPayload: (searchTerm: string, model: ModelStatic, entityId: string, customFieldsTypesToExclude?: CustomFieldDefinitionType[]) => CustomFieldsSearchPayload;
|
|
26
|
+
export {};
|
|
@@ -1,2 +1,40 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateCustomFieldSearchQueryPayload = exports.generateRandomString = void 0;
|
|
4
|
+
/* eslint-disable import/prefer-default-export */
|
|
5
|
+
const sequelize_1 = require("sequelize");
|
|
6
|
+
const sequelize_typescript_1 = require("sequelize-typescript");
|
|
7
|
+
const node_crypto_1 = require("node:crypto");
|
|
8
|
+
const constants_1 = require("../constants");
|
|
9
|
+
const generateRandomString = (length = 5) => {
|
|
10
|
+
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
|
|
11
|
+
return Array.from({ length }, () => characters.charAt((0, node_crypto_1.randomInt)(characters.length))).join('');
|
|
12
|
+
};
|
|
13
|
+
exports.generateRandomString = generateRandomString;
|
|
14
|
+
const generateCustomFieldSearchQueryPayload = (searchTerm, model, entityId, customFieldsTypesToExclude = [
|
|
15
|
+
constants_1.CustomFieldDefinitionType.DATETIME,
|
|
16
|
+
constants_1.CustomFieldDefinitionType.DATE,
|
|
17
|
+
]) => {
|
|
18
|
+
const excludedTypesString = customFieldsTypesToExclude.map((type) => `'${type}'`).join(',');
|
|
19
|
+
const subQuery = 'EXISTS ('
|
|
20
|
+
+ ' SELECT 1'
|
|
21
|
+
+ ' FROM "custom_field_values" AS "cv"'
|
|
22
|
+
+ ' INNER JOIN custom_field_definitions AS cd '
|
|
23
|
+
+ ` ON cd.entity_id = '${entityId}'`
|
|
24
|
+
+ ' AND cv.custom_field_definition_id = cd.id'
|
|
25
|
+
+ ` AND cd.model_type = '${model.name}'`
|
|
26
|
+
+ ` ${excludedTypesString ? `AND cd.field_type NOT IN (${excludedTypesString})` : ''}`
|
|
27
|
+
+ ' WHERE'
|
|
28
|
+
+ ' "cv"."deleted_at" IS NULL'
|
|
29
|
+
+ ` AND "cv"."model_id" = "${model.name}"."id"`
|
|
30
|
+
+ ' AND CAST("cv"."value" AS TEXT) ILIKE :searchTerm)';
|
|
31
|
+
return {
|
|
32
|
+
where: {
|
|
33
|
+
[sequelize_1.Op.or]: [
|
|
34
|
+
sequelize_typescript_1.Sequelize.where(sequelize_typescript_1.Sequelize.literal(subQuery), true),
|
|
35
|
+
],
|
|
36
|
+
},
|
|
37
|
+
replacements: { searchTerm: `%${searchTerm}%` },
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
exports.generateCustomFieldSearchQueryPayload = generateCustomFieldSearchQueryPayload;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { CustomFieldOptions, ModelFetcher, Models } from '../types';
|
|
2
|
+
export declare const addHooks: (models: Models[], getModel: ModelFetcher, sadotOptions?: {
|
|
3
|
+
useCustomFieldsEntries: boolean;
|
|
4
|
+
}) => void;
|
|
5
|
+
export declare const removeHooks: (models: Models[], getModel: ModelFetcher) => void;
|
|
6
|
+
export declare const addScopes: (models: Models[], getModel: ModelFetcher, options?: Pick<CustomFieldOptions, 'useCustomFieldsEntries'>) => void;
|
|
7
|
+
export declare const applyCustomAssociation: (models: Models[]) => void;
|
package/dist/utils/init.js
CHANGED
|
@@ -1,2 +1,112 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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.applyCustomAssociation = 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 custom_fields_1 = require("@autofleet/common-types/lib/custom-fields");
|
|
10
|
+
const models_1 = require("../models");
|
|
11
|
+
const hooks_1 = require("../hooks");
|
|
12
|
+
const scopes_1 = require("../scopes");
|
|
13
|
+
const logger_1 = __importDefault(require("./logger"));
|
|
14
|
+
const filter_1 = require("../scopes/filter");
|
|
15
|
+
const { CUSTOM_FIELDS_FILTER_SCOPE } = common_types_1.customFields;
|
|
16
|
+
const addHooks = (models, getModel, sadotOptions = { useCustomFieldsEntries: false }) => {
|
|
17
|
+
models.forEach(async ({ name, scopeAttributes, modelOptions, }) => {
|
|
18
|
+
try {
|
|
19
|
+
const model = getModel(name);
|
|
20
|
+
if (!model) {
|
|
21
|
+
logger_1.default.warn('sadot - tried to addHooks to a model that does not exist yet', {
|
|
22
|
+
name,
|
|
23
|
+
scopeAttributes,
|
|
24
|
+
});
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
model.rawAttributes.customFields = {
|
|
28
|
+
type: sequelize_1.DataTypes.VIRTUAL,
|
|
29
|
+
};
|
|
30
|
+
model.refreshAttributes();
|
|
31
|
+
// TODO: Uncomment after tests are passed
|
|
32
|
+
// model.addHook('afterFind', workaround);
|
|
33
|
+
model.addHook('beforeFind', 'sadot-beforeFind', (0, hooks_1.beforeFind)(scopeAttributes));
|
|
34
|
+
model.addHook('beforeBulkCreate', 'sadot-beforeBulkCreate', hooks_1.beforeBulkCreate);
|
|
35
|
+
model.addHook('beforeBulkUpdate', 'sadot-beforeBulkUpdate', hooks_1.beforeBulkUpdate);
|
|
36
|
+
model.addHook('beforeCreate', 'sadot-beforeCreate', (0, hooks_1.beforeCreate)(scopeAttributes, modelOptions, sadotOptions));
|
|
37
|
+
model.addHook('beforeUpdate', 'sadot-beforeUpdate', (0, hooks_1.beforeUpdate)(scopeAttributes, modelOptions, sadotOptions));
|
|
38
|
+
model.addHook('afterFind', 'sadot-afterFind', (0, hooks_1.enrichResults)(name, scopeAttributes, 'afterFind', modelOptions, sadotOptions));
|
|
39
|
+
model.addHook('afterUpdate', 'sadot-afterUpdate', (0, hooks_1.enrichResults)(name, scopeAttributes, null, modelOptions, sadotOptions));
|
|
40
|
+
model.addHook('afterCreate', 'sadot-afterCreate', (0, hooks_1.enrichResults)(name, scopeAttributes, null, modelOptions, sadotOptions));
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
logger_1.default.error(`Could not add custom fields hook to model ${name}. `, e);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
exports.addHooks = addHooks;
|
|
48
|
+
const removeHooks = (models, getModel) => {
|
|
49
|
+
models.forEach(async ({ name }) => {
|
|
50
|
+
try {
|
|
51
|
+
const model = getModel(name);
|
|
52
|
+
if (!model)
|
|
53
|
+
return;
|
|
54
|
+
if (model.rawAttributes.customFields) {
|
|
55
|
+
delete model.rawAttributes.customFields;
|
|
56
|
+
model.refreshAttributes();
|
|
57
|
+
}
|
|
58
|
+
// model.removeHook('afterFind', 'sadot-workaround');
|
|
59
|
+
model.removeHook('beforeFind', 'sadot-beforeFind');
|
|
60
|
+
model.removeHook('beforeBulkCreate', 'sadot-beforeBulkCreate');
|
|
61
|
+
model.removeHook('beforeBulkUpdate', 'sadot-beforeBulkUpdate');
|
|
62
|
+
model.removeHook('beforeCreate', 'sadot-beforeCreate');
|
|
63
|
+
model.removeHook('beforeUpdate', 'sadot-beforeUpdate');
|
|
64
|
+
model.removeHook('afterFind', 'sadot-afterFind');
|
|
65
|
+
model.removeHook('afterUpdate', 'sadot-afterUpdate');
|
|
66
|
+
}
|
|
67
|
+
catch (e) {
|
|
68
|
+
logger_1.default.error(`Could not add custom fields hook to model ${name}. `, e);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
exports.removeHooks = removeHooks;
|
|
73
|
+
/**
|
|
74
|
+
* Necessary associations for the {@link customFieldsFilterScope} scope
|
|
75
|
+
*/
|
|
76
|
+
const addAssociations = (model, modelName, options) => {
|
|
77
|
+
if (options?.useCustomFieldsEntries)
|
|
78
|
+
return;
|
|
79
|
+
model.hasMany(models_1.CustomFieldValue, { foreignKey: 'modelId', as: 'customFieldValue' });
|
|
80
|
+
// TBC: maybe can be removed
|
|
81
|
+
models_1.CustomFieldValue.belongsTo(model, { foreignKey: 'modelId', as: modelName });
|
|
82
|
+
};
|
|
83
|
+
const addScopes = (models, getModel, options = { useCustomFieldsEntries: false }) => {
|
|
84
|
+
models.forEach(async ({ name, scopeAttributes }) => {
|
|
85
|
+
try {
|
|
86
|
+
const model = getModel(name);
|
|
87
|
+
if (!model) {
|
|
88
|
+
logger_1.default.warn('sadot - tried to addScopes to a model that does not exist yet', {
|
|
89
|
+
name,
|
|
90
|
+
scopeAttributes,
|
|
91
|
+
});
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
// Necessary associations for the filtering scope
|
|
95
|
+
addAssociations(model, name, options);
|
|
96
|
+
// Add filter scope
|
|
97
|
+
model.addScope(CUSTOM_FIELDS_FILTER_SCOPE, (0, scopes_1.customFieldsFilterScope)(name, options));
|
|
98
|
+
model.addScope(custom_fields_1.CUSTOM_FIELDS_SORT_SCOPE, (0, filter_1.customFieldsSortScope)(name, options));
|
|
99
|
+
}
|
|
100
|
+
catch (e) {
|
|
101
|
+
logger_1.default.error(`Could not add custom fields scopes to model ${name}. `, e);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
exports.addScopes = addScopes;
|
|
106
|
+
const applyCustomAssociation = (models) => {
|
|
107
|
+
models.forEach(({ modelOptions }) => {
|
|
108
|
+
modelOptions?.customAssociation?.(models_1.CustomFieldDefinition);
|
|
109
|
+
modelOptions?.customAssociation?.(models_1.CustomValidator);
|
|
110
|
+
});
|
|
111
|
+
};
|
|
112
|
+
exports.applyCustomAssociation = applyCustomAssociation;
|
|
@@ -1,2 +1,42 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.tryAddingTraceIdMiddleware = void 0;
|
|
30
|
+
const logger_1 = __importDefault(require("@autofleet/logger"));
|
|
31
|
+
const logger = (0, logger_1.default)();
|
|
32
|
+
async function tryAddingTraceIdMiddleware() {
|
|
33
|
+
try {
|
|
34
|
+
const { outbreak } = await Promise.resolve().then(() => __importStar(require('@autofleet/zehut')));
|
|
35
|
+
logger.addContextMiddleware(() => ({ traceId: outbreak.getCurrentContextTraceId() }));
|
|
36
|
+
}
|
|
37
|
+
catch (err) {
|
|
38
|
+
logger.error('Failed to add traceId middleware', { err });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.tryAddingTraceIdMiddleware = tryAddingTraceIdMiddleware;
|
|
42
|
+
exports.default = logger;
|
|
@@ -1,2 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const mapAttributeToInstance = (scopeAttributes, instance) => scopeAttributes.map((attr) => instance[attr]);
|
|
4
|
+
const applyScopeToInstance = (instance, scopeAttributes) => {
|
|
5
|
+
const uniqueAttributes = Array.from(new Set(scopeAttributes));
|
|
6
|
+
if (Array.isArray(instance)) {
|
|
7
|
+
return instance.flatMap((ins) => mapAttributeToInstance(uniqueAttributes, ins));
|
|
8
|
+
}
|
|
9
|
+
return mapAttributeToInstance(uniqueAttributes, instance);
|
|
10
|
+
};
|
|
11
|
+
exports.default = applyScopeToInstance;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { CustomFieldDefinition } from '../../models';
|
|
2
|
+
import type { CustomFieldEntriesDTO } from '../../types/entries';
|
|
3
|
+
import type { CustomFieldDefinitionType } from '../constants';
|
|
4
|
+
export declare const validateFieldType: (type: CustomFieldDefinitionType) => boolean;
|
|
5
|
+
export declare const validateValue: (value: unknown, valueType: CustomFieldDefinitionType, validation?: unknown) => import("joi").ValidationResult;
|
|
6
|
+
export declare const validateInstanceCustomFieldEntries: (instance: CustomFieldEntriesDTO, definitionsByName: {
|
|
7
|
+
[defName: string]: CustomFieldDefinition;
|
|
8
|
+
}) => void;
|
|
@@ -1,2 +1,41 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateInstanceCustomFieldEntries = exports.validateValue = exports.validateFieldType = void 0;
|
|
4
|
+
const errors_1 = require("../../errors");
|
|
5
|
+
const validators_1 = require("./validators");
|
|
6
|
+
const validateFieldType = (type) => Object.keys(validators_1.validators).includes(type);
|
|
7
|
+
exports.validateFieldType = validateFieldType;
|
|
8
|
+
const validateValue = (value, valueType, validation) => {
|
|
9
|
+
const validator = validators_1.validators[valueType];
|
|
10
|
+
return validator(value, validation);
|
|
11
|
+
/** TODO: Add validation for required fields
|
|
12
|
+
* @example
|
|
13
|
+
* if (validations.required && !value) {
|
|
14
|
+
* return false;
|
|
15
|
+
* }
|
|
16
|
+
*/
|
|
17
|
+
};
|
|
18
|
+
exports.validateValue = validateValue;
|
|
19
|
+
const validateInstanceCustomFieldEntries = (instance, definitionsByName) => {
|
|
20
|
+
const validationErrors = Object.entries(instance.customFields)
|
|
21
|
+
.map(([customFieldName, value]) => {
|
|
22
|
+
// Allow NULL values, just like we do in custom_field_values.
|
|
23
|
+
if (value === null)
|
|
24
|
+
return null;
|
|
25
|
+
const { validation, fieldType } = definitionsByName[customFieldName];
|
|
26
|
+
const result = (0, exports.validateValue)(value, fieldType, validation);
|
|
27
|
+
if (result?.error) {
|
|
28
|
+
return {
|
|
29
|
+
joiValidationError: result.error,
|
|
30
|
+
fieldDefinitionName: customFieldName,
|
|
31
|
+
value,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
return null;
|
|
35
|
+
})
|
|
36
|
+
.filter((result) => !!result);
|
|
37
|
+
if (validationErrors?.length) {
|
|
38
|
+
throw new errors_1.InvalidEntriesError(instance.modelId, validationErrors);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
exports.validateInstanceCustomFieldEntries = validateInstanceCustomFieldEntries;
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
import Joi from
|
|
2
|
-
|
|
3
|
-
//#region src/utils/validations/schema/custom-fields.d.ts
|
|
4
|
-
declare const CustomFieldsSchema: Joi.ObjectSchema<Record<string, any>>;
|
|
5
|
-
//#endregion
|
|
1
|
+
import Joi from 'joi';
|
|
2
|
+
declare const CustomFieldsSchema: Joi.ObjectSchema<any>;
|
|
6
3
|
export { CustomFieldsSchema };
|
|
7
|
-
//# sourceMappingURL=custom-fields.d.ts.map
|
|
@@ -1,2 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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
|
+
const joi_1 = __importDefault(require("joi"));
|
|
8
|
+
const CustomFieldsSchema = joi_1.default.object().pattern(joi_1.default.string(), joi_1.default.any());
|
|
9
|
+
exports.CustomFieldsSchema = CustomFieldsSchema;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates that a given schema is a valid Ajv schema
|
|
3
|
+
* This function is used to validate schemas passed to custom validators
|
|
4
|
+
*
|
|
5
|
+
* @param schema The schema to validate
|
|
6
|
+
* @returns true if valid, throws an error if invalid
|
|
7
|
+
*/
|
|
8
|
+
export declare const validateValidatorSchema: (schema: Record<string, unknown>) => boolean;
|
|
9
|
+
export default validateValidatorSchema;
|
|
@@ -1,2 +1,95 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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.validateValidatorSchema = void 0;
|
|
7
|
+
const ajv_1 = __importDefault(require("ajv"));
|
|
8
|
+
const ajv_formats_1 = __importDefault(require("ajv-formats"));
|
|
9
|
+
const errors_1 = require("@autofleet/errors");
|
|
10
|
+
const logger_1 = __importDefault(require("../../logger"));
|
|
11
|
+
// Instantiate Ajv for meta-validation
|
|
12
|
+
const metaValidator = new ajv_1.default({
|
|
13
|
+
allErrors: true,
|
|
14
|
+
strict: false,
|
|
15
|
+
strictTypes: false,
|
|
16
|
+
$data: true, // Enable $data references
|
|
17
|
+
});
|
|
18
|
+
(0, ajv_formats_1.default)(metaValidator);
|
|
19
|
+
/**
|
|
20
|
+
* Schema for validating JSON Schema objects in custom validators
|
|
21
|
+
* This is a meta-schema to ensure that custom validator schemas are valid Ajv schemas
|
|
22
|
+
*/
|
|
23
|
+
const validatorMetaSchema = {
|
|
24
|
+
type: 'object',
|
|
25
|
+
properties: {
|
|
26
|
+
type: { type: 'string', enum: ['object'] },
|
|
27
|
+
properties: {
|
|
28
|
+
type: 'object',
|
|
29
|
+
properties: {
|
|
30
|
+
before: {
|
|
31
|
+
type: 'object',
|
|
32
|
+
properties: {
|
|
33
|
+
type: { type: 'string', enum: ['object'] },
|
|
34
|
+
properties: { type: 'object' },
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
after: {
|
|
38
|
+
type: 'object',
|
|
39
|
+
properties: {
|
|
40
|
+
type: { type: 'string', enum: ['object'] },
|
|
41
|
+
properties: { type: 'object' },
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
required: {
|
|
47
|
+
type: 'array',
|
|
48
|
+
items: { type: 'string' },
|
|
49
|
+
},
|
|
50
|
+
if: { type: 'object' },
|
|
51
|
+
then: { type: 'object' },
|
|
52
|
+
else: { type: 'object' },
|
|
53
|
+
},
|
|
54
|
+
required: ['type', 'properties'],
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Validates that a given schema is a valid Ajv schema
|
|
58
|
+
* This function is used to validate schemas passed to custom validators
|
|
59
|
+
*
|
|
60
|
+
* @param schema The schema to validate
|
|
61
|
+
* @returns true if valid, throws an error if invalid
|
|
62
|
+
*/
|
|
63
|
+
const validateValidatorSchema = (schema) => {
|
|
64
|
+
try {
|
|
65
|
+
// First validate the schema structure
|
|
66
|
+
const validateMetaSchema = metaValidator.compile(validatorMetaSchema);
|
|
67
|
+
const isValidStructure = validateMetaSchema(schema);
|
|
68
|
+
if (!isValidStructure) {
|
|
69
|
+
const errorDetails = validateMetaSchema.errors?.map((err) => `${err.instancePath || ''} ${err.message || 'Invalid schema structure'}`).join(', ');
|
|
70
|
+
logger_1.default.error('Invalid validator schema structure', {
|
|
71
|
+
errors: validateMetaSchema.errors,
|
|
72
|
+
schema,
|
|
73
|
+
});
|
|
74
|
+
throw new errors_1.BadRequest([new Error(`Invalid validator schema structure: ${errorDetails}`)], ['Invalid validator schema structure']);
|
|
75
|
+
}
|
|
76
|
+
// Then try to compile the schema with Ajv to verify it's a valid JSON Schema
|
|
77
|
+
try {
|
|
78
|
+
metaValidator.compile(schema);
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
catch (compileError) {
|
|
82
|
+
logger_1.default.error('Failed to compile validator schema', { error: compileError, schema });
|
|
83
|
+
throw new errors_1.BadRequest([new Error(`Failed to compile validator schema: ${compileError.message}`)], ['Invalid validator schema']);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
if (error instanceof errors_1.BadRequest) {
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
logger_1.default.error('Error validating validator schema', { error, schema });
|
|
91
|
+
throw new errors_1.BadRequest([new Error(`Error validating validator schema: ${error.message}`)], ['Invalid validator schema']);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
exports.validateValidatorSchema = validateValidatorSchema;
|
|
95
|
+
exports.default = exports.validateValidatorSchema;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ValidationResult } from 'joi';
|
|
2
|
+
import type { CustomFieldDefinitionType } from '../constants';
|
|
3
|
+
/**
|
|
4
|
+
* Validator is a function that validates a custom-field `value`,
|
|
5
|
+
* against a custom-field definition `validation object`.
|
|
6
|
+
* @returns `true` if the value is valid, `false` otherwise.
|
|
7
|
+
*/
|
|
8
|
+
export type Validator<Value, DefinitionValidationObject> = (value: Value, validation?: DefinitionValidationObject) => ValidationResult;
|
|
9
|
+
/**
|
|
10
|
+
* Validators is a map of custom-field types to their respective validators.
|
|
11
|
+
* The key is the custom-field type, and the value is the validator function.
|
|
12
|
+
*/
|
|
13
|
+
export type Validators = {
|
|
14
|
+
[K in CustomFieldDefinitionType]: Validator<unknown, unknown>;
|
|
15
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { CustomFieldDefinitionType } from '../../constants';
|
|
2
|
+
import type { Validators } from '../type';
|
|
3
|
+
/**
|
|
4
|
+
* Custom field types that must have custom validation.
|
|
5
|
+
* TODO: remove this after moving to schema-based validation
|
|
6
|
+
*/
|
|
7
|
+
export declare const CustomValidationTypes: {
|
|
8
|
+
readonly select: CustomFieldDefinitionType.SELECT;
|
|
9
|
+
readonly status: CustomFieldDefinitionType.STATUS;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Validators for custom fields
|
|
13
|
+
*/
|
|
14
|
+
export declare const validators: Validators;
|
|
@@ -1,2 +1,40 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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.validators = exports.CustomValidationTypes = void 0;
|
|
7
|
+
const joi_1 = __importDefault(require("joi"));
|
|
8
|
+
const constants_1 = require("../../constants");
|
|
9
|
+
const select_validator_1 = require("./select.validator");
|
|
10
|
+
const status_validator_1 = require("./status.validator");
|
|
11
|
+
/**
|
|
12
|
+
* Custom field types that must have custom validation.
|
|
13
|
+
* TODO: remove this after moving to schema-based validation
|
|
14
|
+
*/
|
|
15
|
+
exports.CustomValidationTypes = {
|
|
16
|
+
[constants_1.CustomFieldDefinitionType.SELECT]: constants_1.CustomFieldDefinitionType.SELECT,
|
|
17
|
+
[constants_1.CustomFieldDefinitionType.STATUS]: constants_1.CustomFieldDefinitionType.STATUS,
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Validators for custom fields
|
|
21
|
+
*/
|
|
22
|
+
exports.validators = {
|
|
23
|
+
[constants_1.CustomFieldDefinitionType.SELECT]: select_validator_1.validateSelect,
|
|
24
|
+
[constants_1.CustomFieldDefinitionType.STATUS]: status_validator_1.validateStatus,
|
|
25
|
+
[constants_1.CustomFieldDefinitionType.TEXT]: (value) => joi_1.default.string().min(0).validate(value),
|
|
26
|
+
[constants_1.CustomFieldDefinitionType.NUMBER]: (value) => joi_1.default.number().strict(true).validate(value),
|
|
27
|
+
[constants_1.CustomFieldDefinitionType.BOOLEAN]: (value) => joi_1.default.boolean().strict().validate(value),
|
|
28
|
+
[constants_1.CustomFieldDefinitionType.DATE]: (value) => joi_1.default.date().validate(value),
|
|
29
|
+
[constants_1.CustomFieldDefinitionType.DATETIME]: (value) => joi_1.default.date().validate(value),
|
|
30
|
+
[constants_1.CustomFieldDefinitionType.IMAGE]: (value) => joi_1.default.array().min(1).unique()
|
|
31
|
+
.items(joi_1.default.string().uri())
|
|
32
|
+
.validate(value),
|
|
33
|
+
[constants_1.CustomFieldDefinitionType.FILE]: (value) => joi_1.default.array().min(1).unique().items(joi_1.default.object({
|
|
34
|
+
name: joi_1.default.string().required(),
|
|
35
|
+
type: joi_1.default.string(),
|
|
36
|
+
size: joi_1.default.string(),
|
|
37
|
+
addedBy: joi_1.default.string().uuid(),
|
|
38
|
+
}))
|
|
39
|
+
.validate(value),
|
|
40
|
+
};
|
|
@@ -1,2 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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.validateSelect = void 0;
|
|
7
|
+
const joi_1 = __importDefault(require("joi"));
|
|
8
|
+
/**
|
|
9
|
+
* Validate that the value is one of the select values
|
|
10
|
+
*/
|
|
11
|
+
const validateSelect = (value, selectValues) => (joi_1.default.string().allow(null).valid(...selectValues).validate(value));
|
|
12
|
+
exports.validateSelect = validateSelect;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Validator } from '../type';
|
|
2
|
+
type StatusColor = string | null;
|
|
3
|
+
type StatusValue = string;
|
|
4
|
+
type StatusOption = {
|
|
5
|
+
value: StatusValue;
|
|
6
|
+
color: StatusColor;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Validate that the value is one of the status values
|
|
10
|
+
*/
|
|
11
|
+
export declare const validateStatus: Validator<StatusValue, StatusOption[]>;
|
|
12
|
+
export {};
|