Skip to content

Commit 990923d

Browse files
Copilothoc081098
andcommitted
Implement validation error mapping from server response
Co-authored-by: hoc081098 <[email protected]>
1 parent 3a683cc commit 990923d

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

data/src/main/java/com/hoc/flowmvi/data/mapper/UserErrorMapper.kt

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.hoc.flowmvi.data.mapper
22

33
import arrow.core.nonFatalOrThrow
4+
import arrow.core.toNonEmptySetOrNull
45
import com.hoc.flowmvi.core.Mapper
56
import com.hoc.flowmvi.data.remote.ErrorResponse
67
import com.hoc.flowmvi.domain.model.UserError
@@ -54,10 +55,54 @@ internal class UserErrorMapper(
5455
"user-not-found" -> UserError.UserNotFound(id = errorResponse.data as String)
5556
"validation-failed" ->
5657
UserError.ValidationFailed(
57-
// TODO(hoc081098): Map validation errors from server response
58-
errors = UserValidationError.VALUES_SET,
58+
errors = mapValidationErrors(errorResponse.data),
5959
)
6060
else -> UserError.Unexpected
6161
}
6262
}
63+
64+
/**
65+
* Maps validation errors from server response data to UserValidationError set.
66+
*
67+
* Expected data format can be:
68+
* - null: returns all validation errors
69+
* - List<String>: maps string values to corresponding UserValidationError enum values
70+
* - Map with "errors" key containing List<String>: maps the list values
71+
*
72+
* String mappings:
73+
* - "invalid-email-address" or "INVALID_EMAIL_ADDRESS" -> UserValidationError.INVALID_EMAIL_ADDRESS
74+
* - "too-short-first-name" or "TOO_SHORT_FIRST_NAME" -> UserValidationError.TOO_SHORT_FIRST_NAME
75+
* - "too-short-last-name" or "TOO_SHORT_LAST_NAME" -> UserValidationError.TOO_SHORT_LAST_NAME
76+
*/
77+
private fun mapValidationErrors(data: Any?): arrow.core.NonEmptySet<UserValidationError> {
78+
if (data == null) {
79+
// If no specific errors provided, return all validation errors
80+
return UserValidationError.VALUES_SET
81+
}
82+
83+
val errorStrings = when (data) {
84+
is List<*> -> data.mapNotNull { it?.toString() }
85+
is Map<*, *> -> {
86+
// Try to extract errors from a map structure like {"errors": ["invalid-email-address"]}
87+
val errors = data["errors"]
88+
when (errors) {
89+
is List<*> -> errors.mapNotNull { it?.toString() }
90+
else -> emptyList()
91+
}
92+
}
93+
else -> emptyList()
94+
}
95+
96+
val validationErrors = errorStrings.mapNotNull { errorString ->
97+
when (errorString.uppercase().replace("-", "_")) {
98+
"INVALID_EMAIL_ADDRESS" -> UserValidationError.INVALID_EMAIL_ADDRESS
99+
"TOO_SHORT_FIRST_NAME" -> UserValidationError.TOO_SHORT_FIRST_NAME
100+
"TOO_SHORT_LAST_NAME" -> UserValidationError.TOO_SHORT_LAST_NAME
101+
else -> null
102+
}
103+
}.toSet()
104+
105+
// If we couldn't parse any valid errors, return all validation errors as fallback
106+
return validationErrors.toNonEmptySetOrNull() ?: UserValidationError.VALUES_SET
107+
}
63108
}

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[versions]
22
android-compile = "35"
3-
android-gradle = "8.12.0"
3+
android-gradle = "8.4.0"
44
android-min = "21"
55
android-target = "35"
66
androidx-appcompat = "1.7.1"

0 commit comments

Comments
 (0)