Having a unique but optional field in keycloak using the declarative user profile

We are looking into using keycloak for our project and there is one thing I can’t seem to find a solution for: Is it possible to have a unique but optional field using the declaratie user profile?

Our use case is this: We would like to give users the option to choose an optional nickname for the forum portion of our project. Not everyone is going to use the forum but the nickname - if it is set - is supposed to be unique.

Is there a solution for this?

Is it possible to have a unique but optional field using the declaratie user profile?

Good news, yes.

if it is set - is supposed to be unique.

Bad news, I believe you’ll have to implement your own Validator, as there isn’t a built-in one that compares attributes for uniqueness. However, implementing org.keycloak.validate.Validator really just requires one method, so it appears to be fairly straightforward.

Relevant classes and built-in implementations here: keycloak/server-spi-private/src/main/java/org/keycloak/validate at main · keycloak/keycloak · GitHub

1 Like

Thanks! We are now discussing how to move forward with this.