@autofleet/sadot 0.6.2-temp-file-6 → 0.6.2
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/.env +3 -0
- package/dist/api/v1/definition/validations.d.ts +2 -2
- package/dist/api/v1/definition/validations.js +6 -19
- package/dist/api/v1/errors.js +1 -1
- package/dist/hooks/create.d.ts +1 -2
- package/dist/hooks/create.js +4 -8
- package/dist/hooks/enrich.d.ts +1 -3
- package/dist/hooks/enrich.js +4 -22
- package/dist/hooks/update.d.ts +1 -2
- package/dist/hooks/update.js +3 -4
- package/dist/index.d.ts +2 -3
- package/dist/index.js +1 -3
- package/dist/models/CustomFieldDefinition.d.ts +1 -1
- package/dist/models/CustomFieldDefinition.js +6 -11
- package/dist/models/CustomFieldValue.js +8 -3
- package/dist/models/index.d.ts +1 -3
- package/dist/models/index.js +2 -8
- package/dist/repository/definition.d.ts +4 -7
- package/dist/repository/definition.js +15 -27
- package/dist/repository/value.d.ts +2 -6
- package/dist/repository/value.js +3 -13
- package/dist/scopes/filter.d.ts +1 -1
- package/dist/scopes/filter.js +4 -16
- package/dist/tests/helpers/database-config.d.ts +0 -1
- package/dist/tests/helpers/database-config.js +0 -1
- package/dist/tests/helpers/index.js +0 -2
- package/dist/tests/mocks/definition.mock.d.ts +3 -9
- package/dist/tests/mocks/definition.mock.js +10 -14
- package/dist/tests/mocks/testModel.d.ts +1 -1
- package/dist/types/index.d.ts +2 -20
- package/dist/utils/constants/index.d.ts +1 -18
- package/dist/utils/constants/index.js +3 -19
- package/dist/utils/init.d.ts +4 -5
- package/dist/utils/init.js +9 -16
- package/dist/utils/validations/custom-fields.d.ts +2 -0
- package/dist/utils/validations/{schema/custom-fields.js → custom-fields.js} +2 -1
- package/dist/utils/validations/custom.d.ts +15 -0
- package/dist/utils/validations/custom.js +42 -0
- package/dist/utils/validations/index.d.ts +2 -2
- package/dist/utils/validations/index.js +15 -15
- package/dist/utils/validations/type.d.ts +14 -10
- package/dist/utils/validations/type.js +48 -0
- package/dist/utils/validations/validators.d.ts +12 -0
- package/dist/utils/validations/validators.js +33 -0
- package/package.json +2 -2
- package/src/api/v1/definition/validations.ts +1 -1
- package/src/index.ts +1 -0
- package/src/models/CustomFieldDefinition.ts +7 -5
- package/src/utils/constants/index.ts +1 -15
- package/src/utils/helpers/index.ts +1 -1
- package/src/utils/validations/index.ts +1 -1
- package/src/utils/validations/type.ts +12 -13
- package/src/utils/validations/validators/index.ts +1 -10
- package/src/utils/validations/validators/select.validator.ts +3 -1
- package/src/utils/validations/validators/status.validator.ts +4 -2
- package/dist/models/tests/contextAwareModels/ContextAwareTestModel.d.ts +0 -10
- package/dist/models/tests/contextAwareModels/ContextAwareTestModel.js +0 -55
- package/dist/models/tests/contextAwareModels/ContextTestModel.d.ts +0 -13
- package/dist/models/tests/contextAwareModels/ContextTestModel.js +0 -47
- package/dist/tests/functional/searching/index.d.ts +0 -8
- package/dist/tests/functional/searching/index.js +0 -44
- package/dist/utils/helpers/index.d.ts +0 -25
- package/dist/utils/helpers/index.js +0 -34
- package/dist/utils/scopeAttributes.d.ts +0 -2
- package/dist/utils/scopeAttributes.js +0 -11
- package/dist/utils/validations/schema/custom-fields.d.ts +0 -3
- package/dist/utils/validations/validators/index.d.ts +0 -14
- package/dist/utils/validations/validators/index.js +0 -40
- package/dist/utils/validations/validators/select.validator.d.ts +0 -5
- package/dist/utils/validations/validators/select.validator.js +0 -9
- package/dist/utils/validations/validators/status.validator.d.ts +0 -12
- package/dist/utils/validations/validators/status.validator.js +0 -9
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
export declare const contextAwareFieldDefinition: {
|
|
3
|
-
name: string;
|
|
4
|
-
modelType: string;
|
|
5
|
-
fieldType: string;
|
|
6
|
-
entityType: string;
|
|
7
|
-
};
|
|
1
|
+
import { CreateCustomFieldDefinition, CustomFieldDefinitionDTO } from '../../types/definition';
|
|
8
2
|
export declare const coolFieldDefinition: CreateCustomFieldDefinition;
|
|
9
3
|
export declare const coolFieldDefinition2: {
|
|
10
4
|
name: string;
|
|
@@ -37,7 +31,7 @@ export declare const coolFieldDefinition3: {
|
|
|
37
31
|
modelType: string;
|
|
38
32
|
};
|
|
39
33
|
export declare const booleanField: (modelType: string) => CreateCustomFieldDefinition;
|
|
40
|
-
export declare const
|
|
41
|
-
export declare const
|
|
34
|
+
export declare const enumField: (modelType: string, options: any) => CreateCustomFieldDefinition;
|
|
35
|
+
export declare const rangeField: (modelType: string) => CreateCustomFieldDefinition;
|
|
42
36
|
export declare const createDefinition: (defaults: Partial<CustomFieldDefinitionDTO>) => CreateCustomFieldDefinition;
|
|
43
37
|
export declare const createDefinitions: (defaults: Partial<CustomFieldDefinitionDTO>, length?: number) => CreateCustomFieldDefinition[];
|
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createDefinitions = exports.createDefinition = exports.
|
|
3
|
+
exports.createDefinitions = exports.createDefinition = exports.rangeField = exports.enumField = exports.booleanField = exports.coolFieldDefinition3 = exports.coolFieldDefinition2 = exports.coolFieldDefinition = void 0;
|
|
4
4
|
const uuid_1 = require("uuid");
|
|
5
|
-
exports.contextAwareFieldDefinition = {
|
|
6
|
-
name: 'cool field',
|
|
7
|
-
modelType: 'ContextAwareTestModel',
|
|
8
|
-
fieldType: 'number',
|
|
9
|
-
entityType: 'fleetId',
|
|
10
|
-
};
|
|
11
5
|
exports.coolFieldDefinition = {
|
|
12
6
|
name: 'cool field',
|
|
13
7
|
modelType: 'TestModel',
|
|
@@ -31,7 +25,7 @@ const booleanField = (modelType) => ({
|
|
|
31
25
|
entityType: 'fleetId',
|
|
32
26
|
});
|
|
33
27
|
exports.booleanField = booleanField;
|
|
34
|
-
const
|
|
28
|
+
const enumField = (modelType, options) => ({
|
|
35
29
|
name: 'choices',
|
|
36
30
|
modelType,
|
|
37
31
|
fieldType: 'select',
|
|
@@ -39,16 +33,18 @@ const selectField = (modelType, options) => ({
|
|
|
39
33
|
entityId: (0, uuid_1.v4)(),
|
|
40
34
|
entityType: 'fleetId',
|
|
41
35
|
});
|
|
42
|
-
exports.
|
|
43
|
-
const
|
|
44
|
-
name: '
|
|
36
|
+
exports.enumField = enumField;
|
|
37
|
+
const rangeField = (modelType) => ({
|
|
38
|
+
name: 'ranges',
|
|
45
39
|
modelType,
|
|
46
|
-
fieldType: '
|
|
47
|
-
validation:
|
|
40
|
+
fieldType: 'number',
|
|
41
|
+
validation: {
|
|
42
|
+
between: [10, 12],
|
|
43
|
+
},
|
|
48
44
|
entityId: (0, uuid_1.v4)(),
|
|
49
45
|
entityType: 'fleetId',
|
|
50
46
|
});
|
|
51
|
-
exports.
|
|
47
|
+
exports.rangeField = rangeField;
|
|
52
48
|
// eslint-disable-next-line max-len
|
|
53
49
|
const createDefinition = (defaults) => ({
|
|
54
50
|
name: defaults?.name || `def_${(0, uuid_1.v4)()}`,
|
|
@@ -3,7 +3,7 @@ export declare const createTestModel: (payload?: {}) => Promise<TestModel>;
|
|
|
3
3
|
export declare const createTestModels: (fleetId: any, total: number) => Promise<TestModel[]>;
|
|
4
4
|
export declare const upsertTestModel: (payload: any) => Promise<[TestModel, boolean]>;
|
|
5
5
|
export declare const destroyTestModels: () => Promise<number>;
|
|
6
|
-
export declare const getTestModel: (id: string, options?: {}) => Promise<TestModel
|
|
6
|
+
export declare const getTestModel: (id: string, options?: {}) => Promise<TestModel>;
|
|
7
7
|
export declare const getSomeTestModels: (options?: {
|
|
8
8
|
limit: number;
|
|
9
9
|
}) => Promise<TestModel[]>;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,31 +1,13 @@
|
|
|
1
|
-
import type { IncludeOptions } from 'sequelize';
|
|
2
|
-
import type { ModelCtor } from 'sequelize-typescript';
|
|
3
1
|
export type ModelFetcher = (name: string) => any;
|
|
4
2
|
export type ModelOptions = {
|
|
5
|
-
/**
|
|
6
|
-
* Include options for the model
|
|
7
|
-
*/
|
|
8
|
-
include?: (entityIds: string[]) => IncludeOptions[];
|
|
9
|
-
/**
|
|
10
|
-
* Custom association for the model
|
|
11
|
-
* @param model - The model to associate with
|
|
12
|
-
*/
|
|
13
|
-
customAssociation?: (model: ModelCtor) => void;
|
|
14
|
-
/**
|
|
15
|
-
* Whether to use the entity id from the instance per scope attribute
|
|
16
|
-
*/
|
|
17
|
-
useEntityIdFromInclude?: boolean;
|
|
18
|
-
};
|
|
19
|
-
export type Models = {
|
|
20
3
|
name: string;
|
|
21
|
-
scopeAttributes:
|
|
22
|
-
modelOptions?: ModelOptions;
|
|
4
|
+
scopeAttributes: string[];
|
|
23
5
|
creationWebhookHandler?: (instance: any) => any;
|
|
24
6
|
updateWebhookHandler?: (instance: any) => any;
|
|
25
7
|
deletionWebhookHandler?: (instance: any) => any;
|
|
26
8
|
};
|
|
27
9
|
export type CustomFieldOptions = {
|
|
28
|
-
models:
|
|
10
|
+
models: ModelOptions[];
|
|
29
11
|
databaseConfig: any;
|
|
30
12
|
getUser: () => any;
|
|
31
13
|
};
|
|
@@ -1,19 +1,2 @@
|
|
|
1
|
-
declare const CUSTOM_FIELDS_FILTER_SCOPE: string;
|
|
2
1
|
export declare const supportedEntities: string[];
|
|
3
|
-
|
|
4
|
-
* Supported custom field types
|
|
5
|
-
*/
|
|
6
|
-
export declare enum CustomFieldDefinitionType {
|
|
7
|
-
NUMBER = "number",
|
|
8
|
-
BOOLEAN = "boolean",
|
|
9
|
-
DATE = "date",
|
|
10
|
-
DATETIME = "datetime",
|
|
11
|
-
TEXT = "text",
|
|
12
|
-
IMAGE = "image",
|
|
13
|
-
SELECT = "select",
|
|
14
|
-
STATUS = "status",
|
|
15
|
-
FILE = "file"
|
|
16
|
-
}
|
|
17
|
-
export {
|
|
18
|
-
/** @deprecated Use the value from `@autofleet/common-types` instead */
|
|
19
|
-
CUSTOM_FIELDS_FILTER_SCOPE, };
|
|
2
|
+
export declare const CUSTOM_FIELDS_FILTER_SCOPE = "filterByCustomFields";
|
|
@@ -1,22 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CUSTOM_FIELDS_FILTER_SCOPE = exports.
|
|
4
|
-
|
|
5
|
-
const { CUSTOM_FIELDS_FILTER_SCOPE } = common_types_1.customFields;
|
|
6
|
-
exports.CUSTOM_FIELDS_FILTER_SCOPE = CUSTOM_FIELDS_FILTER_SCOPE;
|
|
3
|
+
exports.CUSTOM_FIELDS_FILTER_SCOPE = exports.supportedEntities = void 0;
|
|
4
|
+
// eslint-disable-next-line import/prefer-default-export
|
|
7
5
|
exports.supportedEntities = ['businessModelId', 'fleetId', 'demandSourceId'];
|
|
8
|
-
|
|
9
|
-
* Supported custom field types
|
|
10
|
-
*/
|
|
11
|
-
var CustomFieldDefinitionType;
|
|
12
|
-
(function (CustomFieldDefinitionType) {
|
|
13
|
-
CustomFieldDefinitionType["NUMBER"] = "number";
|
|
14
|
-
CustomFieldDefinitionType["BOOLEAN"] = "boolean";
|
|
15
|
-
CustomFieldDefinitionType["DATE"] = "date";
|
|
16
|
-
CustomFieldDefinitionType["DATETIME"] = "datetime";
|
|
17
|
-
CustomFieldDefinitionType["TEXT"] = "text";
|
|
18
|
-
CustomFieldDefinitionType["IMAGE"] = "image";
|
|
19
|
-
CustomFieldDefinitionType["SELECT"] = "select";
|
|
20
|
-
CustomFieldDefinitionType["STATUS"] = "status";
|
|
21
|
-
CustomFieldDefinitionType["FILE"] = "file";
|
|
22
|
-
})(CustomFieldDefinitionType || (exports.CustomFieldDefinitionType = CustomFieldDefinitionType = {}));
|
|
6
|
+
exports.CUSTOM_FIELDS_FILTER_SCOPE = 'filterByCustomFields';
|
package/dist/utils/init.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type { ModelFetcher,
|
|
2
|
-
export declare const addHooks: (models:
|
|
3
|
-
export declare const removeHooks: (models:
|
|
4
|
-
export declare const addScopes: (models:
|
|
5
|
-
export declare const applyCustomAssociation: (models: Models[]) => void;
|
|
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;
|
package/dist/utils/init.js
CHANGED
|
@@ -3,16 +3,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
6
|
+
exports.addScopes = exports.removeHooks = exports.addHooks = void 0;
|
|
7
7
|
const sequelize_1 = require("sequelize");
|
|
8
|
-
const common_types_1 = require("@autofleet/common-types");
|
|
9
8
|
const models_1 = require("../models");
|
|
10
9
|
const hooks_1 = require("../hooks");
|
|
11
10
|
const scopes_1 = require("../scopes");
|
|
12
11
|
const logger_1 = __importDefault(require("./logger"));
|
|
13
|
-
const
|
|
12
|
+
const constants_1 = require("./constants");
|
|
14
13
|
const addHooks = (models, getModel) => {
|
|
15
|
-
models.forEach(async ({ name, scopeAttributes
|
|
14
|
+
models.forEach(async ({ name, scopeAttributes }) => {
|
|
16
15
|
try {
|
|
17
16
|
const model = getModel(name);
|
|
18
17
|
if (!model) {
|
|
@@ -31,11 +30,11 @@ const addHooks = (models, getModel) => {
|
|
|
31
30
|
model.addHook('beforeFind', 'sadot-beforeFind', (0, hooks_1.beforeFind)(scopeAttributes));
|
|
32
31
|
model.addHook('beforeBulkCreate', 'sadot-beforeBulkCreate', hooks_1.beforeBulkCreate);
|
|
33
32
|
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
|
|
37
|
-
model.addHook('afterUpdate', 'sadot-afterUpdate', (0, hooks_1.enrichResults)(name, scopeAttributes
|
|
38
|
-
model.addHook('afterCreate', 'sadot-afterCreate', (0, hooks_1.enrichResults)(name, scopeAttributes
|
|
33
|
+
model.addHook('beforeCreate', 'sadot-beforeCreate', (0, hooks_1.beforeCreate)(scopeAttributes));
|
|
34
|
+
model.addHook('beforeUpdate', 'sadot-beforeUpdate', (0, hooks_1.beforeUpdate)(scopeAttributes));
|
|
35
|
+
model.addHook('afterFind', 'sadot-afterFind', (0, hooks_1.enrichResults)(name, scopeAttributes));
|
|
36
|
+
model.addHook('afterUpdate', 'sadot-afterUpdate', (0, hooks_1.enrichResults)(name, scopeAttributes));
|
|
37
|
+
model.addHook('afterCreate', 'sadot-afterCreate', (0, hooks_1.enrichResults)(name, scopeAttributes));
|
|
39
38
|
}
|
|
40
39
|
catch (e) {
|
|
41
40
|
logger_1.default.error(`Could not add custom fields hook to model ${name}. `, e);
|
|
@@ -90,7 +89,7 @@ const addScopes = (models, getModel) => {
|
|
|
90
89
|
// Necessary associations for the filtering scope
|
|
91
90
|
addAssociations(model, name);
|
|
92
91
|
// Add filter scope
|
|
93
|
-
model.addScope(CUSTOM_FIELDS_FILTER_SCOPE, (0, scopes_1.customFieldsFilterScope)(name));
|
|
92
|
+
model.addScope(constants_1.CUSTOM_FIELDS_FILTER_SCOPE, (0, scopes_1.customFieldsFilterScope)(name));
|
|
94
93
|
}
|
|
95
94
|
catch (e) {
|
|
96
95
|
logger_1.default.error(`Could not add custom fields scopes to model ${name}. `, e);
|
|
@@ -98,9 +97,3 @@ const addScopes = (models, getModel) => {
|
|
|
98
97
|
});
|
|
99
98
|
};
|
|
100
99
|
exports.addScopes = addScopes;
|
|
101
|
-
const applyCustomAssociation = (models) => {
|
|
102
|
-
models.forEach(({ modelOptions }) => {
|
|
103
|
-
modelOptions?.customAssociation?.(models_1.CustomFieldDefinition);
|
|
104
|
-
});
|
|
105
|
-
};
|
|
106
|
-
exports.applyCustomAssociation = applyCustomAssociation;
|
|
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.CustomFieldsSchema = void 0;
|
|
7
|
-
|
|
7
|
+
/* eslint-disable import/prefer-default-export */
|
|
8
|
+
const joi_1 = __importDefault(require("@hapi/joi"));
|
|
8
9
|
const CustomFieldsSchema = joi_1.default.object().pattern(joi_1.default.string(), joi_1.default.any());
|
|
9
10
|
exports.CustomFieldsSchema = CustomFieldsSchema;
|
|
@@ -0,0 +1,15 @@
|
|
|
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;
|
|
@@ -0,0 +1,42 @@
|
|
|
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 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
1
|
+
declare const validateValue: (value: any, valueType: any, customValidations: any) => boolean;
|
|
2
|
+
export default validateValue;
|
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const validateValue = (value, valueType,
|
|
6
|
-
|
|
7
|
-
if (!validator) {
|
|
8
|
-
// Unsupported field type
|
|
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)) {
|
|
9
10
|
return false;
|
|
10
11
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
*/
|
|
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
19
|
};
|
|
20
|
-
exports.
|
|
20
|
+
exports.default = validateValue;
|
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
import type { CustomFieldDefinitionType } from '../constants';
|
|
2
1
|
/**
|
|
3
|
-
*
|
|
4
|
-
* against a custom-field definition `validation object`.
|
|
5
|
-
* @returns `true` if the value is valid, `false` otherwise.
|
|
2
|
+
* Supported custom field types
|
|
6
3
|
*/
|
|
7
|
-
export
|
|
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
|
+
}
|
|
8
13
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
14
|
+
* Validate that the given value is really of type "valueType"
|
|
15
|
+
* TODO: verify that required field not set to null
|
|
11
16
|
*/
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
};
|
|
17
|
+
declare const validateValueType: (value: unknown, valueType: CustomFieldDefinitionType) => boolean;
|
|
18
|
+
export default validateValueType;
|
|
@@ -1,2 +1,50 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.CustomFieldDefinitionType = void 0;
|
|
7
|
+
const joi_1 = __importDefault(require("@hapi/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;
|
|
@@ -0,0 +1,12 @@
|
|
|
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;
|
|
@@ -0,0 +1,33 @@
|
|
|
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;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -17,6 +17,7 @@ export * from './utils/constants';
|
|
|
17
17
|
|
|
18
18
|
export * from './utils/helpers';
|
|
19
19
|
|
|
20
|
+
export { CustomFieldDefinitionType } from './utils/validations/type';
|
|
20
21
|
/**
|
|
21
22
|
* Adding custom fields enrichment to the models inside the MODELS_FILE_NAME json file
|
|
22
23
|
* @see {@link 'custom-fields/config'} for configurations
|
|
@@ -10,7 +10,8 @@ import {
|
|
|
10
10
|
AfterSave,
|
|
11
11
|
Is,
|
|
12
12
|
} from 'sequelize-typescript';
|
|
13
|
-
|
|
13
|
+
|
|
14
|
+
import { CustomFieldDefinitionType } from '../utils/validations/type';
|
|
14
15
|
import { CustomValidationTypes } from '../utils/validations/validators';
|
|
15
16
|
import { CustomFieldValue } from '.';
|
|
16
17
|
import { sendDimEvent } from '../events';
|
|
@@ -30,10 +31,11 @@ import logger from '../utils/logger';
|
|
|
30
31
|
validate: {
|
|
31
32
|
validationByType(this: CustomFieldDefinition) {
|
|
32
33
|
// Verify the validation is provided if needed
|
|
33
|
-
if (!this.validation
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
if (!this.validation) {
|
|
35
|
+
if (CustomValidationTypes[this.fieldType]) {
|
|
36
|
+
logger.error(`No custom validation for custom field type ${this.fieldType} found`);
|
|
37
|
+
throw new UnsupportedCustomValidationError(`Validation provided for "${this.fieldType}" is not supported`);
|
|
38
|
+
}
|
|
37
39
|
}
|
|
38
40
|
},
|
|
39
41
|
},
|
|
@@ -2,23 +2,9 @@ import { customFields } from '@autofleet/common-types';
|
|
|
2
2
|
|
|
3
3
|
const { CUSTOM_FIELDS_FILTER_SCOPE } = customFields;
|
|
4
4
|
|
|
5
|
+
// eslint-disable-next-line import/prefer-default-export
|
|
5
6
|
export const supportedEntities = ['businessModelId', 'fleetId', 'demandSourceId'];
|
|
6
7
|
|
|
7
|
-
/**
|
|
8
|
-
* Supported custom field types
|
|
9
|
-
*/
|
|
10
|
-
export enum CustomFieldDefinitionType {
|
|
11
|
-
NUMBER = 'number',
|
|
12
|
-
BOOLEAN = 'boolean',
|
|
13
|
-
DATE = 'date',
|
|
14
|
-
DATETIME = 'datetime',
|
|
15
|
-
TEXT = 'text',
|
|
16
|
-
IMAGE = 'image',
|
|
17
|
-
SELECT = 'select',
|
|
18
|
-
STATUS = 'status',
|
|
19
|
-
FILE = 'file',
|
|
20
|
-
}
|
|
21
|
-
|
|
22
8
|
export {
|
|
23
9
|
/** @deprecated Use the value from `@autofleet/common-types` instead */
|
|
24
10
|
CUSTOM_FIELDS_FILTER_SCOPE,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/* eslint-disable import/prefer-default-export */
|
|
2
2
|
import { type WhereOptions, Op, type BindOrReplacements } from 'sequelize';
|
|
3
3
|
import { type ModelStatic, Sequelize } from 'sequelize-typescript';
|
|
4
|
-
import { CustomFieldDefinitionType } from '../
|
|
4
|
+
import { CustomFieldDefinitionType } from '../validations/type';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Builds a WHERE clause and replacements for free-text search by custom fields.
|
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
import type { CustomFieldDefinitionType } from '../constants';
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
|
-
*
|
|
5
|
-
* against a custom-field definition `validation object`.
|
|
6
|
-
* @returns `true` if the value is valid, `false` otherwise.
|
|
2
|
+
* Supported custom field types enum
|
|
7
3
|
*/
|
|
8
|
-
export
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
4
|
+
export enum CustomFieldDefinitionType {
|
|
5
|
+
NUMBER = 'number',
|
|
6
|
+
BOOLEAN = 'boolean',
|
|
7
|
+
DATE = 'date',
|
|
8
|
+
DATETIME = 'datetime',
|
|
9
|
+
TEXT = 'text',
|
|
10
|
+
IMAGE = 'image',
|
|
11
|
+
SELECT = 'select',
|
|
12
|
+
STATUS = 'status',
|
|
13
|
+
}
|
|
12
14
|
|
|
13
|
-
|
|
14
|
-
* Validators is a map of custom-field types to their respective validators.
|
|
15
|
-
* The key is the custom-field type, and the value is the validator function.
|
|
16
|
-
*/
|
|
15
|
+
export type Validator<Value, Validation> = (value: Value, validation?: Validation) => boolean;
|
|
17
16
|
export type Validators = {
|
|
18
17
|
[K in CustomFieldDefinitionType]: Validator<unknown, unknown>;
|
|
19
18
|
};
|
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
import Joi from 'joi';
|
|
2
|
-
import { CustomFieldDefinitionType } from '
|
|
3
|
-
import type { Validators } from '../type';
|
|
2
|
+
import { CustomFieldDefinitionType, type Validators } from '../type';
|
|
4
3
|
import { validateSelect } from './select.validator';
|
|
5
4
|
import { validateStatus } from './status.validator';
|
|
6
5
|
|
|
7
6
|
type CustomValidationTypes = Extract<CustomFieldDefinitionType, 'select' | 'status'>;
|
|
8
7
|
/**
|
|
9
8
|
* Custom field types that must have custom validation.
|
|
10
|
-
* TODO: remove this after moving to schema-based validation
|
|
11
9
|
*/
|
|
12
10
|
export const CustomValidationTypes = {
|
|
13
11
|
[CustomFieldDefinitionType.SELECT]: CustomFieldDefinitionType.SELECT,
|
|
@@ -28,11 +26,4 @@ export const validators: Validators = {
|
|
|
28
26
|
[CustomFieldDefinitionType.IMAGE]: (value) => (!Joi.array().min(1).unique()
|
|
29
27
|
.items(Joi.string().uri())
|
|
30
28
|
.validate(value).error),
|
|
31
|
-
[CustomFieldDefinitionType.FILE]: (value) => (!Joi.array().min(1).unique().items(Joi.object({
|
|
32
|
-
name: Joi.string().required(),
|
|
33
|
-
type: Joi.string(),
|
|
34
|
-
size: Joi.string(),
|
|
35
|
-
addedBy: Joi.string().uuid(),
|
|
36
|
-
}))
|
|
37
|
-
.validate(value).error),
|
|
38
29
|
};
|