Last active
April 8, 2021 13:25
-
-
Save 1gravity/c7adc05a714b2cb2bb5ae2f28800bf1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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