Skip to content

Commit

Permalink
feat(validation) #71: improve constraint violation readability
Browse files Browse the repository at this point in the history
  • Loading branch information
lilgallon committed Jan 18, 2024
1 parent 34b9f03 commit 4c70af6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
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

0 comments on commit 4c70af6

Please sign in to comment.