File tree Expand file tree Collapse file tree 1 file changed +22
-14
lines changed Expand file tree Collapse file tree 1 file changed +22
-14
lines changed Original file line number Diff line number Diff line change 1
1
import Elysia from "elysia" ;
2
2
import type { ValidationError } from "elysia" ;
3
3
4
+ interface ValidationField {
5
+ path : string ;
6
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
7
+ value : any ;
8
+ message : string ;
9
+ }
10
+
4
11
interface ApiErrorCode {
5
12
ERR_UNKNOWN : never ;
6
13
ERR_VALIDATION : {
7
- path : string ;
8
- fail : string ;
14
+ fields : ValidationField [ ] ;
9
15
} ;
10
16
ERR_UNAUTHORIZED : never ;
11
17
ERR_NOT_FOUND : never ;
@@ -64,11 +70,13 @@ export const errors = () =>
64
70
}
65
71
66
72
if ( code === "VALIDATION" ) {
67
- return mapValidationError ( error ) ;
73
+ try {
74
+ return mapValidationError ( error ) ;
75
+ } catch {
76
+ // Mapping validation failed, continue.
77
+ }
68
78
}
69
79
70
- console . error ( error ) ;
71
-
72
80
set . status = 500 ;
73
81
return {
74
82
type : "ERR_UNKNOWN" ,
@@ -78,17 +86,17 @@ export const errors = () =>
78
86
function mapValidationError (
79
87
error : ValidationError ,
80
88
) : ApiError < "ERR_VALIDATION" > {
81
- const first = error . validator ?. Errors ( error . value ) . First ( ) ;
82
- if ( ! first ) {
83
- return {
84
- type : "ERR_VALIDATION" ,
85
- path : "/" ,
86
- fail : error . message ,
87
- } ;
89
+ const fields : ValidationField [ ] = [ ] ;
90
+ const iterator = error . validator . Errors ( error . value ) ;
91
+ for ( const error of iterator ) {
92
+ fields . push ( {
93
+ path : error . path ,
94
+ value : error . value ,
95
+ message : error . message ,
96
+ } ) ;
88
97
}
89
98
return {
90
99
type : "ERR_VALIDATION" ,
91
- path : first . path ,
92
- fail : first . message ,
100
+ fields,
93
101
} ;
94
102
}
You can’t perform that action at this time.
0 commit comments