@autofleet/sadot 0.7.9 → 0.7.10-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env +3 -0
- package/dist/errors/index.d.ts +4 -1
- package/dist/errors/index.js +12 -4
- package/dist/models/CustomFieldDefinition.js +2 -2
- package/dist/models/CustomFieldValue.d.ts +3 -2
- package/dist/models/CustomFieldValue.js +23 -14
- package/dist/utils/validations/index.d.ts +2 -1
- package/dist/utils/validations/index.js +4 -7
- package/dist/utils/validations/type.d.ts +2 -1
- package/dist/utils/validations/validators/index.js +10 -9
- package/dist/utils/validations/validators/select.validator.js +5 -2
- package/dist/utils/validations/validators/status.validator.js +5 -2
- package/package.json +1 -1
- package/src/api/v1/definition/validations.ts +3 -3
- package/src/errors/index.ts +24 -3
- package/src/models/CustomFieldDefinition.ts +2 -2
- package/src/models/CustomFieldValue.ts +25 -17
- package/src/utils/validations/index.ts +3 -6
- package/src/utils/validations/type.ts +2 -1
- package/src/utils/validations/validators/index.ts +9 -9
- package/src/utils/validations/validators/select.validator.ts +3 -2
- package/src/utils/validations/validators/status.validator.ts +6 -2
package/.env
ADDED
package/dist/errors/index.d.ts
CHANGED
|
@@ -8,8 +8,11 @@ export declare class UnsupportedCustomFieldTypeError extends BadRequest {
|
|
|
8
8
|
export declare class UnsupportedCustomValidationError extends BadRequest {
|
|
9
9
|
constructor(fieldType: string);
|
|
10
10
|
}
|
|
11
|
+
export declare class InvalidFieldTypeError extends BadRequest {
|
|
12
|
+
constructor(fieldType: string);
|
|
13
|
+
}
|
|
11
14
|
export declare class InvalidValueError extends BadRequest {
|
|
12
|
-
constructor(value: any,
|
|
15
|
+
constructor(value: any, fieldDefinitionName: string, validationErrorMessage: string);
|
|
13
16
|
}
|
|
14
17
|
export declare class MissingDefinitionError extends BadRequest {
|
|
15
18
|
constructor(fieldNames: string[]);
|
package/dist/errors/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MissingDefinitionError = exports.InvalidValueError = exports.UnsupportedCustomValidationError = exports.UnsupportedCustomFieldTypeError = exports.MissingRequiredCustomFieldError = void 0;
|
|
3
|
+
exports.MissingDefinitionError = exports.InvalidValueError = exports.InvalidFieldTypeError = exports.UnsupportedCustomValidationError = exports.UnsupportedCustomFieldTypeError = exports.MissingRequiredCustomFieldError = void 0;
|
|
4
4
|
/* eslint-disable max-classes-per-file */
|
|
5
5
|
const errors_1 = require("@autofleet/errors");
|
|
6
6
|
class MissingRequiredCustomFieldError extends errors_1.BadRequest {
|
|
@@ -27,11 +27,19 @@ class UnsupportedCustomValidationError extends errors_1.BadRequest {
|
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
exports.UnsupportedCustomValidationError = UnsupportedCustomValidationError;
|
|
30
|
+
class InvalidFieldTypeError extends errors_1.BadRequest {
|
|
31
|
+
constructor(fieldType) {
|
|
32
|
+
const err = new Error(`Invalid field type ${fieldType}`);
|
|
33
|
+
super([err], null, null);
|
|
34
|
+
this.message = 'INVALID_FIELD_TYPE';
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.InvalidFieldTypeError = InvalidFieldTypeError;
|
|
30
38
|
class InvalidValueError extends errors_1.BadRequest {
|
|
31
|
-
constructor(value,
|
|
32
|
-
const err = new Error(`Invalid
|
|
39
|
+
constructor(value, fieldDefinitionName, validationErrorMessage) {
|
|
40
|
+
const err = new Error(`Invalid value ${value} for field ${fieldDefinitionName} - ${validationErrorMessage}`);
|
|
33
41
|
super([err], null, null);
|
|
34
|
-
this.message =
|
|
42
|
+
this.message = err.message;
|
|
35
43
|
}
|
|
36
44
|
}
|
|
37
45
|
exports.InvalidValueError = InvalidValueError;
|
|
@@ -28,8 +28,8 @@ let CustomFieldDefinition = class CustomFieldDefinition extends sequelize_typesc
|
|
|
28
28
|
}
|
|
29
29
|
if (![null, undefined].includes(instance.defaultValue)) {
|
|
30
30
|
const isValid = (0, validations_1.validateValue)(instance.defaultValue, instance.fieldType, instance.validation);
|
|
31
|
-
if (
|
|
32
|
-
throw new errors_1.InvalidValueError(instance.defaultValue, instance.fieldType);
|
|
31
|
+
if (isValid.error) {
|
|
32
|
+
throw new errors_1.InvalidValueError(instance.defaultValue, instance.fieldType, isValid.error.details[0].message);
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
}
|
|
@@ -8,8 +8,9 @@ declare class CustomFieldValue extends Model {
|
|
|
8
8
|
updatedAt?: Date;
|
|
9
9
|
deletedAt?: Date;
|
|
10
10
|
customFieldDefinition: CustomFieldDefinition;
|
|
11
|
-
static
|
|
12
|
-
static
|
|
11
|
+
private static validateValueAgainstDefinition;
|
|
12
|
+
static validateCustomFieldValues(instances: CustomFieldValue[]): Promise<void>;
|
|
13
|
+
static validateCustomFieldValue(instance: CustomFieldValue): Promise<void>;
|
|
13
14
|
static afterSaveHandler(instance: CustomFieldValue, options: any): void;
|
|
14
15
|
}
|
|
15
16
|
export default CustomFieldValue;
|
|
@@ -39,7 +39,22 @@ const validations_1 = require("../utils/validations");
|
|
|
39
39
|
const CustomFieldDefinitionRepo = __importStar(require("../repository/definition"));
|
|
40
40
|
const errors_1 = require("../errors");
|
|
41
41
|
let CustomFieldValue = class CustomFieldValue extends sequelize_typescript_1.Model {
|
|
42
|
-
static
|
|
42
|
+
static validateValueAgainstDefinition(instance, definition) {
|
|
43
|
+
const { validation, fieldType, displayName } = definition;
|
|
44
|
+
const isValidFieldType = (0, validations_1.validateFieldType)(fieldType); // check if can be removed for value
|
|
45
|
+
if (!isValidFieldType) {
|
|
46
|
+
throw new errors_1.InvalidFieldTypeError(fieldType);
|
|
47
|
+
}
|
|
48
|
+
// Always allow null values
|
|
49
|
+
if (instance.value === null)
|
|
50
|
+
return; // check if can be used in Joi
|
|
51
|
+
const validateValueResponse = (0, validations_1.validateValue)(instance.value, fieldType, validation);
|
|
52
|
+
if (validateValueResponse.error) {
|
|
53
|
+
const validationErrorMessage = validateValueResponse.error.details[0].message;
|
|
54
|
+
throw new errors_1.InvalidValueError(instance.value, displayName, validationErrorMessage);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
static async validateCustomFieldValues(instances) {
|
|
43
58
|
const ids = instances.map((instance) => instance.customFieldDefinitionId);
|
|
44
59
|
const uniqueIds = [...new Set(ids)];
|
|
45
60
|
const definitions = await CustomFieldDefinitionRepo.findByIds(uniqueIds, { withDisabled: true });
|
|
@@ -47,23 +62,17 @@ let CustomFieldValue = class CustomFieldValue extends sequelize_typescript_1.Mod
|
|
|
47
62
|
throw new Error('Definitions not found');
|
|
48
63
|
}
|
|
49
64
|
instances.forEach((instance) => {
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
if (!isValid) {
|
|
54
|
-
throw new errors_1.InvalidValueError(instance.value, fieldType);
|
|
65
|
+
const definition = definitions.find((d) => d.id === instance.customFieldDefinitionId);
|
|
66
|
+
if (definition) {
|
|
67
|
+
this.validateValueAgainstDefinition(instance, definition);
|
|
55
68
|
}
|
|
56
69
|
});
|
|
57
70
|
}
|
|
58
|
-
static async
|
|
71
|
+
static async validateCustomFieldValue(instance) {
|
|
59
72
|
const { customFieldDefinitionId } = instance;
|
|
60
73
|
// eslint-disable-next-line max-len
|
|
61
74
|
const cfd = await CustomFieldDefinitionRepo.findById(customFieldDefinitionId, { withDisabled: true });
|
|
62
|
-
|
|
63
|
-
const isValid = (0, validations_1.validateValue)(instance.value, fieldType, validation);
|
|
64
|
-
if (!isValid) {
|
|
65
|
-
throw new errors_1.InvalidValueError(instance.value, fieldType);
|
|
66
|
-
}
|
|
75
|
+
this.validateValueAgainstDefinition(instance, cfd);
|
|
67
76
|
}
|
|
68
77
|
static afterSaveHandler(instance, options) {
|
|
69
78
|
if (options.transaction) {
|
|
@@ -120,7 +129,7 @@ __decorate([
|
|
|
120
129
|
__metadata("design:type", Function),
|
|
121
130
|
__metadata("design:paramtypes", [Array]),
|
|
122
131
|
__metadata("design:returntype", Promise)
|
|
123
|
-
], CustomFieldValue, "
|
|
132
|
+
], CustomFieldValue, "validateCustomFieldValues", null);
|
|
124
133
|
__decorate([
|
|
125
134
|
sequelize_typescript_1.BeforeUpdate,
|
|
126
135
|
sequelize_typescript_1.BeforeCreate,
|
|
@@ -128,7 +137,7 @@ __decorate([
|
|
|
128
137
|
__metadata("design:type", Function),
|
|
129
138
|
__metadata("design:paramtypes", [CustomFieldValue]),
|
|
130
139
|
__metadata("design:returntype", Promise)
|
|
131
|
-
], CustomFieldValue, "
|
|
140
|
+
], CustomFieldValue, "validateCustomFieldValue", null);
|
|
132
141
|
__decorate([
|
|
133
142
|
sequelize_typescript_1.AfterUpsert,
|
|
134
143
|
__metadata("design:type", Function),
|
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import type { CustomFieldDefinitionType } from '../constants';
|
|
2
|
-
export declare const
|
|
2
|
+
export declare const validateFieldType: (type: CustomFieldDefinitionType) => boolean;
|
|
3
|
+
export declare const validateValue: (value: unknown, valueType: CustomFieldDefinitionType, validation?: unknown) => import("joi").ValidationResult;
|
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.validateValue = void 0;
|
|
3
|
+
exports.validateValue = exports.validateFieldType = void 0;
|
|
4
4
|
const validators_1 = require("./validators");
|
|
5
|
+
const validateFieldType = (type) => Object.keys(validators_1.validators).includes(type);
|
|
6
|
+
exports.validateFieldType = validateFieldType;
|
|
5
7
|
const validateValue = (value, valueType, validation) => {
|
|
6
8
|
const validator = validators_1.validators[valueType];
|
|
7
|
-
|
|
8
|
-
// Unsupported field type
|
|
9
|
-
return false;
|
|
10
|
-
}
|
|
11
|
-
// Always allow null values
|
|
12
|
-
return value === null || validator(value, validation);
|
|
9
|
+
return validator(value, validation);
|
|
13
10
|
/** TODO: Add validation for required fields
|
|
14
11
|
* @example
|
|
15
12
|
* if (validations.required && !value) {
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import type { ValidationResult } from 'joi';
|
|
1
2
|
import type { CustomFieldDefinitionType } from '../constants';
|
|
2
3
|
/**
|
|
3
4
|
* Validator is a function that validates a custom-field `value`,
|
|
4
5
|
* against a custom-field definition `validation object`.
|
|
5
6
|
* @returns `true` if the value is valid, `false` otherwise.
|
|
6
7
|
*/
|
|
7
|
-
export type Validator<Value, DefinitionValidationObject> = (value: Value, validation?: DefinitionValidationObject) =>
|
|
8
|
+
export type Validator<Value, DefinitionValidationObject> = (value: Value, validation?: DefinitionValidationObject) => ValidationResult;
|
|
8
9
|
/**
|
|
9
10
|
* Validators is a map of custom-field types to their respective validators.
|
|
10
11
|
* The key is the custom-field type, and the value is the validator function.
|
|
@@ -20,21 +20,22 @@ exports.CustomValidationTypes = {
|
|
|
20
20
|
* Validators for custom fields
|
|
21
21
|
*/
|
|
22
22
|
exports.validators = {
|
|
23
|
+
// currently the name shows up as "value", i want to change it to be based on the type
|
|
23
24
|
[constants_1.CustomFieldDefinitionType.SELECT]: select_validator_1.validateSelect,
|
|
24
25
|
[constants_1.CustomFieldDefinitionType.STATUS]: status_validator_1.validateStatus,
|
|
25
|
-
[constants_1.CustomFieldDefinitionType.TEXT]: (value) => (
|
|
26
|
-
[constants_1.CustomFieldDefinitionType.NUMBER]: (value) => (
|
|
27
|
-
[constants_1.CustomFieldDefinitionType.BOOLEAN]: (value) => (
|
|
28
|
-
[constants_1.CustomFieldDefinitionType.DATE]: (value) =>
|
|
29
|
-
[constants_1.CustomFieldDefinitionType.DATETIME]: (value) =>
|
|
30
|
-
[constants_1.CustomFieldDefinitionType.IMAGE]: (value) =>
|
|
26
|
+
[constants_1.CustomFieldDefinitionType.TEXT]: (value) => joi_1.default.string().validate(value),
|
|
27
|
+
[constants_1.CustomFieldDefinitionType.NUMBER]: (value) => joi_1.default.number().strict(true).validate(value),
|
|
28
|
+
[constants_1.CustomFieldDefinitionType.BOOLEAN]: (value) => joi_1.default.boolean().validate(value),
|
|
29
|
+
[constants_1.CustomFieldDefinitionType.DATE]: (value) => joi_1.default.date().validate(value),
|
|
30
|
+
[constants_1.CustomFieldDefinitionType.DATETIME]: (value) => joi_1.default.date().validate(value),
|
|
31
|
+
[constants_1.CustomFieldDefinitionType.IMAGE]: (value) => joi_1.default.array().min(1).unique()
|
|
31
32
|
.items(joi_1.default.string().uri())
|
|
32
|
-
.validate(value)
|
|
33
|
-
[constants_1.CustomFieldDefinitionType.FILE]: (value) =>
|
|
33
|
+
.validate(value),
|
|
34
|
+
[constants_1.CustomFieldDefinitionType.FILE]: (value) => joi_1.default.array().min(1).unique().items(joi_1.default.object({
|
|
34
35
|
name: joi_1.default.string().required(),
|
|
35
36
|
type: joi_1.default.string(),
|
|
36
37
|
size: joi_1.default.string(),
|
|
37
38
|
addedBy: joi_1.default.string().uuid(),
|
|
38
39
|
}))
|
|
39
|
-
.validate(value)
|
|
40
|
+
.validate(value),
|
|
40
41
|
};
|
|
@@ -1,9 +1,12 @@
|
|
|
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
6
|
exports.validateSelect = void 0;
|
|
7
|
+
const joi_1 = __importDefault(require("joi"));
|
|
4
8
|
/**
|
|
5
9
|
* Validate that the value is one of the select values
|
|
6
10
|
*/
|
|
7
|
-
const validateSelect = (value, selectValues) => (
|
|
8
|
-
&& selectValues.includes(value));
|
|
11
|
+
const validateSelect = (value, selectValues) => (joi_1.default.string().allow(null).valid(...selectValues).validate(value));
|
|
9
12
|
exports.validateSelect = validateSelect;
|
|
@@ -1,9 +1,12 @@
|
|
|
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
6
|
exports.validateStatus = void 0;
|
|
7
|
+
const joi_1 = __importDefault(require("joi"));
|
|
4
8
|
/**
|
|
5
9
|
* Validate that the value is one of the status values
|
|
6
10
|
*/
|
|
7
|
-
const validateStatus = (value, statusValues) => (
|
|
8
|
-
&& statusValues.some((status) => status.value === value));
|
|
11
|
+
const validateStatus = (value, statusValues) => (joi_1.default.string().allow(null).valid(...statusValues.map((statusValue) => statusValue.value)).validate(value));
|
|
9
12
|
exports.validateStatus = validateStatus;
|
package/package.json
CHANGED
|
@@ -36,11 +36,11 @@ const DefaultValueSchema = Joi.when('fieldType', {
|
|
|
36
36
|
{ is: CustomFieldDefinitionType.BOOLEAN, then: Joi.boolean().allow(null) },
|
|
37
37
|
{ is: CustomFieldDefinitionType.DATE, then: Joi.date().allow(null) },
|
|
38
38
|
{ is: CustomFieldDefinitionType.DATETIME, then: Joi.date().allow(null) },
|
|
39
|
-
{ is: CustomFieldDefinitionType.FILE, then: FileValidationSchema },
|
|
40
|
-
{ is: CustomFieldDefinitionType.IMAGE, then: Joi.string().uri().allow(null) },
|
|
39
|
+
{ is: CustomFieldDefinitionType.FILE, then: Joi.array().items(FileValidationSchema).allow(null) },
|
|
40
|
+
{ is: CustomFieldDefinitionType.IMAGE, then: Joi.array().items(Joi.string().uri()).allow(null) },
|
|
41
41
|
{ is: CustomFieldDefinitionType.NUMBER, then: Joi.number().allow(null) },
|
|
42
42
|
{ is: CustomFieldDefinitionType.SELECT, then: Joi.string().allow(null) },
|
|
43
|
-
{ is: CustomFieldDefinitionType.STATUS, then:
|
|
43
|
+
{ is: CustomFieldDefinitionType.STATUS, then: Joi.string().allow(null) },
|
|
44
44
|
{ is: CustomFieldDefinitionType.TEXT, then: Joi.string().allow(null) },
|
|
45
45
|
],
|
|
46
46
|
});
|
package/src/errors/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/* eslint-disable max-classes-per-file */
|
|
2
2
|
import { BadRequest } from '@autofleet/errors';
|
|
3
|
+
import type { ValidationError } from 'joi';
|
|
3
4
|
|
|
4
5
|
export class MissingRequiredCustomFieldError extends BadRequest {
|
|
5
6
|
constructor(missingFields: string[]) {
|
|
@@ -25,11 +26,31 @@ export class UnsupportedCustomValidationError extends BadRequest {
|
|
|
25
26
|
}
|
|
26
27
|
}
|
|
27
28
|
|
|
29
|
+
export class InvalidFieldTypeError extends BadRequest {
|
|
30
|
+
constructor(fieldType: string) {
|
|
31
|
+
const err = new Error(`Invalid field type ${fieldType}`);
|
|
32
|
+
super([err], null, null);
|
|
33
|
+
this.message = 'INVALID_FIELD_TYPE';
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
28
37
|
export class InvalidValueError extends BadRequest {
|
|
29
|
-
constructor(value: any,
|
|
30
|
-
const
|
|
38
|
+
constructor(value: any, fieldDefinitionName: string, joiValidationError: ValidationError) {
|
|
39
|
+
const formattedErrorMessage = joiValidationError.message
|
|
40
|
+
.replace(/"/g, '')
|
|
41
|
+
.replace('value', fieldDefinitionName);
|
|
42
|
+
|
|
43
|
+
const formattedValue = typeof value === 'object' ? JSON.stringify(value) : value;
|
|
44
|
+
|
|
45
|
+
const detailedMessage = `
|
|
46
|
+
Invalid value for field ${fieldDefinitionName}
|
|
47
|
+
Received: ${formattedValue}
|
|
48
|
+
Error: ${formattedErrorMessage}
|
|
49
|
+
`.trim();
|
|
50
|
+
|
|
51
|
+
const err = new Error(detailedMessage);
|
|
31
52
|
super([err], null, null);
|
|
32
|
-
this.message =
|
|
53
|
+
this.message = err.message;
|
|
33
54
|
}
|
|
34
55
|
}
|
|
35
56
|
|
|
@@ -136,8 +136,8 @@ class CustomFieldDefinition extends Model {
|
|
|
136
136
|
}
|
|
137
137
|
if (![null, undefined].includes(instance.defaultValue)) {
|
|
138
138
|
const isValid = validateValue(instance.defaultValue, instance.fieldType, instance.validation);
|
|
139
|
-
if (
|
|
140
|
-
throw new InvalidValueError(instance.defaultValue, instance.
|
|
139
|
+
if (isValid.error) {
|
|
140
|
+
throw new InvalidValueError(instance.defaultValue, instance.name, isValid.error);
|
|
141
141
|
}
|
|
142
142
|
}
|
|
143
143
|
}
|
|
@@ -15,9 +15,9 @@ import {
|
|
|
15
15
|
} from 'sequelize-typescript';
|
|
16
16
|
import { sendDimEvent } from '../events';
|
|
17
17
|
import { CustomFieldDefinition } from '.';
|
|
18
|
-
import { validateValue } from '../utils/validations';
|
|
18
|
+
import { validateFieldType, validateValue } from '../utils/validations';
|
|
19
19
|
import * as CustomFieldDefinitionRepo from '../repository/definition';
|
|
20
|
-
import { InvalidValueError } from '../errors';
|
|
20
|
+
import { InvalidFieldTypeError, InvalidValueError } from '../errors';
|
|
21
21
|
|
|
22
22
|
@Table({
|
|
23
23
|
timestamps: true,
|
|
@@ -56,9 +56,26 @@ class CustomFieldValue extends Model {
|
|
|
56
56
|
@BelongsTo(() => CustomFieldDefinition, { scope: { disabled: false } })
|
|
57
57
|
customFieldDefinition: CustomFieldDefinition;
|
|
58
58
|
|
|
59
|
+
private static validateValueAgainstDefinition(
|
|
60
|
+
instance: CustomFieldValue,
|
|
61
|
+
definition: CustomFieldDefinition,
|
|
62
|
+
): void {
|
|
63
|
+
const { validation, fieldType, displayName } = definition;
|
|
64
|
+
const isValidFieldType = validateFieldType(fieldType);
|
|
65
|
+
if (!isValidFieldType) {
|
|
66
|
+
throw new InvalidFieldTypeError(fieldType);
|
|
67
|
+
}
|
|
68
|
+
// Always allow null values
|
|
69
|
+
if (instance.value === null) return;
|
|
70
|
+
const validateValueResponse = validateValue(instance.value, fieldType, validation);
|
|
71
|
+
if (validateValueResponse.error) {
|
|
72
|
+
throw new InvalidValueError(instance.value, displayName, validateValueResponse.error);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
59
76
|
@BeforeBulkCreate
|
|
60
77
|
@BeforeBulkUpdate
|
|
61
|
-
static async
|
|
78
|
+
static async validateCustomFieldValues(instances: CustomFieldValue[]): Promise<void> {
|
|
62
79
|
const ids = instances.map((instance) => instance.customFieldDefinitionId);
|
|
63
80
|
const uniqueIds = [...new Set(ids)];
|
|
64
81
|
const definitions = await CustomFieldDefinitionRepo.findByIds(
|
|
@@ -71,14 +88,9 @@ class CustomFieldValue extends Model {
|
|
|
71
88
|
}
|
|
72
89
|
|
|
73
90
|
instances.forEach((instance) => {
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
} = definitions
|
|
78
|
-
.find((definition) => definition.id === instance.customFieldDefinitionId);
|
|
79
|
-
const isValid = validateValue(instance.value, fieldType, validation);
|
|
80
|
-
if (!isValid) {
|
|
81
|
-
throw new InvalidValueError(instance.value, fieldType);
|
|
91
|
+
const definition = definitions.find((d) => d.id === instance.customFieldDefinitionId);
|
|
92
|
+
if (definition) {
|
|
93
|
+
this.validateValueAgainstDefinition(instance, definition);
|
|
82
94
|
}
|
|
83
95
|
});
|
|
84
96
|
}
|
|
@@ -86,15 +98,11 @@ class CustomFieldValue extends Model {
|
|
|
86
98
|
@BeforeUpdate
|
|
87
99
|
@BeforeCreate
|
|
88
100
|
@BeforeUpsert
|
|
89
|
-
static async
|
|
101
|
+
static async validateCustomFieldValue(instance: CustomFieldValue): Promise<void> {
|
|
90
102
|
const { customFieldDefinitionId } = instance;
|
|
91
103
|
// eslint-disable-next-line max-len
|
|
92
104
|
const cfd = await CustomFieldDefinitionRepo.findById(customFieldDefinitionId, { withDisabled: true });
|
|
93
|
-
|
|
94
|
-
const isValid = validateValue(instance.value, fieldType, validation);
|
|
95
|
-
if (!isValid) {
|
|
96
|
-
throw new InvalidValueError(instance.value, fieldType);
|
|
97
|
-
}
|
|
105
|
+
this.validateValueAgainstDefinition(instance, cfd);
|
|
98
106
|
}
|
|
99
107
|
|
|
100
108
|
@AfterUpsert
|
|
@@ -1,18 +1,15 @@
|
|
|
1
1
|
import type { CustomFieldDefinitionType } from '../constants';
|
|
2
2
|
import { validators } from './validators';
|
|
3
3
|
|
|
4
|
+
export const validateFieldType = (type: CustomFieldDefinitionType): boolean => Object.keys(validators).includes(type);
|
|
5
|
+
|
|
4
6
|
export const validateValue = (
|
|
5
7
|
value: unknown,
|
|
6
8
|
valueType: CustomFieldDefinitionType,
|
|
7
9
|
validation?: unknown,
|
|
8
10
|
) => {
|
|
9
11
|
const validator = validators[valueType];
|
|
10
|
-
|
|
11
|
-
// Unsupported field type
|
|
12
|
-
return false;
|
|
13
|
-
}
|
|
14
|
-
// Always allow null values
|
|
15
|
-
return value === null || validator(value, validation);
|
|
12
|
+
return validator(value, validation);
|
|
16
13
|
/** TODO: Add validation for required fields
|
|
17
14
|
* @example
|
|
18
15
|
* if (validations.required && !value) {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ValidationResult } from 'joi';
|
|
1
2
|
import type { CustomFieldDefinitionType } from '../constants';
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -8,7 +9,7 @@ import type { CustomFieldDefinitionType } from '../constants';
|
|
|
8
9
|
export type Validator<Value, DefinitionValidationObject> = (
|
|
9
10
|
value: Value,
|
|
10
11
|
validation?: DefinitionValidationObject
|
|
11
|
-
) =>
|
|
12
|
+
) => ValidationResult;
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* Validators is a map of custom-field types to their respective validators.
|
|
@@ -20,19 +20,19 @@ export const CustomValidationTypes = {
|
|
|
20
20
|
export const validators: Validators = {
|
|
21
21
|
[CustomFieldDefinitionType.SELECT]: validateSelect,
|
|
22
22
|
[CustomFieldDefinitionType.STATUS]: validateStatus,
|
|
23
|
-
[CustomFieldDefinitionType.TEXT]: (value) => (
|
|
24
|
-
[CustomFieldDefinitionType.NUMBER]: (value) => (
|
|
25
|
-
[CustomFieldDefinitionType.BOOLEAN]: (value) => (
|
|
26
|
-
[CustomFieldDefinitionType.DATE]: (value) =>
|
|
27
|
-
[CustomFieldDefinitionType.DATETIME]: (value) =>
|
|
28
|
-
[CustomFieldDefinitionType.IMAGE]: (value) =>
|
|
23
|
+
[CustomFieldDefinitionType.TEXT]: (value) => Joi.string().validate(value),
|
|
24
|
+
[CustomFieldDefinitionType.NUMBER]: (value) => Joi.number().strict(true).validate(value),
|
|
25
|
+
[CustomFieldDefinitionType.BOOLEAN]: (value) => Joi.boolean().strict().validate(value),
|
|
26
|
+
[CustomFieldDefinitionType.DATE]: (value) => Joi.date().validate(value),
|
|
27
|
+
[CustomFieldDefinitionType.DATETIME]: (value) => Joi.date().validate(value),
|
|
28
|
+
[CustomFieldDefinitionType.IMAGE]: (value) => Joi.array().min(1).unique()
|
|
29
29
|
.items(Joi.string().uri())
|
|
30
|
-
.validate(value)
|
|
31
|
-
[CustomFieldDefinitionType.FILE]: (value) =>
|
|
30
|
+
.validate(value),
|
|
31
|
+
[CustomFieldDefinitionType.FILE]: (value) => Joi.array().min(1).unique().items(Joi.object({
|
|
32
32
|
name: Joi.string().required(),
|
|
33
33
|
type: Joi.string(),
|
|
34
34
|
size: Joi.string(),
|
|
35
35
|
addedBy: Joi.string().uuid(),
|
|
36
36
|
}))
|
|
37
|
-
.validate(value)
|
|
37
|
+
.validate(value),
|
|
38
38
|
};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import Joi from 'joi';
|
|
1
2
|
import type { Validator } from '../type';
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -6,6 +7,6 @@ import type { Validator } from '../type';
|
|
|
6
7
|
export const validateSelect: Validator<string, string[]> = (
|
|
7
8
|
value,
|
|
8
9
|
selectValues,
|
|
9
|
-
) => (
|
|
10
|
-
|
|
10
|
+
) => (
|
|
11
|
+
Joi.string().allow(null).valid(...selectValues).validate(value)
|
|
11
12
|
);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import Joi from 'joi';
|
|
1
2
|
import type { Validator } from '../type';
|
|
2
3
|
|
|
3
4
|
type StatusColor = string | null; // TODO: Takes from @autofleet/colors ?
|
|
@@ -13,6 +14,9 @@ type StatusOption = {
|
|
|
13
14
|
export const validateStatus: Validator<StatusValue, StatusOption[]> = (
|
|
14
15
|
value,
|
|
15
16
|
statusValues,
|
|
16
|
-
) => (
|
|
17
|
-
|
|
17
|
+
) => (
|
|
18
|
+
Joi.string()
|
|
19
|
+
.allow(null)
|
|
20
|
+
.valid(...statusValues.map((statusValue) => statusValue.value))
|
|
21
|
+
.validate(value)
|
|
18
22
|
);
|