I am working on some user registration code, and I’m wondering where the best place is to put my validation code. I can create a user validation object and pass the user model to it to validate, or I can just instantiate the validation object within the user model or even write the validation code into the user model, however, I’m thinking that this would be wasteful as the validation object/code is only used when registering, and editing user data. Therefore, I’m leaning to keeping the user validation object external. Can someone advise me as to what the best practices are? Appreciate your help!
Share
Generally speaking, I’d say make the User model responsible for maintaining its own validity. Put these invariants in the model itself. Then, nothing can put a User in an invalid state, not just your current creation and editing features.