Validation on POST entity on custom REST endpoints

Hello everyone,

I am currently working on custom REST endpoints in Keycloak, and I have a POST endpoint that requires several fields. I want to validate that these fields are neither null nor blank before proceeding with my functional processing.

In Spring Boot, there is a validation mechanism with annotations such as @Valid and @NotBlank. Is there a way to implement similar validation for a custom REST endpoint in Keycloak, or do I have to check each field one by one, at the beginning of my function ?

Thank you in advance for your help !

I did it manually since there was just one Endpoint. But since Keycloak is running on top of Quarkus, check Quarkus docs but I guess you can always use Hibernate for this:

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-hibernate-validator</artifactId>
</dependency>

then all should be the same:

public User create(@Valid CreateUserRequest request) {
public class CreateUserRequest {

    @Email
    @NotBlank(message = "email may not be blank")
    private String email;

    @Size(min = 4, max = 15, message = "username should have size [{min},{max}]")
    @NotBlank(message = "username may not be blank")
    @Pattern(regexp = "^[a-zA-Z][a-zA-Z0-9]+$", message = "\"username\" should start with a letter and should only accept letters and numbers")
    private String username;

    @NotBlank(message = "firstName may not be blank")
    private String firstName;