open_api_spex v1.1.2 OpenApiSpex View Source

Provides the entry-points for defining schemas, validating and casting.

Link to this section Summary

Functions

Cast params to conform to a Schema or Operation spec

Adds schemas to the api spec from the modules specified in the Operations

Declares a OpenApiSpex Schema

Validate params against a Schema or Operation spec

Link to this section Functions

Cast params to conform to a Schema or Operation spec.

Link to this function cast(spec, operation, params, content_type \\ nil) View Source
Link to this function resolve_schema_modules(spec) View Source

Adds schemas to the api spec from the modules specified in the Operations.

Eg, if the response schema for an operation is defined with:

responses: %{
  200 => Operation.response("User", "application/json", UserResponse)
}

Then the UserResponse.schema() function will be called to load the schema, and a Reference to the loaded schema will be used in the operation response.

See OpenApiSpex.schema macro for a convenient syntax for defining schema modules.

Declares a OpenApiSpex Schema

  • defines the schema/0 callback
  • ensures the schema is linked to the module by “x-struct” extension property
  • defines a struct with keys matching the schema properties
  • defines a @type t for the struct
  • derives a Poison.Encoder for the struct

Example

require OpenApiSpex

defmodule User do
  OpenApiSpex.schema %{
    title: "User",
    description: "A user of the app",
    type: :object,
    properties: %{
      id: %Schema{type: :integer, description: "User ID"},
      name:  %Schema{type: :string, description: "User name", pattern: ~r/[a-zA-Z][a-zA-Z0-9_]+/},
      email: %Schema{type: :string, description: "Email address", format: :email},
      inserted_at: %Schema{type: :string, description: "Creation timestamp", format: :'date-time'},
      updated_at: %Schema{type: :string, description: "Update timestamp", format: :'date-time'}
    },
    required: [:name, :email],
    example: %{
      "id" => 123,
      "name" => "Joe User",
      "email" => "joe@gmail.com",
      "inserted_at" => "2017-09-12T12:34:55Z",
      "updated_at" => "2017-09-13T10:11:12Z"
    }
  }
end
Link to this function validate(spec, schema, params) View Source

Validate params against a Schema or Operation spec.

Link to this function validate(spec, operation, params, content_type \\ nil) View Source