How to add validation to the custom attribute

I added some custom attributes in registration page, Now I would like to put some Validation on the attributes.
Can someone tell which file to modify to add the validation for the same

The new User Profile SPI might be what you are looking for: Server Administration Guide
It’s still a preview feature, but it allows to add custom attributes and validate them (and also the default ones).

Thank you @dasniko
I will look into User Profile SPI