open_api_spex v3.4.0 OpenApiSpex.Cast View Source
Link to this section Summary
Functions
Cast and validate a value against the given schema
Link to this section Types
Link to this type
schema_or_reference()
View Source
schema_or_reference()
View Source
schema_or_reference() :: OpenApiSpex.Schema.t() | OpenApiSpex.Reference.t()
schema_or_reference() :: OpenApiSpex.Schema.t() | OpenApiSpex.Reference.t()
Link to this type
t()
View Source
t()
View Source
t() :: %OpenApiSpex.Cast{
errors: [OpenApiSpex.Cast.Error.t()],
index: integer(),
key: atom() | nil,
path: [atom() | OpenApiSpex.Cast.String.t() | integer()],
schema: schema_or_reference() | nil,
schemas: map(),
value: term()
}
t() :: %OpenApiSpex.Cast{ errors: [OpenApiSpex.Cast.Error.t()], index: integer(), key: atom() | nil, path: [atom() | OpenApiSpex.Cast.String.t() | integer()], schema: schema_or_reference() | nil, schemas: map(), value: term() }
Link to this section Functions
Link to this function
cast(ctx)
View Source
cast(ctx)
View Source
cast(t()) :: {:ok, term()} | {:error, [OpenApiSpex.Cast.Error.t()]}
cast(t()) :: {:ok, term()} | {:error, [OpenApiSpex.Cast.Error.t()]}
Link to this function
cast(schema, value, schemas \\ %{})
View Source
cast(schema, value, schemas \\ %{})
View Source
cast(schema_or_reference() | nil, term(), map()) ::
{:ok, term()} | {:error, [OpenApiSpex.Cast.Error.t()]}
cast(schema_or_reference() | nil, term(), map()) :: {:ok, term()} | {:error, [OpenApiSpex.Cast.Error.t()]}
Cast and validate a value against the given schema.
Recognizes all the types defined in Open API (itself a superset of JSON Schema).
JSON Schema types: https://json-schema.org/latest/json-schema-core.html#rfc.section.4.2.1
Open API primitive types: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#data-types
For an :object
schema type, the cast operation returns a map with atom keys.
Examples
iex> alias OpenApiSpex.{Cast, Schema}
iex> schema = %Schema{type: :string}
iex> Cast.cast(schema, "a string")
{:ok, "a string"}
iex> Cast.cast(schema, :not_a_string)
{
:error,
[
%OpenApiSpex.Cast.Error{
reason: :invalid_type,
type: :string,
value: :not_a_string
}
]
}
iex> schema = %Schema{
...> type: :object,
...> properties: %{
...> name: nil
...> }
...> }
iex> Cast.cast(schema, %{"name" => "spex"})
{:ok, %{name: "spex"}}
iex> Cast.cast(schema, %{"bad" => "spex"})
{
:error,
[
%OpenApiSpex.Cast.Error{
name: "bad",
path: ["bad"],
reason: :unexpected_field,
value: %{"bad" => "spex"}
}
]
}
Link to this function
error(ctx, error_args) View Source
Link to this function
ok(cast) View Source
Link to this function