Support interdependencies between query parameters
It would be great to be able to specify interdependencies between query parameters. In my app, some query parameters become "required" only when some other query parameter is present. And when conditionally required parameters are missing when the conditions are met, the API fails. Of course I can have the API reply back that some required parameter is missing, but it would be great to have that built into Swagger.
This is something that's very complicated to describe elegantly. Feel free to suggest a format that you think would be suitable for it.
My suggestion would be as follows: instead of having the field "required" boolean, make it either boolean or an array of references to other parameters. If you put required: true, it would have to always be present. If you put required: [$a, $b], it means that this parameter is required only when parameters a and b are present.
How you describe an endpoint where one of it's parameters is required if another query parameter is equal to a string or is larger than a number? This can get very complicated. I would say if a parameter is required sometimes it's not required.
I'd say either keep it simple and only allow simple interdependencies like, if a is present, then bis required (i.e. required: [$a, $b]), or define simple operations like, required: [$a>3, $b=="hello", "world" in $c]. I would favor keeping it simple, but then I have to admit that my only response to "why not keep it even simpler (i.e. as it is now) and only have a boolean required" is: it's a matter of how useful more sophistications are. For me, having simple interdependencies is useful and better than just a boolean required, but I don't think I need more complicated stuff. Other people might disagree.
While this may be a solution, it can also be a validation nightmare. Definitely not something that can be done with a json schema.
maybe something like
"required": false, "depends": [ $a, $b ]
Don't forget the "OR" dependency. I have a service where one of two query parameters must be present but it doesn't matter which one. For now we are just documenting the interdependency and using a error schema with the appropriate code/message but would be preferable to have this defined in the contract up front.
+1 for cwarny simple suggestions
My use-case is to have only one property of the parameter required, but from the array of the definitions.
You can solve the OR use case already using an enumerated filter_type paired with a filter_value query parameter(s).
Example:
- name: filter_type
in: query
description: What type of values to filter on
required: false
type: string
enum: [location, service, name]
default: location
- name: filter_value
in: query
description: The value to apply to the filter_type. Should match what was in the filter, i.e. if filtering on location use a location name.
required: false
type: string
default: all
wow, good one, thanks
@craig-bayley could you format your example as YAML, please? Just adding
```yaml
[...]
```
around it makes it much easier to read.
@ePaul Thanks for the tip. Done.
+1 Something like this would be very nice.
Folks just to set expectations, we're not doing this in the initial cut of 3.0
some query parameters become "required" only when some other query parameter is present
The JSON Schema "dependencies" keyword does exactly this. The "if"/"then"/"else" keywords (json-schema-org/json-schema-spec#180) that seem likely to go into a near-future draft would also accomplish this (probably more clearly for most people).
I don't see how JSON Schema would be relevant for this at all.
@webron further up you said:
Definitely not something that can be done with a json schema.
so I assumed that meant that JSON Schema was at least potentially relevant, which meant that I should point out that JSON Schema can in fact do this. I think you were specifically referring to an approach based on "required" which would not be valid JSON Schema, but there is JSON Schema support for this use case.
Gotcha. That referred to the validation of it, not the representation of it in the spec. I assumed you were referring to the representation, making it my bad.
When it going to be on air ? much needed one.