View Source Xema.JsonSchema (xema v0.17.4)

Converts a JSON Schema to Xema source.

Summary

Functions

This function converts a JSON Schema in Xema schema source. The argument json_schema is expected as a decoded JSON Schema.

Types

@type json_schema() :: true | false | map()
@type opts() :: [draft: String.t(), atom: :force]

Functions

Link to this function

to_xema(json_schema, opts \\ [])

View Source
@spec to_xema(json_schema(), opts()) :: atom() | tuple()

This function converts a JSON Schema in Xema schema source. The argument json_schema is expected as a decoded JSON Schema.

All keys that are not standard JSON Schema keywords have to be known atoms. If the schema has additional keys that are unknown atoms the option atom: :force is needed. In this case the atoms will be created. This is not needed for keys expected by JSON Schema (e.g. in properties)

Options:

  • :draft specifies the draft to check the given JSON Schema. Possible values are "draft4", "draft6", and "draft7", default is "draft7". If :draft not set and the schema contains $schema then the value for $schema is used for this option.
  • :atom creates atoms for unknown atoms when set to :force. This is just needed for additional JSON Schema keywords.

Examples

iex> Xema.JsonSchema.to_xema(%{"type" => "integer", "minimum" => 5})
{:integer, [minimum: 5]}

iex> schema = %{
...>   "type" => "object",
...>   "properties" => %{"foo" => %{"type" => "integer"}}
...> }
iex> Xema.JsonSchema.to_xema(schema)
{:map, [keys: :strings, properties: %{"foo" => :integer}]}

iex> Xema.JsonSchema.to_xema(%{"type" => "integer", "foo" => "bar"}, atom: :force)
{:integer, [foo: "bar"]}