Skip to content

Param with multiple acceptable Hash Types #2385

@mia-n

Description

@mia-n

How would I define that a given param can have multiple possible hash values?

As a very simplified example, say that either of these hashes are valid params:

"value": {
    "time_unit": "hour",
    "rate": 100.0
}
"value": {
    "fixed_price": 100.0,
}

but that value can take only one or the other of those forms. It's also possible I could add more acceptable formats in the future, but that there will always be a finite number of acceptable formats to the "value" key.

I attempted something like this but it does not seem to be possible:

params do
  requires :value, desc: "TODO", types: [
    Hash {
      requires :fixed_price, type: Float
    },
    Hash {
      requires :time_unit, type: String
      requires :rate, type: Float
    },
  ]
end

I can sort of achieve what I want right now by delcaring its type as hash, declaring all of the possible fields as optional, and setting some complicated rules with mutually_exclusive and given, but this is messy and it is difficult to parse at a glance what the expected possible schemas are.

Is there a better way to achieve this?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions