Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put validations when following a DCI design?

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.

like image 475
Carlos Paramio Avatar asked Oct 13 '12 18:10

Carlos Paramio


1 Answers

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

like image 81
Rune FS Avatar answered Nov 03 '22 13:11

Rune FS