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 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.
@finn-matti did you successfully implement this? i’m having trouble with updating a user already having the supposed-to-be-unique attribute set.
in my case i manage a subscription number in a custom JPA entity.
whenever i create or update a user, the validator checks if the subscription number exists and if no other user already has this subscription number.
on create this is not really a problem, but when i update a user, i need to exclude the user i am updating from the check but i did not find any solution to be aware of the entity i am updating from inside the validator.
We didn’t. Back then we decided to make the field optional but not unique and have since opted to not use keycloak at all (I have since changed projects and therefore don’t know the reason behind this latter decision).
Hey @finallyFedUp, I see you’ve removed your post, but I already have seen it So I extracted my code into github repository, it should work for you by simply running docker compose up -d