validate

fun <C : Any> C.validate(): C

Validates this object using Jakarta Bean Validation and throws an exception if validation fails.

This extension function performs validation on the receiver object using the TestValidator. If the object implements CommandValidator, it first calls the custom validation logic. Then, it validates the object against Jakarta Bean Validation constraints. If any constraint violations are found, it throws a CommandValidationException.

Receiver

The object to validate.

Return

The same object if validation passes.

Type Parameters

C

The type of the object being validated, must be a non-null type.

Throws

CommandValidationException

If validation fails due to constraint violations.

data class TestCommand(@NotBlank val name: String = "")

fun testValidation() { val validCommand = TestCommand("valid") val result = validCommand.validate() // Returns the command if valid

val invalidCommand = TestCommand("") // Empty name violates @NotBlank
try {
    invalidCommand.validate() // Throws CommandValidationException
} catch (e: CommandValidationException) {
    // Handle validation error
}

}