@autofleet/sadot 0.7.10-beta.0 → 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/package.json +1 -1
- package/src/api/v1/definition/validations.ts +3 -3
- package/src/errors/index.ts +15 -2
- package/src/models/CustomFieldDefinition.ts +1 -1
- package/src/models/CustomFieldValue.ts +3 -4
- package/src/utils/validations/validators/index.ts +1 -2
- package/src/utils/validations/validators/status.validator.ts +4 -1
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[]) {
|
|
@@ -34,8 +35,20 @@ export class InvalidFieldTypeError extends BadRequest {
|
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
export class InvalidValueError extends BadRequest {
|
|
37
|
-
constructor(value: any, fieldDefinitionName: string,
|
|
38
|
-
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);
|
|
39
52
|
super([err], null, null);
|
|
40
53
|
this.message = err.message;
|
|
41
54
|
}
|
|
@@ -137,7 +137,7 @@ class CustomFieldDefinition extends Model {
|
|
|
137
137
|
if (![null, undefined].includes(instance.defaultValue)) {
|
|
138
138
|
const isValid = validateValue(instance.defaultValue, instance.fieldType, instance.validation);
|
|
139
139
|
if (isValid.error) {
|
|
140
|
-
throw new InvalidValueError(instance.defaultValue, instance.
|
|
140
|
+
throw new InvalidValueError(instance.defaultValue, instance.name, isValid.error);
|
|
141
141
|
}
|
|
142
142
|
}
|
|
143
143
|
}
|
|
@@ -61,16 +61,15 @@ class CustomFieldValue extends Model {
|
|
|
61
61
|
definition: CustomFieldDefinition,
|
|
62
62
|
): void {
|
|
63
63
|
const { validation, fieldType, displayName } = definition;
|
|
64
|
-
const isValidFieldType = validateFieldType(fieldType);
|
|
64
|
+
const isValidFieldType = validateFieldType(fieldType);
|
|
65
65
|
if (!isValidFieldType) {
|
|
66
66
|
throw new InvalidFieldTypeError(fieldType);
|
|
67
67
|
}
|
|
68
68
|
// Always allow null values
|
|
69
|
-
if (instance.value === null) return;
|
|
69
|
+
if (instance.value === null) return;
|
|
70
70
|
const validateValueResponse = validateValue(instance.value, fieldType, validation);
|
|
71
71
|
if (validateValueResponse.error) {
|
|
72
|
-
|
|
73
|
-
throw new InvalidValueError(instance.value, displayName, validationErrorMessage);
|
|
72
|
+
throw new InvalidValueError(instance.value, displayName, validateValueResponse.error);
|
|
74
73
|
}
|
|
75
74
|
}
|
|
76
75
|
|
|
@@ -18,12 +18,11 @@ export const CustomValidationTypes = {
|
|
|
18
18
|
* Validators for custom fields
|
|
19
19
|
*/
|
|
20
20
|
export const validators: Validators = {
|
|
21
|
-
// currently the name shows up as "value", i want to change it to be based on the type
|
|
22
21
|
[CustomFieldDefinitionType.SELECT]: validateSelect,
|
|
23
22
|
[CustomFieldDefinitionType.STATUS]: validateStatus,
|
|
24
23
|
[CustomFieldDefinitionType.TEXT]: (value) => Joi.string().validate(value),
|
|
25
24
|
[CustomFieldDefinitionType.NUMBER]: (value) => Joi.number().strict(true).validate(value),
|
|
26
|
-
[CustomFieldDefinitionType.BOOLEAN]: (value) => Joi.boolean().validate(value),
|
|
25
|
+
[CustomFieldDefinitionType.BOOLEAN]: (value) => Joi.boolean().strict().validate(value),
|
|
27
26
|
[CustomFieldDefinitionType.DATE]: (value) => Joi.date().validate(value),
|
|
28
27
|
[CustomFieldDefinitionType.DATETIME]: (value) => Joi.date().validate(value),
|
|
29
28
|
[CustomFieldDefinitionType.IMAGE]: (value) => Joi.array().min(1).unique()
|
|
@@ -15,5 +15,8 @@ export const validateStatus: Validator<StatusValue, StatusOption[]> = (
|
|
|
15
15
|
value,
|
|
16
16
|
statusValues,
|
|
17
17
|
) => (
|
|
18
|
-
Joi.string()
|
|
18
|
+
Joi.string()
|
|
19
|
+
.allow(null)
|
|
20
|
+
.valid(...statusValues.map((statusValue) => statusValue.value))
|
|
21
|
+
.validate(value)
|
|
19
22
|
);
|