Skip to content

Kotlin UUID bean validation

Sébastien Dubois / December 10, 2017

1 min read

Here’s a quickie: a small bean validation constraint annotation in Kotlin for UUIDs:

import javax.validation.Constraint
import javax.validation.Payload
import javax.validation.constraints.NotBlank
import javax.validation.constraints.Pattern
import kotlin.reflect.KClass

const val UUID_REGEXP = "^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"

 * Validation constraint for {@link UUID}s.
 * Reference:
@Constraint(validatedBy = [])
@Pattern(regexp = UUID_REGEXP)
annotation class UUID(
        val message: String = "{invalid.uuid}",
        val groups: Array<KClass<*>> = [],
        val payload: Array<KClass<out Payload>> = []

That’s all folks!

PS: check out the Dev Concepts collection of e-books, join the Software Crafters community, the Personal Knowledge Management community, and come say hi on Twitter!
Discuss on TwitterEdit on GitHub