@autofleet/sadot 0.7.10-beta.0 → 0.7.10-beta.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/dist/api/v1/definition/validations.js +3 -3
- package/dist/errors/index.d.ts +2 -1
- package/dist/errors/index.js +11 -2
- package/dist/models/CustomFieldDefinition.js +1 -1
- package/dist/models/CustomFieldValue.js +3 -4
- package/dist/utils/validations/validators/index.js +1 -2
- package/dist/utils/validations/validators/status.validator.js +4 -1
- 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
|
@@ -40,11 +40,11 @@ const DefaultValueSchema = joi_1.default.when('fieldType', {
|
|
|
40
40
|
{ is: constants_1.CustomFieldDefinitionType.BOOLEAN, then: joi_1.default.boolean().allow(null) },
|
|
41
41
|
{ is: constants_1.CustomFieldDefinitionType.DATE, then: joi_1.default.date().allow(null) },
|
|
42
42
|
{ is: constants_1.CustomFieldDefinitionType.DATETIME, then: joi_1.default.date().allow(null) },
|
|
43
|
-
{ is: constants_1.CustomFieldDefinitionType.FILE, then: FileValidationSchema },
|
|
44
|
-
{ is: constants_1.CustomFieldDefinitionType.IMAGE, then: joi_1.default.string().uri().allow(null) },
|
|
43
|
+
{ is: constants_1.CustomFieldDefinitionType.FILE, then: joi_1.default.array().items(FileValidationSchema).allow(null) },
|
|
44
|
+
{ is: constants_1.CustomFieldDefinitionType.IMAGE, then: joi_1.default.array().items(joi_1.default.string().uri()).allow(null) },
|
|
45
45
|
{ is: constants_1.CustomFieldDefinitionType.NUMBER, then: joi_1.default.number().allow(null) },
|
|
46
46
|
{ is: constants_1.CustomFieldDefinitionType.SELECT, then: joi_1.default.string().allow(null) },
|
|
47
|
-
{ is: constants_1.CustomFieldDefinitionType.STATUS, then:
|
|
47
|
+
{ is: constants_1.CustomFieldDefinitionType.STATUS, then: joi_1.default.string().allow(null) },
|
|
48
48
|
{ is: constants_1.CustomFieldDefinitionType.TEXT, then: joi_1.default.string().allow(null) },
|
|
49
49
|
],
|
|
50
50
|
});
|
package/dist/errors/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { BadRequest } from '@autofleet/errors';
|
|
2
|
+
import type { ValidationError } from 'joi';
|
|
2
3
|
export declare class MissingRequiredCustomFieldError extends BadRequest {
|
|
3
4
|
constructor(missingFields: string[]);
|
|
4
5
|
}
|
|
@@ -12,7 +13,7 @@ export declare class InvalidFieldTypeError extends BadRequest {
|
|
|
12
13
|
constructor(fieldType: string);
|
|
13
14
|
}
|
|
14
15
|
export declare class InvalidValueError extends BadRequest {
|
|
15
|
-
constructor(value: any, fieldDefinitionName: string,
|
|
16
|
+
constructor(value: any, fieldDefinitionName: string, joiValidationError: ValidationError);
|
|
16
17
|
}
|
|
17
18
|
export declare class MissingDefinitionError extends BadRequest {
|
|
18
19
|
constructor(fieldNames: string[]);
|
package/dist/errors/index.js
CHANGED
|
@@ -36,8 +36,17 @@ class InvalidFieldTypeError extends errors_1.BadRequest {
|
|
|
36
36
|
}
|
|
37
37
|
exports.InvalidFieldTypeError = InvalidFieldTypeError;
|
|
38
38
|
class InvalidValueError extends errors_1.BadRequest {
|
|
39
|
-
constructor(value, fieldDefinitionName,
|
|
40
|
-
const
|
|
39
|
+
constructor(value, fieldDefinitionName, joiValidationError) {
|
|
40
|
+
const formattedErrorMessage = joiValidationError.message
|
|
41
|
+
.replace(/"/g, '')
|
|
42
|
+
.replace('value', fieldDefinitionName);
|
|
43
|
+
const formattedValue = typeof value === 'object' ? JSON.stringify(value) : value;
|
|
44
|
+
const detailedMessage = `
|
|
45
|
+
Invalid value for field ${fieldDefinitionName}
|
|
46
|
+
Received: ${formattedValue}
|
|
47
|
+
Error: ${formattedErrorMessage}
|
|
48
|
+
`.trim();
|
|
49
|
+
const err = new Error(detailedMessage);
|
|
41
50
|
super([err], null, null);
|
|
42
51
|
this.message = err.message;
|
|
43
52
|
}
|
|
@@ -29,7 +29,7 @@ let CustomFieldDefinition = class CustomFieldDefinition extends sequelize_typesc
|
|
|
29
29
|
if (![null, undefined].includes(instance.defaultValue)) {
|
|
30
30
|
const isValid = (0, validations_1.validateValue)(instance.defaultValue, instance.fieldType, instance.validation);
|
|
31
31
|
if (isValid.error) {
|
|
32
|
-
throw new errors_1.InvalidValueError(instance.defaultValue, instance.
|
|
32
|
+
throw new errors_1.InvalidValueError(instance.defaultValue, instance.name, isValid.error);
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
}
|
|
@@ -41,17 +41,16 @@ const errors_1 = require("../errors");
|
|
|
41
41
|
let CustomFieldValue = class CustomFieldValue extends sequelize_typescript_1.Model {
|
|
42
42
|
static validateValueAgainstDefinition(instance, definition) {
|
|
43
43
|
const { validation, fieldType, displayName } = definition;
|
|
44
|
-
const isValidFieldType = (0, validations_1.validateFieldType)(fieldType);
|
|
44
|
+
const isValidFieldType = (0, validations_1.validateFieldType)(fieldType);
|
|
45
45
|
if (!isValidFieldType) {
|
|
46
46
|
throw new errors_1.InvalidFieldTypeError(fieldType);
|
|
47
47
|
}
|
|
48
48
|
// Always allow null values
|
|
49
49
|
if (instance.value === null)
|
|
50
|
-
return;
|
|
50
|
+
return;
|
|
51
51
|
const validateValueResponse = (0, validations_1.validateValue)(instance.value, fieldType, validation);
|
|
52
52
|
if (validateValueResponse.error) {
|
|
53
|
-
|
|
54
|
-
throw new errors_1.InvalidValueError(instance.value, displayName, validationErrorMessage);
|
|
53
|
+
throw new errors_1.InvalidValueError(instance.value, displayName, validateValueResponse.error);
|
|
55
54
|
}
|
|
56
55
|
}
|
|
57
56
|
static async validateCustomFieldValues(instances) {
|
|
@@ -20,12 +20,11 @@ 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
|
|
24
23
|
[constants_1.CustomFieldDefinitionType.SELECT]: select_validator_1.validateSelect,
|
|
25
24
|
[constants_1.CustomFieldDefinitionType.STATUS]: status_validator_1.validateStatus,
|
|
26
25
|
[constants_1.CustomFieldDefinitionType.TEXT]: (value) => joi_1.default.string().validate(value),
|
|
27
26
|
[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),
|
|
27
|
+
[constants_1.CustomFieldDefinitionType.BOOLEAN]: (value) => joi_1.default.boolean().strict().validate(value),
|
|
29
28
|
[constants_1.CustomFieldDefinitionType.DATE]: (value) => joi_1.default.date().validate(value),
|
|
30
29
|
[constants_1.CustomFieldDefinitionType.DATETIME]: (value) => joi_1.default.date().validate(value),
|
|
31
30
|
[constants_1.CustomFieldDefinitionType.IMAGE]: (value) => joi_1.default.array().min(1).unique()
|
|
@@ -8,5 +8,8 @@ const joi_1 = __importDefault(require("joi"));
|
|
|
8
8
|
/**
|
|
9
9
|
* Validate that the value is one of the status values
|
|
10
10
|
*/
|
|
11
|
-
const validateStatus = (value, statusValues) => (joi_1.default.string()
|
|
11
|
+
const validateStatus = (value, statusValues) => (joi_1.default.string()
|
|
12
|
+
.allow(null)
|
|
13
|
+
.valid(...statusValues.map((statusValue) => statusValue.value))
|
|
14
|
+
.validate(value));
|
|
12
15
|
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[]) {
|
|
@@ -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
|
);
|