JSV.Schema.Composer (jsv v0.9.0)

View Source

This module contains a composable API to build schemas in a functionnal way.

Every function will return a schema and accepts an optional first argument to merge onto, using JSV.Schema.merge/2.

See JSV.Schema.Helpers to work with a more "presets" oriented API.

Example

iex> %JSV.Schema{}
...> |> object()
...> |> properties(foo: string())
...> |> required([:foo])
%JSV.Schema{type: :object, properties: %{foo: %JSV.Schema{type: :string}}, required: [:foo]}

Summary

Functions

Defines or merges onto a JSON Schema with allOf: schemas.

Defines or merges onto a JSON Schema with anyOf: schemas.

Defines or merges onto a JSON Schema with type: :array and items: item_schema.

Defines or merges onto a JSON Schema with type: :boolean.

Defines or merges onto a JSON Schema with type: :string and format: :date.

Defines or merges onto a JSON Schema with type: :string and format: :"date-time".

Defines or merges onto a JSON Schema with type: :string and format: :email.

Defines or merges onto a JSON Schema with format: format.

Defines or merges onto a JSON Schema with type: :integer.

Defines or merges onto a JSON Schema with items: item_schema.

Defines or merges onto a JSON Schema with type: :integer and maximum: -1.

Defines or merges onto a JSON Schema with type: :string and minLength: 1.

Defines or merges onto a JSON Schema with type: :integer and minimum: 0.

Defines or merges onto a JSON Schema with type: :number.

Defines or merges onto a JSON Schema with type: :object.

Defines or merges onto a JSON Schema with oneOf: schemas.

Defines or merges onto a JSON Schema with type: :integer and minimum: 1.

Defines or merges onto a JSON Schema with properties: properties.

Defines or merges onto a JSON Schema with type: :object and properties: properties.

Defines or merges onto a JSON Schema with $ref: ref.

Defines a JSON Schema with required: keys or adds the given keys if the base schema already has a :required definition.

Defines or merges onto a JSON Schema with type: :string.

Defines or merges onto a JSON Schema with type: :string and format: format.

Defines or merges onto a JSON Schema with type: :string and jsv-cast: JSV.Cast.string_to_atom().

Defines or merges onto a JSON Schema with type: :string, enum: enum and jsv-cast: JSV.Cast.string_to_atom().

Defines or merges onto a JSON Schema with type: :string and jsv-cast: JSV.Cast.string_to_boolean().

Defines or merges onto a JSON Schema with type: :string and jsv-cast: JSV.Cast.string_to_existing_atom().

Defines or merges onto a JSON Schema with type: :string and jsv-cast: JSV.Cast.string_to_float().

Defines or merges onto a JSON Schema with type: :string and jsv-cast: JSV.Cast.string_to_integer().

Defines or merges onto a JSON Schema with type: :string and jsv-cast: JSV.Cast.string_to_number().

Defines or merges onto a JSON Schema with type: :string and format: :uri.

Defines or merges onto a JSON Schema with type: :string and format: :uuid.

Types

properties()

@type properties() ::
  [{property_key(), JSV.Schema.schema()}]
  | %{optional(property_key()) => JSV.Schema.schema()}

property_key()

@type property_key() :: atom() | binary()

Functions

all_of(merge_base \\ nil, schemas)

Defines or merges onto a JSON Schema with allOf: schemas.

any_of(merge_base \\ nil, schemas)

Defines or merges onto a JSON Schema with anyOf: schemas.

array_of(merge_base \\ nil, item_schema)

Defines or merges onto a JSON Schema with type: :array and items: item_schema.

boolean(merge_base \\ nil)

Defines or merges onto a JSON Schema with type: :boolean.

date(merge_base \\ nil)

Defines or merges onto a JSON Schema with type: :string and format: :date.

datetime(merge_base \\ nil)

@spec datetime(JSV.Schema.merge_base()) :: JSV.Schema.schema()

Defines or merges onto a JSON Schema with type: :string and format: :"date-time".

email(merge_base \\ nil)

Defines or merges onto a JSON Schema with type: :string and format: :email.

format(merge_base \\ nil, format)

@spec format(JSV.Schema.merge_base(), term()) :: JSV.Schema.schema()

Defines or merges onto a JSON Schema with format: format.

Does not set the type: :string on the schema. Use string_of/2 for a shortcut.

integer(merge_base \\ nil)

Defines or merges onto a JSON Schema with type: :integer.

items(merge_base \\ nil, item_schema)

Defines or merges onto a JSON Schema with items: item_schema.

Does not set the type: :array on the schema. Use array_of/2 for a shortcut.

neg_integer(merge_base \\ nil)

@spec neg_integer(JSV.Schema.merge_base()) :: JSV.Schema.schema()

Defines or merges onto a JSON Schema with type: :integer and maximum: -1.

non_empty_string(merge_base \\ nil)

@spec non_empty_string(JSV.Schema.merge_base()) :: JSV.Schema.schema()

Defines or merges onto a JSON Schema with type: :string and minLength: 1.

non_neg_integer(merge_base \\ nil)

@spec non_neg_integer(JSV.Schema.merge_base()) :: JSV.Schema.schema()

Defines or merges onto a JSON Schema with type: :integer and minimum: 0.

number(merge_base \\ nil)

Defines or merges onto a JSON Schema with type: :number.

object(merge_base \\ nil)

Defines or merges onto a JSON Schema with type: :object.

See props/2 to define the properties as well.

one_of(merge_base \\ nil, schemas)

Defines or merges onto a JSON Schema with oneOf: schemas.

pos_integer(merge_base \\ nil)

@spec pos_integer(JSV.Schema.merge_base()) :: JSV.Schema.schema()

Defines or merges onto a JSON Schema with type: :integer and minimum: 1.

properties(merge_base \\ nil, properties)

@spec properties(
  JSV.Schema.merge_base(),
  properties()
) :: JSV.Schema.schema()

Defines or merges onto a JSON Schema with properties: properties.

Does not set the type: :object on the schema. Use props/2 for a shortcut.

props(merge_base \\ nil, properties)

Defines or merges onto a JSON Schema with type: :object and properties: properties.

ref(merge_base \\ nil, ref)

Defines or merges onto a JSON Schema with $ref: ref.

A struct-based schema module name is not a valid reference. Modules should be passed directly where a schema (and not a $ref) is expected.

Example

For instance to define a user property, this is valid:

props(user: UserSchema)

The following is invalid:

# Do not do this
props(user: ref(UserSchema))

required(merge_base \\ nil, key_or_keys)

@spec required(JSV.Schema.merge_base(), [atom() | binary()]) :: JSV.Schema.schema()

Defines a JSON Schema with required: keys or adds the given keys if the base schema already has a :required definition.

Existing required keys are preserved.

Examples

iex> JSV.Schema.required(%{}, [:a, :b])
%{required: [:a, :b]}

iex> JSV.Schema.required(%{required: nil}, [:a, :b])
%{required: [:a, :b]}

iex> JSV.Schema.required(%{required: [:c]}, [:a, :b])
%{required: [:a, :b, :c]}

iex> JSV.Schema.required(%{required: [:a]}, [:a])
%{required: [:a, :a]}

Use JSV.Schema.merge/2 to replace existing required keys.

iex> JSV.Schema.merge(%{required: [:a, :b, :c]}, required: [:x, :y, :z])
%{required: [:x, :y, :z]}

string(merge_base \\ nil)

Defines or merges onto a JSON Schema with type: :string.

string_of(merge_base \\ nil, format)

@spec string_of(JSV.Schema.merge_base(), term()) :: JSV.Schema.schema()

Defines or merges onto a JSON Schema with type: :string and format: format.

string_to_atom(merge_base \\ nil)

@spec string_to_atom(JSV.Schema.merge_base()) :: JSV.Schema.schema()

Defines or merges onto a JSON Schema with type: :string and jsv-cast: JSV.Cast.string_to_atom().

string_to_atom_enum(merge_base \\ nil, enum)

@spec string_to_atom_enum(JSV.Schema.merge_base(), [atom()]) :: JSV.Schema.schema()

Defines or merges onto a JSON Schema with type: :string, enum: enum and jsv-cast: JSV.Cast.string_to_atom().

Accepts a list of atoms and validates that a given value is a string representation of one of the given atoms.

On validation, a cast will be made to return the original atom value.

This is useful when dealing with enums that are represented as atoms in the codebase, such as Oban job statuses or other Ecto enum types.

iex> schema = JSV.Schema.props(status: JSV.Schema.Composer.string_to_atom_enum([:executing, :pending]))
iex> root = JSV.build!(schema)
iex> JSV.validate(%{"status" => "pending"}, root)
{:ok, %{"status" => :pending}}

Does not support nil

This function sets the string type on the schema. If nil is given in the enum, the corresponding valid JSON value will be the "nil" string rather than null

string_to_boolean(merge_base \\ nil)

@spec string_to_boolean(JSV.Schema.merge_base()) :: JSV.Schema.schema()

Defines or merges onto a JSON Schema with type: :string and jsv-cast: JSV.Cast.string_to_boolean().

string_to_existing_atom(merge_base \\ nil)

@spec string_to_existing_atom(JSV.Schema.merge_base()) :: JSV.Schema.schema()

Defines or merges onto a JSON Schema with type: :string and jsv-cast: JSV.Cast.string_to_existing_atom().

string_to_float(merge_base \\ nil)

@spec string_to_float(JSV.Schema.merge_base()) :: JSV.Schema.schema()

Defines or merges onto a JSON Schema with type: :string and jsv-cast: JSV.Cast.string_to_float().

string_to_integer(merge_base \\ nil)

@spec string_to_integer(JSV.Schema.merge_base()) :: JSV.Schema.schema()

Defines or merges onto a JSON Schema with type: :string and jsv-cast: JSV.Cast.string_to_integer().

string_to_number(merge_base \\ nil)

@spec string_to_number(JSV.Schema.merge_base()) :: JSV.Schema.schema()

Defines or merges onto a JSON Schema with type: :string and jsv-cast: JSV.Cast.string_to_number().

uri(merge_base \\ nil)

Defines or merges onto a JSON Schema with type: :string and format: :uri.

uuid(merge_base \\ nil)

Defines or merges onto a JSON Schema with type: :string and format: :uuid.