A generic Value Object library for Kotlin. Inspired by Zod. We never use 3rd party libraries.
A Value Object is an object whose equality is determined by the "values of its attributes" rather than the object's "identifier." In Domain-Driven Design (DDD), they are used to clearly model concepts and rules.
view iolite API
- Date
- DateTime
- Time
- Base64
- UUID
- CIDR
- Domain
- HostName
- IPV4
- IPV6
- MacAddress
- URL
- Age
- CreditCardNumber
- JP Phone Number
- JP Postal Code
- AlphaNumeric
- Decimal
- Integer
val email = Email("[email protected]")
You will primarily be using parse and safeParse, which are explained in more detail below.
Parse validates the input value provided and raises an exception if it is invalid.
try {
val email: String = Email("[email protected]").parse()
}catch (e: IllegalArgumentException) {
// Handle the exception if needed
}
safeParse also verifies that the given value is valid, but returns a Result type instead of an exception.
val email = Email("[email protected]").safeParse()
if(email.isFailure){
// Handle the exception if needed
}
println(email.getOrNull()) // print "[email protected]"
All value classes other than StringValueObject in the strings package inherit StringValueObject, and it is possible to check the values using method chaining as shown below.
val stringVal = StringValueObject("prefix123suffix")
.notEmpty()
.startWith("prefix")
.endWith("suffix")
.min(10)
.max(20)
.regex(Regex("^[a-zA-Z0-9]+$"))
.customerValidation(
validation = { it.contains("123") },
errorMessage = "Custom validation failed"
)
.parse()
val integerString = IntegerString("100000000")
.parse() // can retlieve StringValueObject
.notEmpty()
.min(3)
.max(10)
.parse()
You can validate these credit card brands.
- Amex
- Diners Club
- Discover
- JCB
- MasterCard
- Visa
val creditCardNumber = CreditCard("4111111111111111").parse()
this is same logic of Valibot. and By running them through the same test cases, we can ensure that they are of the same quality.
implementation("io.github.ysknsid25.iolite:iolite:{version}")
refer here
- Inspired by Zod. Zod's icon is a blue gem, so I thought I'd use the gem's name.
- I thought of jewels with similar colors to Kotlin's brand color.
- Because my favorite artist has a song with the same name.