refactor: rename errors to validations (#2105)

* Update validation error unpacking

* Rename validations on backend

* Format
This commit is contained in:
Presley Pizzo
2022-06-07 09:31:15 -04:00
committed by GitHub
parent 3616c629c7
commit 6d966963da
6 changed files with 17 additions and 10 deletions
+1 -1
View File
@@ -67,7 +67,7 @@ type Response struct {
// Validations are form field-specific friendly error messages. They will be
// shown on a form field in the UI. These can also be used to add additional
// context if there is a set of errors in the primary 'Message'.
Validations []Error `json:"errors,omitempty"`
Validations []Error `json:"validations,omitempty"`
}
// Error represents a scoped error to a user input.
+1 -1
View File
@@ -29,7 +29,7 @@ describe("mapApiErrorToFieldErrors", () => {
expect(
mapApiErrorToFieldErrors({
message: "Invalid entry",
errors: [{ detail: "Username is already in use", field: "username" }],
validations: [{ detail: "Username is already in use", field: "username" }],
}),
).toEqual({
username: "Username is already in use",
+5 -4
View File
@@ -15,7 +15,8 @@ export type FieldErrors = Record<FieldError["field"], FieldError["detail"]>
export interface ApiErrorResponse {
message: string
errors?: FieldError[]
detail?: string
validations?: FieldError[]
}
export type ApiError = AxiosError<ApiErrorResponse> & { response: AxiosResponse<ApiErrorResponse> }
@@ -39,13 +40,13 @@ export const isApiError = (err: any): err is ApiError => {
* @param error ApiError
* @returns true if the ApiError contains error messages for specific form fields.
*/
export const hasApiFieldErrors = (error: ApiError): boolean => Array.isArray(error.response.data.errors)
export const hasApiFieldErrors = (error: ApiError): boolean => Array.isArray(error.response.data.validations)
export const mapApiErrorToFieldErrors = (apiErrorResponse: ApiErrorResponse): FieldErrors => {
const result: FieldErrors = {}
if (apiErrorResponse.errors) {
for (const error of apiErrorResponse.errors) {
if (apiErrorResponse.validations) {
for (const error of apiErrorResponse.validations) {
result[error.field] = error.detail || Language.errorsByCode.defaultErrorCode
}
}
@@ -54,7 +54,10 @@ describe("AccountPage", () => {
jest.spyOn(API, "updateProfile").mockRejectedValueOnce({
isAxiosError: true,
response: {
data: { message: "Invalid profile", errors: [{ detail: "Username is already in use", field: "username" }] },
data: {
message: "Invalid profile",
validations: [{ detail: "Username is already in use", field: "username" }],
},
},
})
@@ -49,7 +49,10 @@ describe("SecurityPage", () => {
jest.spyOn(API, "updateUserPassword").mockRejectedValueOnce({
isAxiosError: true,
response: {
data: { message: "Incorrect password.", errors: [{ detail: "Incorrect password.", field: "old_password" }] },
data: {
message: "Incorrect password.",
validations: [{ detail: "Incorrect password.", field: "old_password" }],
},
},
})
@@ -68,7 +71,7 @@ describe("SecurityPage", () => {
jest.spyOn(API, "updateUserPassword").mockRejectedValueOnce({
isAxiosError: true,
response: {
data: { message: "Invalid password.", errors: [{ detail: "Invalid password.", field: "password" }] },
data: { message: "Invalid password.", validations: [{ detail: "Invalid password.", field: "password" }] },
},
})
@@ -58,7 +58,7 @@ describe("Create User Page", () => {
ctx.status(400),
ctx.json({
message: "invalid field",
errors: [
validations: [
{
detail: fieldErrorMessage,
field: "username",