Skip to content

Commit 220e55c

Browse files
committed
fix: Expose all fields for validation errors
1 parent 46f3f63 commit 220e55c

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

packages/api/src/errors.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import Elysia from "elysia";
22
import type { ValidationError } from "elysia";
33

4+
interface ValidationField {
5+
path: string;
6+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7+
value: any;
8+
message: string;
9+
}
10+
411
interface ApiErrorCode {
512
ERR_UNKNOWN: never;
613
ERR_VALIDATION: {
7-
path: string;
8-
fail: string;
14+
fields: ValidationField[];
915
};
1016
ERR_UNAUTHORIZED: never;
1117
ERR_NOT_FOUND: never;
@@ -64,11 +70,13 @@ export const errors = () =>
6470
}
6571

6672
if (code === "VALIDATION") {
67-
return mapValidationError(error);
73+
try {
74+
return mapValidationError(error);
75+
} catch {
76+
// Mapping validation failed, continue.
77+
}
6878
}
6979

70-
console.error(error);
71-
7280
set.status = 500;
7381
return {
7482
type: "ERR_UNKNOWN",
@@ -78,17 +86,17 @@ export const errors = () =>
7886
function mapValidationError(
7987
error: ValidationError,
8088
): 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+
});
8897
}
8998
return {
9099
type: "ERR_VALIDATION",
91-
path: first.path,
92-
fail: first.message,
100+
fields,
93101
};
94102
}

0 commit comments

Comments
 (0)