-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Feature: add group attributes for parameter definitions #1507
Conversation
I think the use case where all parameters for an endpoint share the same options is pretty rare; even if your initial endpoint has only homogenous params, eventually you'll want to add an extra param of a different type and be forced to undo all the shared options. However, like you point out, having a group of parameters with shared options is very common. What about a solution which introduces a new method within the params DSL? Something like this: params do
with(type: String, allow_blank: false) do
requires :first_name
requires :last_name
end
requires :age, type: Integer, allow_blank: true
end |
You can use an array of attribute names rather than a single one to define a bunch of identical attributes, right?
One could also use a params helper to dynamically specify the name(s) of attrs that need a specific validation.
|
Thank you for the feedback! I like @rnubel's suggestion too. I think it is better to have it this way than as a part of params method. I will try to implement it and I will update this PR when I have something to show. |
44c7c00
to
c90ff12
Compare
It is ready. I've added a new method /cc @dblock |
I'm going to merge this. Seems like a sensible add-on and doesn't hurt at the very least. If anyone hates it or thinks it can be improved further, speak up so we can iterate before the next release. |
It is a proposal with a working prototype for a group attributes feature. If it makes sense then I will continue working on this PR. Your feedback is welcome.
I was looking into this issue (#1482) recently. I think the presented use case is common for complex APIs schemas. Currently, if I want to apply the same validator (or the same type) for a several endpoint attributes, then I have to define it for every parameter.
What if we introduce a way of defining common parameter settings? Something like Rails conditional validations.
For example, this parameters definition:
can be replaced with
It is also possible to extract group attributes into a class method and use it instead of default params in your code. It should provide the solution for the problem from #1482.
/cc @dblock