Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(validation) #71: improve constraint violation readability #72

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,45 @@ fun <T> validate(fn: ValidationContext.() -> T) = with(ValidationContext()) {
}
}

fun ValidationContext.validateLength(propertyName: String, property: String, maxLength: Int) {
if (property.length > maxLength) {
fun ValidationContext.validateLength(property: String, value: String, maxLength: Int) {
if (value.length > maxLength) {
violations.add(
DefaultConstraintViolation(
property = property,
constraint = Greater(maxLength)
value = value,
constraint = MaxLengthContraint(maxLength)
)
)
}
}

fun ValidationContext.validateDates(fromPropertyName: String, from: Instant, toPropertyName: String, to: Instant) {
if (from.isAfter(to)) {
fun ValidationContext.validateDates(fromProperty: String, fromValue: Instant, toProperty: String, toValue: Instant) {
if (fromValue.isAfter(toValue)) {
violations.add(
DefaultConstraintViolation(
property = "from",
constraint = Greater(to)
property = fromProperty,
value = fromValue,
constraint = IsAfterContraint(toProperty, toValue)
)
)
}
}

fun ValidationContext.validateInt(propertyName: String, property: Int, min: Int?, max: Int?) {
if (min != null && property < min) {
fun ValidationContext.validateInt(property: String, value: Int, min: Int?, max: Int?) {
if (min != null && value < min) {
violations.add(
DefaultConstraintViolation(
property = "value",
property = property,
value = value,
constraint = Less(min)
)
)
} else if (max != null && property > max) {
} else if (max != null && value > max) {
violations.add(
DefaultConstraintViolation(
property = "value",
constraint = Greater(min)
property = property,
value = value,
constraint = Greater(max)
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import com.izivia.ocpi.toolkit.common.CiString
import org.valiktor.Constraint
import org.valiktor.ConstraintViolationException
import org.valiktor.Validator
import org.valiktor.i18n.toMessage
import java.net.URL
import java.time.Instant
import java.util.*

fun ConstraintViolationException.toReadableString(): String = constraintViolations
.map { it.toMessage(baseName = "messages", locale = Locale.ENGLISH) }
.joinToString(",") { "${it.property}: ${it.message}" }
fun ConstraintViolationException.toReadableString(): String =
constraintViolations.joinToString(", ") {
"${it.constraint} violation on ${it.property}=${it.value}"
}

class PrintableAsciiConstraint : Constraint
class PrintableUtf8Constraint : Constraint
class MaxLengthContraint(val length: Int) : Constraint
data class MaxLengthContraint(val length: Int) : Constraint
data class IsAfterContraint(val property: String, val instant: Instant) : Constraint
class CountryCodeConstraint : Constraint
class CurrencyCodeConstraint : Constraint
class UrlConstraint : Constraint
Expand Down
Loading