-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Idea] Extract validation to external file #86
Comments
This sounds interesting and it would work similarly to "Form" objects. # app/validators/foo_controller_validator.rb
module FooControllerValidator
def validate_create
param! :username, String, required: true, blank: false
param! :password, String, required: true, blank: false
end
end
# app/controllers/foo_controller.rb
class FooController
include FooControllerValidator
def create
validate_create
# rest of the implementation
end
end What would be the usage on the controller side with this proposal and how would that be better than the one already possible? @ifellinaholeonce your input on this would also be appreciated |
That could be either module or class tho. I came up with class and final controller usage looks like: before_action :validate_params, only: %i[create refresh] The point is as I mentioned to keep it neat and automated. |
That looks cool and similar to other gems behaviour, though we'd need to be extra careful with performance as Ruby can be quite slow with reflections. Test and documentation coverage will also be important. This looks like a big feature! Feel free to open a PR early on the development so we can see how it could work and potentially help with making decisions or tackling complex code together 👍 |
Sounds great :) |
This is an interesting idea for sure. Personally, I don't think this is a feature that I would use much. I find the "documenting" that
Is this meant to help DRY up controller actions that have the exact same params and validations as other controller actions? Do you find a lot of your endpoints have the same params and validations? Defining unique parameters outside of the specific controller seems a little unnecessary. I think I am just missing something here. As iMacTia mentioned, feel free to open a PR early on. Looking forward to seeing how this all could work. |
Basic Info
Issue description
I've been leveraging
rails_params
in one of prior projects. It works great however I prefer to keep controller neat. I wrote a chuck of code to extract validation rules to external files under some path e.g/app/validators/.../*._validator.rb
. Validation file itself could look like:What do you think about such solution? If you find it as useful I could open a PR.
The text was updated successfully, but these errors were encountered: