Skip to content

exactly_one_of and mutually_exclusive seem not to work in a group #699

@klaustopher

Description

@klaustopher

Hi there,

I'm currently creating an API with the stable version (0.8.0) and one of the endpoints should get some nested params of which I only need one.

class RatingService < Grape::API
  version 'v1'

  namespace :ratings do
    desc 'Rates an Artist or a Song'
    params do
      requires :rating, type: Hash do
        requires :value, type: Integer
        optional :artist, type: String
        optional :song, type: String
        exactly_one_of :song, :artist
      end
    end
    post do
      puts params.to_yaml
      { message: 'Rating created' }
    end
  end
end

When I send data to the API, I get the following error:

# POST http://localhost:3000/api/v1/ratings.json
# body:  { "rating": {"value": 12, "aritst": "Some Artist"} }
HTTP/1.1 400 Bad Request

{
    "error": "[:song, :artist] - exactly one parameter must be provided"
}

It works fine, if I put the artist field on the root of body, but that is not what I'm intending.

Is there a special syntax that needs to be used, when defining an exactly_one_of or mutually_exclusive constraint for nested params, or is this a bug?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions