@autofleet/sadot 0.7.10-beta.1 → 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
|
@@ -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;
|