PhoenixSwagger.Validator (phoenix_swagger v0.8.3) View Source

The PhoenixSwagger.Validator module provides converter of swagger schema to ex_json_schema structure for further validation.

There are two main functions:

  • parse_swagger_schema/1
  • validate/2

Before validate/2 will be called, a swagger schema should be parsed for futher validation with the parse_swagger_schema/1. This function takes path to a swagger schema and returns it in ex_json_schema format.

During execution of the parse_swagger_schema/1 function, it creates the validator_table ets table and stores associative key/value there. Where key is an API path of a resource and value is input parameters of a resource.

To validate of a parsed swagger schema, the validate/1 should be used.

For more information, see more in ./phoenix_swagger/tests/ directory.

Link to this section Summary

Functions

The parse_swagger_schema/1 takes path or list of paths to a swagger schema(s), parses it/them into ex_json_schema format and store to the validator_table ets table.

The validate/2 takes a resource path and input parameters of this resource.

Link to this section Functions

Link to this function

parse_swagger_schema(specs)

View Source

The parse_swagger_schema/1 takes path or list of paths to a swagger schema(s), parses it/them into ex_json_schema format and store to the validator_table ets table.

Usage:

iex(1)> parse_swagger_schema("my_json_spec.json")
[{"/person",  %{'__struct__' => 'Elixir.ExJsonSchema.Schema.Root',
                location => root,
                refs => %{},
                schema => %{
                  "properties" => %{
                    "name" => %{"type" => "string"},
                    "age" => %{"type" => "integer"}
                  }
                }
              }
}]

The validate/2 takes a resource path and input parameters of this resource.

Returns :ok in a case when parameters are valid for the given resource or:

  • in a case when path is not exists in the validator table;
  • in a case when at least one parameter is not valid for the given resource.