@autofleet/sadot 0.13.5-beta-1 → 0.13.5-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/hooks/hooks.js +18 -2
- package/package.json +2 -1
- package/src/hooks/hooks.ts +37 -2
package/dist/hooks/hooks.js
CHANGED
|
@@ -31,6 +31,7 @@ const ajv_1 = __importDefault(require("ajv"));
|
|
|
31
31
|
const joi_1 = __importDefault(require("joi"));
|
|
32
32
|
const ajv_formats_1 = __importDefault(require("ajv-formats"));
|
|
33
33
|
const errors_1 = require("@autofleet/errors");
|
|
34
|
+
const ajv_errors_1 = __importDefault(require("ajv-errors"));
|
|
34
35
|
const logger_1 = __importDefault(require("../utils/logger"));
|
|
35
36
|
const ValidatorRepo = __importStar(require("../repository/validator"));
|
|
36
37
|
const DefinitionRepo = __importStar(require("../repository/definition"));
|
|
@@ -48,6 +49,7 @@ const ajv = new ajv_1.default({
|
|
|
48
49
|
$data: true, // Enable $data references
|
|
49
50
|
});
|
|
50
51
|
(0, ajv_formats_1.default)(ajv);
|
|
52
|
+
(0, ajv_errors_1.default)(ajv);
|
|
51
53
|
/**
|
|
52
54
|
* Helper function to manually copy object properties
|
|
53
55
|
* This is more efficient for large objects and avoids excessive object creation
|
|
@@ -88,6 +90,18 @@ const getCompleteCustomFields = async (instance, options) => {
|
|
|
88
90
|
}
|
|
89
91
|
return instance.customFields || {};
|
|
90
92
|
};
|
|
93
|
+
const formatAjvErrors = (errors) => errors.reduce((acc, err) => {
|
|
94
|
+
const basePath = (err.instancePath || '')
|
|
95
|
+
.split('/')
|
|
96
|
+
.filter(Boolean)
|
|
97
|
+
.join('.')
|
|
98
|
+
.replace(/^after\./, '');
|
|
99
|
+
const missingProp = err.keyword === 'required' ? `.${err.params?.missingProperty}` : '';
|
|
100
|
+
const key = (basePath + missingProp).replace(/^\./, '') || 'root';
|
|
101
|
+
const message = err.message || 'Invalid value';
|
|
102
|
+
acc[key] = message;
|
|
103
|
+
return acc;
|
|
104
|
+
}, {});
|
|
91
105
|
/**
|
|
92
106
|
* Validates the model using custom validators
|
|
93
107
|
*/
|
|
@@ -176,7 +190,8 @@ const validateModel = async (instance, options, scopeAttributes, modelOptions =
|
|
|
176
190
|
})));
|
|
177
191
|
if (!isValid) {
|
|
178
192
|
const errorDetails = validateSchema.errors?.map((err) => `${err.instancePath || ''} ${err.message || 'Invalid value'}`).join(', ');
|
|
179
|
-
|
|
193
|
+
const formattedErrors = formatAjvErrors(validateSchema.errors);
|
|
194
|
+
throw new errors_1.BadRequest([new Error(`Validation failed for ${modelType}: ${errorDetails}`)], undefined, formattedErrors);
|
|
180
195
|
}
|
|
181
196
|
}
|
|
182
197
|
}
|
|
@@ -205,7 +220,8 @@ const validateModel = async (instance, options, scopeAttributes, modelOptions =
|
|
|
205
220
|
const errorDetails = validateSchema
|
|
206
221
|
.errors
|
|
207
222
|
?.map((err) => `${err.instancePath || ''} ${err.message || 'Invalid value'}`).join(', ');
|
|
208
|
-
|
|
223
|
+
const formattedErrors = formatAjvErrors(validateSchema.errors);
|
|
224
|
+
throw new errors_1.BadRequest([new Error(`Validation failed for ${modelType}: ${errorDetails}`)], undefined, formattedErrors);
|
|
209
225
|
}
|
|
210
226
|
}
|
|
211
227
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/sadot",
|
|
3
|
-
"version": "0.13.5-beta-
|
|
3
|
+
"version": "0.13.5-beta-2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
"@autofleet/common-types": "^4.4.0",
|
|
32
32
|
"@autofleet/events": "^4.0.0",
|
|
33
33
|
"ajv": "^8.12.0",
|
|
34
|
+
"ajv-errors": "^3.0.0",
|
|
34
35
|
"ajv-formats": "^3.0.1",
|
|
35
36
|
"http-status-codes": "^2.3.0",
|
|
36
37
|
"joi": "^17.7.0",
|
package/src/hooks/hooks.ts
CHANGED
|
@@ -3,6 +3,7 @@ import Ajv from 'ajv';
|
|
|
3
3
|
import Joi from 'joi';
|
|
4
4
|
import addFormats from 'ajv-formats';
|
|
5
5
|
import { BadRequest } from '@autofleet/errors';
|
|
6
|
+
import ajvErrors from 'ajv-errors';
|
|
6
7
|
import logger from '../utils/logger';
|
|
7
8
|
import * as ValidatorRepo from '../repository/validator';
|
|
8
9
|
import * as DefinitionRepo from '../repository/definition';
|
|
@@ -25,6 +26,7 @@ const ajv = new Ajv({
|
|
|
25
26
|
});
|
|
26
27
|
|
|
27
28
|
addFormats(ajv);
|
|
29
|
+
ajvErrors(ajv);
|
|
28
30
|
|
|
29
31
|
/**
|
|
30
32
|
* Helper function to manually copy object properties
|
|
@@ -76,6 +78,29 @@ const getCompleteCustomFields = async (instance, options): Promise<Record<string
|
|
|
76
78
|
return instance.customFields || {};
|
|
77
79
|
};
|
|
78
80
|
|
|
81
|
+
const formatAjvErrors = (
|
|
82
|
+
errors: {
|
|
83
|
+
instancePath?: string;
|
|
84
|
+
keyword: string;
|
|
85
|
+
message?: string;
|
|
86
|
+
params?: Record<string, any>;
|
|
87
|
+
}[],
|
|
88
|
+
): Record<string, string> => errors.reduce((acc, err) => {
|
|
89
|
+
const basePath = (err.instancePath || '')
|
|
90
|
+
.split('/')
|
|
91
|
+
.filter(Boolean)
|
|
92
|
+
.join('.')
|
|
93
|
+
.replace(/^after\./, '');
|
|
94
|
+
|
|
95
|
+
const missingProp = err.keyword === 'required' ? `.${err.params?.missingProperty}` : '';
|
|
96
|
+
const key = (basePath + missingProp).replace(/^\./, '') || 'root';
|
|
97
|
+
|
|
98
|
+
const message = err.message || 'Invalid value';
|
|
99
|
+
acc[key] = message;
|
|
100
|
+
|
|
101
|
+
return acc;
|
|
102
|
+
}, {} as Record<string, string>);
|
|
103
|
+
|
|
79
104
|
/**
|
|
80
105
|
* Validates the model using custom validators
|
|
81
106
|
*/
|
|
@@ -193,7 +218,12 @@ const validateModel = async (
|
|
|
193
218
|
const errorDetails = validateSchema.errors?.map((err) =>
|
|
194
219
|
`${(err as any).instancePath || ''} ${(err as any).message || 'Invalid value'}`).join(', ');
|
|
195
220
|
|
|
196
|
-
|
|
221
|
+
const formattedErrors = formatAjvErrors(validateSchema.errors);
|
|
222
|
+
throw new BadRequest(
|
|
223
|
+
[new Error(`Validation failed for ${modelType}: ${errorDetails}`)],
|
|
224
|
+
undefined,
|
|
225
|
+
formattedErrors,
|
|
226
|
+
);
|
|
197
227
|
}
|
|
198
228
|
}
|
|
199
229
|
} else {
|
|
@@ -228,7 +258,12 @@ const validateModel = async (
|
|
|
228
258
|
.errors
|
|
229
259
|
?.map((err) => `${(err as any).instancePath || ''} ${(err as any).message || 'Invalid value'}`).join(', ');
|
|
230
260
|
|
|
231
|
-
|
|
261
|
+
const formattedErrors = formatAjvErrors(validateSchema.errors);
|
|
262
|
+
throw new BadRequest(
|
|
263
|
+
[new Error(`Validation failed for ${modelType}: ${errorDetails}`)],
|
|
264
|
+
undefined,
|
|
265
|
+
formattedErrors,
|
|
266
|
+
);
|
|
232
267
|
}
|
|
233
268
|
}
|
|
234
269
|
}
|