@autofleet/sadot 0.6.1-temp-2 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/index.d.ts +2 -0
- package/dist/api/index.js +11 -0
- package/dist/api/v1/definition/index.d.ts +2 -0
- package/dist/api/v1/definition/index.js +118 -0
- package/dist/api/v1/definition/validations.d.ts +2 -0
- package/dist/api/v1/definition/validations.js +36 -0
- package/dist/api/v1/errors.d.ts +2 -0
- package/dist/api/v1/errors.js +15 -0
- package/dist/api/v1/index.d.ts +2 -0
- package/dist/api/v1/index.js +10 -0
- package/dist/errors/index.d.ts +16 -0
- package/dist/errors/index.js +45 -0
- package/dist/events/index.d.ts +4 -0
- package/dist/events/index.js +47 -0
- package/dist/hooks/create.d.ts +10 -0
- package/dist/hooks/create.js +74 -0
- package/dist/hooks/enrich.d.ts +7 -0
- package/dist/hooks/enrich.js +126 -0
- package/dist/hooks/find.d.ts +1 -0
- package/dist/hooks/find.js +29 -0
- package/dist/hooks/index.d.ts +6 -0
- package/dist/hooks/index.js +18 -0
- package/dist/hooks/update.d.ts +10 -0
- package/dist/hooks/update.js +59 -0
- package/dist/hooks/workaround.d.ts +10 -0
- package/dist/hooks/workaround.js +37 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +56 -0
- package/dist/models/CustomFieldDefinition.d.ts +23 -0
- package/dist/models/CustomFieldDefinition.js +165 -0
- package/dist/models/CustomFieldValue.d.ts +15 -0
- package/dist/models/CustomFieldValue.js +148 -0
- package/dist/models/index.d.ts +10 -0
- package/dist/models/index.js +93 -0
- package/dist/models/tests/AssociatedTestModel.d.ts +12 -0
- package/dist/models/tests/AssociatedTestModel.js +71 -0
- package/dist/models/tests/TestModel.d.ts +12 -0
- package/dist/models/tests/TestModel.js +69 -0
- package/dist/models/tests/contextAwareModels/ContextAwareTestModel.d.ts +10 -0
- package/dist/models/tests/contextAwareModels/ContextAwareTestModel.js +55 -0
- package/dist/models/tests/contextAwareModels/ContextTestModel.d.ts +13 -0
- package/dist/models/tests/contextAwareModels/ContextTestModel.js +47 -0
- package/dist/repository/definition.d.ts +20 -0
- package/dist/repository/definition.js +92 -0
- package/dist/repository/value.d.ts +28 -0
- package/dist/repository/value.js +117 -0
- package/dist/scopes/filter.d.ts +23 -0
- package/dist/scopes/filter.js +55 -0
- package/dist/scopes/index.d.ts +2 -0
- package/dist/scopes/index.js +6 -0
- package/dist/tests/api/test-api.d.ts +2 -0
- package/dist/tests/api/test-api.js +56 -0
- package/dist/tests/functional/searching/index.d.ts +8 -0
- package/dist/tests/functional/searching/index.js +44 -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 +2 -0
- package/dist/tests/helpers/index.js +20 -0
- package/dist/tests/mocks/definition.mock.d.ts +43 -0
- package/dist/tests/mocks/definition.mock.js +70 -0
- package/dist/tests/mocks/events.mock.d.ts +3 -0
- package/dist/tests/mocks/events.mock.js +19 -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 +23 -0
- package/dist/types/definition/index.js +2 -0
- package/dist/types/index.d.ts +31 -0
- 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 +5 -0
- package/dist/utils/constants/index.js +8 -0
- package/dist/utils/db/index.d.ts +4 -0
- package/dist/utils/db/index.js +24 -0
- package/dist/utils/helpers/index.d.ts +25 -0
- package/dist/utils/helpers/index.js +34 -0
- package/dist/utils/init.d.ts +5 -0
- package/dist/utils/init.js +106 -0
- package/dist/utils/logger/index.d.ts +2 -0
- package/dist/utils/logger/index.js +6 -0
- package/dist/utils/scopeAttributes.d.ts +2 -0
- package/dist/utils/scopeAttributes.js +11 -0
- package/dist/utils/validations/custom-fields.d.ts +3 -0
- package/dist/utils/validations/custom-fields.js +10 -0
- 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 -0
- package/dist/utils/validations/index.js +20 -0
- package/dist/utils/validations/type.d.ts +18 -0
- package/dist/utils/validations/type.js +50 -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/utils/validations/type.ts +0 -10
|
@@ -0,0 +1,106 @@
|
|
|
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 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, modelOptions, }) => {
|
|
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, modelOptions));
|
|
35
|
+
model.addHook('beforeUpdate', 'sadot-beforeUpdate', (0, hooks_1.beforeUpdate)(scopeAttributes, modelOptions));
|
|
36
|
+
model.addHook('afterFind', 'sadot-afterFind', (0, hooks_1.enrichResults)(name, scopeAttributes, 'afterFind', modelOptions));
|
|
37
|
+
model.addHook('afterUpdate', 'sadot-afterUpdate', (0, hooks_1.enrichResults)(name, scopeAttributes, null, modelOptions));
|
|
38
|
+
model.addHook('afterCreate', 'sadot-afterCreate', (0, hooks_1.enrichResults)(name, scopeAttributes, null, modelOptions));
|
|
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;
|
|
101
|
+
const applyCustomAssociation = (models) => {
|
|
102
|
+
models.forEach(({ modelOptions }) => {
|
|
103
|
+
modelOptions?.customAssociation?.(models_1.CustomFieldDefinition);
|
|
104
|
+
});
|
|
105
|
+
};
|
|
106
|
+
exports.applyCustomAssociation = applyCustomAssociation;
|
|
@@ -0,0 +1,11 @@
|
|
|
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,10 @@
|
|
|
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;
|
|
@@ -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;
|
|
@@ -0,0 +1,20 @@
|
|
|
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;
|
|
@@ -0,0 +1,18 @@
|
|
|
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;
|
|
@@ -0,0 +1,50 @@
|
|
|
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;
|
|
@@ -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
|
@@ -11,7 +11,6 @@ export enum CustomFieldDefinitionType {
|
|
|
11
11
|
TEXT = 'text',
|
|
12
12
|
IMAGE = 'image',
|
|
13
13
|
SELECT = 'select',
|
|
14
|
-
FILE = 'file',
|
|
15
14
|
}
|
|
16
15
|
/**
|
|
17
16
|
* Validate that the given value is really of type "valueType"
|
|
@@ -38,15 +37,6 @@ const validateValueType = (value: unknown, valueType: CustomFieldDefinitionType)
|
|
|
38
37
|
case CustomFieldDefinitionType.IMAGE:
|
|
39
38
|
return !Joi.array().min(1).unique().items(Joi.string().uri())
|
|
40
39
|
.validate(value).error;
|
|
41
|
-
case CustomFieldDefinitionType.FILE:
|
|
42
|
-
return !Joi.array().min(1).unique().items(Joi.object({
|
|
43
|
-
id: Joi.string().uuid().required(),
|
|
44
|
-
name: Joi.string().required(),
|
|
45
|
-
link: Joi.string().required(),
|
|
46
|
-
createdAt: Joi.date(),
|
|
47
|
-
addedBy: Joi.string().uuid(),
|
|
48
|
-
}))
|
|
49
|
-
.validate(value).error;
|
|
50
40
|
default:
|
|
51
41
|
return false;
|
|
52
42
|
}
|