View Source Xema.Schema (xema v0.16.0)

This module contains the struct for the keywords of a schema.

Link to this section Summary

Types

t()

The struct contains the keywords for a schema.

The type for the schema.

Functions

Fetches a subschema from the schema by the given pointer.

Fetches a subschema from the schema by the given pointer.

Returns all keywords in a list.

Returns a %Schema{} for the given keywords in the keyword list.

Returns the %Schema{} as a map. Items which a nil value are not in the map.

Returns all available types in a list.

Link to this section Types

@type t() :: %Xema.Schema{
  additional_items: Xema.Behaviour.t() | t() | boolean() | nil,
  additional_properties: map() | boolean() | nil,
  all_of: [t()] | nil,
  any_of: [t()] | nil,
  caster:
    function()
    | module()
    | {module(), atom()}
    | {module(), atom(), arity()}
    | list()
    | nil,
  comment: String.t() | nil,
  const: any(),
  contains: Xema.Behaviour.t() | t() | nil,
  content_encoding: String.t() | nil,
  content_media_type: String.t() | nil,
  data: map() | nil,
  default: any(),
  definitions: map() | nil,
  dependencies: list() | map() | nil,
  description: String.t() | nil,
  else: Xema.Behaviour.t() | t() | nil,
  enum: list() | nil,
  examples: [any()] | nil,
  exclusive_maximum: boolean() | number() | nil,
  exclusive_minimum: boolean() | number() | nil,
  format: atom() | nil,
  id: String.t() | nil,
  if: Xema.Behaviour.t() | t() | nil,
  items: list() | Xema.Behaviour.t() | t() | nil,
  keys: atom() | nil,
  max_items: pos_integer() | nil,
  max_length: pos_integer() | nil,
  max_properties: pos_integer() | nil,
  maximum: number() | nil,
  min_items: pos_integer() | nil,
  min_length: pos_integer() | nil,
  min_properties: pos_integer() | nil,
  minimum: number() | nil,
  module: atom() | nil,
  multiple_of: number() | nil,
  not: t() | nil,
  one_of: [t()] | nil,
  pattern: Regex.t() | nil,
  pattern_properties: map() | nil,
  properties: map() | nil,
  property_names: Xema.Behaviour.t() | t() | nil,
  ref: Xema.Ref.t() | nil,
  required: MapSet.t() | nil,
  schema: String.t() | nil,
  then: Xema.Behaviour.t() | t() | nil,
  title: String.t() | nil,
  type: type() | [type()],
  unique_items: boolean() | nil,
  validator:
    function()
    | module()
    | {module(), atom()}
    | {module(), atom(), arity()}
    | list()
    | nil
}

The struct contains the keywords for a schema.

  • additional_items disallow additional items, if set to false. The keyword can also contain a schema to specify the type of additional items.
  • additional_properties disallow additional properties, if set to true.
  • 'all_of' a list of schemas they must all be valid.
  • 'any_of' a list of schemas with any valid schema.
  • caster a custom caster. This can be a function, a tuple with module and function name, or a Xema.Caster behaviour.
  • comment for the schema.
  • const specifies a constant.
  • content_encoding annotation for the encoding.
  • content_media_type annotation for the media type.
  • contains validates a list whether any item is valid for the given schema.
  • data none schema data.
  • default this keyword can be used to supply a default value for JSON and defstruct.
  • definitions contains schemas for reuse.
  • dependencies allows the schema of the map to change based on the presence of certain special properties
  • description of the schema.
  • else see if, then, else.
  • enum specifies an enumeration
  • examples the value of this keyword must be an array. There are no restrictions placed on the values within the array.
  • exclusive_maximum is a boolean. When true, it indicates that the range excludes the maximum value.
  • exclusive_minimum is a boolean. When true, it indicates that the range excludes the minimum value.
  • format semantic validation.
  • id a unique identifier.
  • if, then, else: These keywords work together to implement conditional application of a subschema based on the outcome of another subschema.
  • items specifies the type(s) of the items.
  • keys could be :atoms or :strings.
  • max_items the maximum length of list.
  • max_length the maximum length of string.
  • max_properties the maximum count of properties for the map.
  • maximum the maximum value.
  • min_items the minimal length of list.
  • min_length the minimal length of string.
  • min_properties the minimal count of properties for the map.
  • minimum the minimum value.
  • module the module of a struct.
  • multiple_of is a number greater 0. The value has to be a multiple of this number.
  • not negates the given schema
  • one_of the given data must be valid against exactly one of the given subschemas.
  • pattern_properties specifies schemas for properties by patterns
  • pattern restrict a string to a particular regular expression.
  • properties specifies schemas for properties.
  • property_names a schema to check property names.
  • ref a reference to a schema.
  • required contains a set of required properties.
  • schema declares the used schema.
  • title of the schema.
  • then see if, then, else
  • type specifies the data type for a schema.
  • unique_items disallow duplicate items, if set to true.
  • validator a custom validator. This can be a function, a tuple with module and function name, or a Xema.Validator behaviour.
@type type() ::
  :any
  | :atom
  | :boolean
  | false
  | :float
  | :integer
  | :keyword
  | :list
  | :map
  | nil
  | :number
  | :string
  | :struct
  | true
  | :tuple

The type for the schema.

@type xema() :: struct()

Link to this section Functions

@spec fetch!(t(), Xema.Ref.t() | String.t()) :: t()

Fetches a subschema from the schema by the given pointer.

If schema contains the given pointer with a subschema, then {:ok, schema} is returned otherwise a SchemaError is raised.

@spec fetch(t(), Xema.Ref.t() | String.t()) :: {:ok, t()} | :error

Fetches a subschema from the schema by the given pointer.

If schema contains the given pointer with a subschema, then {:ok, schema} is returned otherwise :error.

@spec keywords() :: [atom()]

Returns all keywords in a list.

The key :data is not a regular keyword and is not in the list.

@spec new(keyword()) :: t()

Returns a %Schema{} for the given keywords in the keyword list.

@spec to_map(t()) :: map()

Returns the %Schema{} as a map. Items which a nil value are not in the map.

@spec types() :: [type()]

Returns all available types in a list.