marzelwidmer
02/09/2021, 6:21 PMKoliners
I want ask about SecurityByDesign
if there better setup how to create the classes https://gist.github.com/marzelwidmer/6aa9733c919f2a8dc6aa84116fe4a580 what I don’t like is… invoke
function is available
data class Customer (val firstName: FirstName, val lastName: LastName, val gender: Gender)
// Domain Primitive
data class FirstName private constructor(val value: String) {
companion object {
// Create Object
operator fun invoke(value: String) = FirstName(validate(value = value))
private fun validate(value: String): String {
check(value.isNotEmpty()) { "value must be non-empty" }
check(value.trim().length >= 2) { "wrong value length" }
check(value.trim().length <= 20) { "wrong value length" }
return value
}
}
}
nanodeath
02/09/2021, 6:23 PMnanodeath
02/09/2021, 6:24 PMdiesieben07
02/09/2021, 6:24 PMinit
Block of a data class:
data class FirstName(val value: String) {
init {
require(value.isNotEmpty()) { ... }
}
}
marzelwidmer
02/09/2021, 6:24 PM@ParameterizedTest(name = "{index} test gender {0}")
@ValueSource(chars = ['f', 'F', 'm', 'M'])
fun `Customer creation test - it must have a valid gender fF 👧 or mM 🧔`(input: Char) {
Customer(firstName = FirstName(Faker().name.firstName()), lastName = LastName(Faker().name.lastName()), gender = Gender(input))
}
I it posible to hide itdiesieben07
02/09/2021, 6:24 PMrequire
instead of check
, as it throws IllegalArgumentException
nanodeath
02/09/2021, 6:25 PMcopy
methodMarc Knaup
02/09/2021, 6:26 PMMarc Knaup
02/09/2021, 6:26 PMcopy
is guarded by init
afaik.diesieben07
02/09/2021, 6:26 PMinit
can never be bypassed (unless you hack the JVM)Marc Knaup
02/09/2021, 6:27 PMnanodeath
02/09/2021, 6:27 PMmarzelwidmer
02/09/2021, 6:45 PMinit
setup I like … muchos gracias
🙂 @Marc Knaup what U mean to reuse those classes
I am working in a a like microservice
setup… was planing to validate the Bounded Context
with this. how U do it then with the validations ?Marc Knaup
02/09/2021, 6:47 PMMarc Knaup
02/09/2021, 6:48 PMFirstName
to 20 chars (which is ridiculous btw 😉) and store such data to DB.
In the future you want to limit to 19 chars, so you change the validation.
What happens to old users loaded from DB? They may have 20 chars.marzelwidmer
02/09/2021, 6:59 PMMarc Knaup
02/09/2021, 7:00 PMmarzelwidmer
02/09/2021, 7:01 PMmarzelwidmer
02/09/2021, 7:08 PMMarc Knaup
02/09/2021, 7:09 PMIllegalArgumentException
message to the client :)marzelwidmer
02/09/2021, 7:10 PMmarzelwidmer
02/09/2021, 7:11 PMMarc Knaup
02/09/2021, 7:11 PM