Skip to content

Model definitions, array of primitive types (i.e. type : [ "string", "null" ] ) #459

@chimmelb

Description

@chimmelb

I'm using swagger on a project and validating the JSON HTTP requests and responses with the same swagger models. Quite handy since both are JSON v4 compliant . . . almost.

It is valid JSON for the "type" of a property to be an array of primitives. See section 5.5.2 of the JSON schema validation document.

My use case has a model object definition like this:

var contact = {
  "id" : "Contact",
  "description" : "Contact information.",
  "additionalProperties" : false,
  "properties" : {
    //...
    "middleName" : {
      "type" : "any",//["string","null"],
      "description" : "middle name, is a string, may be null"
    },
  }
}

I use a JSON v4 compliant validation module to ensure input and outputs to our system are valid. Like when an object is returned from our database(-layer) I validate against the same Model that is defined in swagger. Sweet! However, some database fields can be null.

I use "type" : "any" to make swagger and my validation happy, except it's not true that any type will work, and I'm stuck with un-enforceable comments to let the user of the API know how to pass data.

Supporting the JSON v4 spec with an array of types would be great when using the JSON v4 model in both swagger and validation, especially with a pass-through database that allows NULLs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions