Skip to content

Instantly share code, notes, and snippets.

@1gravity
Last active April 8, 2021 13:25
Show Gist options
  • Save 1gravity/c7adc05a714b2cb2bb5ae2f28800bf1d to your computer and use it in GitHub Desktop.
Save 1gravity/c7adc05a714b2cb2bb5ae2f28800bf1d to your computer and use it in GitHub Desktop.
val uuidRegex = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$".toRegex()
data class Account(
var accountUUID: String,
var createdAt: Instant,
var modifiedAt: Instant,
var status: AccountStatus
) {
init {
Validation<Account> {
Account::accountUUID {
pattern(uuidRegex)
}
Account::createdAt required { }
Account::modifiedAt required { }
Account::status required { }
}.validateAndThrowOnFailure(this)
}
}
fun <T> Validation<T>.validateAndThrowOnFailure(value: T) {
val result = validate(value)
if (result is Invalid<T>) {
throw IllegalArgumentException(result.errors.toString())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment