Search code examples
apiblueprintapiary

Is it possible to define a fixed array length in API Blueprint?


I'm writing an API specification in API Blueprint and I want to define a location attribute to be a fixed array of length 2, like this:

location object

I have written it as a number array like:

+ location (array[number], required)

And this is the result in Apiary:

enter image description here

I think that it's possible because of that zero in the image, but I don't know how. I've searched trough the documentation and I didn't find anything... Any idea on how to do it? Thanks.


Solution

  • It is possible, you can use the fixed type attribute in the array which means the values are fixed along side the length of the array.

    In your case you don't want the values to be fixed as you intend that any numbers can be inside the array. To do this you would need to move the values into being samples. Here's an example:

    + Response 200 (application/json)
        + Attributes
            + location (array, fixed)
                + `43.3` (number, sample)
                + `-8.41` (number, sample)
    

    Unfortunately there are some current bugs in the API Blueprint parser so this won't work as intended. It will produce an incorrect JSON Schema and example JSON body, the latter is tracked by https://github.com/apiaryio/drafter/issues/519 which I believe will be resolved soon.

    Due to these limitations, perhaps you would be better off explaining this in the description for the location property:

    + Response 200 (application/json)
        + Attributes
            + location (array) - Location MUST contain exactly two numbers.
                + `43.3` (number)
                + `-8.41` (number)
    

    Which would be rendered as follows:

    enter image description here