plumbapius v0.8.0 Plumbapius.Response
Defines methods for validating responses by request schema
Link to this section Summary
Functions
Validates the response body according to the schema.
Link to this section Functions
Link to this function
validate_response(request_schema, response_status, response_content_type, response_body)
Specs
validate_response( request_schema :: Plumbapius.Request.Schema.t(), response_status :: non_neg_integer(), response_content_type :: String.t(), body :: map() ) :: :ok | {:error, String.t()}
Validates the response body according to the schema.
Parameters
- request_schema: Request schema with responses for validation.
- response_status: Response status.
- response_body: Response body to validate.
Examples
iex> request_schema = Plumbapius.Request.Schema.new(%{
...> "method"=>"GET",
...> "path"=>"/users",
...> "content-type"=>"application/json",
...> "request"=>%{
...> "$schema" => "http://json-schema.org/draft-04/schema#",
...> "type" => "object",
...> "properties" => %{"msisdn" => %{"type" => "number"}},
...> "required" => ["msisdn"]
...> },
...> "responses"=>[
...> %{
...> "content-type" => "application/json",
...> "status" => "200",
...> "body" => %{
...> "$schema" => "http://json-schema.org/draft-04/schema#",
...> "type"=> "object",
...> "properties" => %{
...> "field_name" => %{"type" => "string"}
...> },
...> "required" => ["field_name"],
...> }
...> }
...> ]
...> })
iex> Plumbapius.Response.validate_response(request_schema, 200, "application/json", %{"field_name" => "foobar"})
:ok
iex> Plumbapius.Response.validate_response(request_schema, 200, "application/json", %{"another_field_name" => "12345"})
{:error, "no_such_response_in_schema"}
iex> Plumbapius.Response.validate_response(request_schema, 200, "text/plain", %{"field_name" => "foobar"})
{:error, "no_such_response_in_schema"}
iex> Plumbapius.Response.validate_response(request_schema, 401, "application/json", %{"field_name" => "foobar"})
{:error, "no_such_response_in_schema"}