/
util.ts
49 lines (45 loc) 路 1.1 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import ono from 'ono';
export function extractContentType(req) {
let contentType = req.headers['content-type'] || 'not_provided';
let end = contentType.indexOf(';');
end = end === -1 ? contentType.length : end;
if (contentType) {
return contentType.substring(0, end);
}
return contentType;
}
const _validationError = (
status: number,
path: string,
message: string,
errors?: any, // TODO rename - normalize...something else
) => ({
status,
errors: [
{
path,
message,
...({ errors } || {}),
},
],
});
export function validationError(status, path, message) {
const err = _validationError(status, path, message);
return ono(err, message);
}
export function ajvErrorsToValidatorError(status, errors) {
return {
status,
errors: errors.map(e => {
const required =
e.params &&
e.params.missingProperty &&
e.dataPath + '.' + e.params.missingProperty;
return {
path: required || e.dataPath || e.schemaPath,
message: e.message,
errorCode: `${e.keyword}.openapi.validation`,
};
}),
};
}