I'm following DCI to structure the behavior of a new Rails application, but I have some doubts about where to put the validations.
Traditionally, if you're going to manage your data using ActiveRecord models, validations are defined at the particular class that inherits from AR, and they seem to fit right as part of the data layer.
However, to my eyes it makes sense to have certain validations that only happens under a specific role, and they should only be checked if the object is on that context, being ignored in all the other cases. Which basically means that those validations should be defined at particular roles, and the object should be extended with those role modules when it's being used on the context where it makes sense.
Do you think it's a good idea to keep those validations at the roles? If so, how do you declare them without contaminating the other instances of the same class than the object? If I want to use the ActiveRecord validations, they are declared at class level, so I can't attach them to the object individually, being forced to use a re-declaration of the "validate" instance method on the role module (attaching the errors to the errors array of the object directly), or some similar technique.
It depends on the validations/rules in question. A main goal of DCI is to segregated what the system is (domain model) from what the system does (functionality) if the rules are associated with the domain model. E.g. rules for welformedness of SSN then it should be part of the data objects. On the other side if the rules are related to the functionality of the system E.g. a user is only allowed to order two super discounted products each week, then it's a matter for a context
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With