View Source Parameter.Field (Parameter v0.5.3)

This module define the structure of a Field inside a Parameter Schema. The field follow the structure:

field :field_name, :field_type, opts

The :field_type are types implemented on Parameter.Types or custom modules that implements the Parameter.Parametrizable behaviour.

The other options available for the field are:

  • key: This is the key on the external source that will be converted to the param definition. As an example, when receiving data from an external source that uses a camelCase for mapping first_name, this option should be set as "firstName". If this parameter is not set it will default to the field name.
  • default: default value of the field when no value is given to the field.
  • required: defines if the field needs to be present when parsing the input.
  • validator: Validation function that will validate the field after loading.
  • virtual: if true the field will be ignored on Parameter.load/2 and Parameter.dump/2 functions.

As an example having an email field that is required and needs email validation could be implemented this way:

field :email, :string, required: true, validator: &Parameter.Validators.email/1

Link to this section Summary

Link to this section Types

@type t() :: %Parameter.Field{
  default: any(),
  key: binary(),
  name: atom(),
  required: boolean(),
  type: Parameter.Types.t(),
  validator: (... -> any()),
  virtual: boolean()
}

Link to this section Functions

@spec new(opts :: Keyword.t()) :: t() | {:error, binary()}
@spec new!(Keyword.t()) :: t() | no_return()