Skip to content
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

Allow parameter only if another parameter is present. #958

Closed
dblock opened this issue Mar 18, 2015 · 8 comments
Closed

Allow parameter only if another parameter is present. #958

dblock opened this issue Mar 18, 2015 · 8 comments

Comments

@dblock
Copy link
Member

dblock commented Mar 18, 2015

For example:

requires :product_id, type: String # a product id
optional :shelf_id, type: String # only if the product is on this shelf
optional :shelf_reachable, type: Boolean # only if the shelf is reachable

You shouldn't be able to specify shelf_reachable unless :shelf_id is present.

@u2
Copy link
Contributor

u2 commented Apr 2, 2015

That's a good idea.I just have this demand.

        params do
          requires :cid, type: Integer
          requires :car_order, type: Hash do
            optional :cars_params, type: Array do
              requires :id, type: Integer
              requires :quantity, type: Integer #
            end
            optional :car_sets_params, type: Array
          end

We want at least one of cars_params and car_sets_params is present.Maybe can write something like
if: Proc.new { }.

@dblock
Copy link
Member Author

dblock commented Apr 3, 2015

@u2 We already have at_least_one_of.

@u2
Copy link
Contributor

u2 commented Apr 3, 2015

Thank you.:smile:

@rnubel
Copy link
Contributor

rnubel commented Jun 21, 2015

After thinking about this for a bit, I think it'd be more useful to be able to say "if shelf_id is given, then require shelf_reachable" rather than the inverse. The two are closely related, at any rate, and I could imagine there being use cases for both. So along those lines, I've quickly prototyped a couple possible ways to implement such logic:

Option 1 (DSL style)

https://github.com/rnubel/grape/compare/dependent_parameters_dsl

params do
  optional :shelf_id, type: Integer
  if_given :shelf_id do
    requires :shelf_reachable, type: Boolean
  end
end

Option 2 (Validation style)

https://github.com/rnubel/grape/compare/dependent_parameters_validation

params do
  optional :shelf_id, type: Integer
  optional :shelf_reachable, type: Boolean
  if_given :shelf_id, require: :shelf_reachable
end

What do you think?

@dblock
Copy link
Member Author

dblock commented Jun 22, 2015

I like the first one, but I don't love the name. Maybe just with?

@rnubel
Copy link
Contributor

rnubel commented Jun 23, 2015

Hmm... with is a bit vague. I think that's the tricky part here -- we want a term that's clear about what it means, but still concise. What about just given?

params do
  optional :shelf_id, type: Integer
  given :shelf_id do
    requires :shelf_reachable, type: Boolean
  end
end

@dblock
Copy link
Member Author

dblock commented Jun 23, 2015

I like given, go for it!

rnubel added a commit to rnubel/grape that referenced this issue Jun 26, 2015
Usage:

    # ...
    params do
      optional :shelf_id, type: Integer
      given :shelf_id do
        requires :bin_id, type: Integer
      end
    end

This implements ruby-grape#958. In order to achieve the DSL-style implementation,
I introduced the concept of a "lateral scope" as opposed to the
"nested scope" which `requires :foo do` opens up. A lateral scope is
subordinate to its parent, but not nested under an element.
rnubel added a commit to rnubel/grape that referenced this issue Jun 26, 2015
Usage:

    # ...
    params do
      optional :shelf_id, type: Integer
      given :shelf_id do
        requires :bin_id, type: Integer
      end
    end

This implements ruby-grape#958. In order to achieve the DSL-style implementation,
I introduced the concept of a "lateral scope" as opposed to the
"nested scope" which `requires :foo do` opens up. A lateral scope is
subordinate to its parent, but not nested under an element.
rnubel added a commit to rnubel/grape that referenced this issue Jun 26, 2015
Usage:

    # ...
    params do
      optional :shelf_id, type: Integer
      given :shelf_id do
        requires :bin_id, type: Integer
      end
    end

This implements ruby-grape#958. In order to achieve the DSL-style implementation,
I introduced the concept of a "lateral scope" as opposed to the
"nested scope" which `requires :foo do` opens up. A lateral scope is
subordinate to its parent, but not nested under an element.
calebjclark pushed a commit to calebjclark/grape that referenced this issue Aug 17, 2015
Usage:

    # ...
    params do
      optional :shelf_id, type: Integer
      given :shelf_id do
        requires :bin_id, type: Integer
      end
    end

This implements ruby-grape#958. In order to achieve the DSL-style implementation,
I introduced the concept of a "lateral scope" as opposed to the
"nested scope" which `requires :foo do` opens up. A lateral scope is
subordinate to its parent, but not nested under an element.
@dblock
Copy link
Member Author

dblock commented Aug 22, 2015

This was resolved in #1047.

@dblock dblock closed this as completed Aug 22, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants