View Source Parameter.Field (Parameter v0.12.0)

The field inside a Parameter Schema have the following structure:

field :name, :type, opts

options

Options

  • :key - This is the key from the params that will be converted to the field schema. Examples:

    • If an input field use camelCase for mapping first_name, this option should be set as "firstName".
    • If an input field use the same case for the field definition, this key can be ignored.
  • :default - Default value of the field when no value is given.

  • :load_default - Default value of the field when no value is given when loading with Parameter.load/3 function. This option should not be used at the same time as default option.

  • :dump_default - Default value of the field when no value is given when loading with Parameter.dump/3 function. This option should not be used at the same time as default option.

  • :required - Defines if the field needs to be present when parsing the input. Parameter.load/3 will return an error if the value is missing from the input data.

  • :validator - Validation function that will validate the field after loading.

  • :virtual - If true the field will be ignored on Parameter.load/3 and Parameter.dump/3 functions.

  • :on_load - Function to specify how to load the field. The function must have two arguments where the first one is the field value and the second one will be the data to be loaded. Should return {:ok, value} or {:error, reason} tuple.

  • :on_dump - Function to specify how to dump the field. The function must have two arguments where the first one is the field value and the second one will be the data to be dumped. Should return {:ok, value} or {:error, reason} tuple.

NOTE: Validation only occurs on Parameter.load/3. By desgin, data passed into Parameter.dump/3 are considered valid.

example

Example

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(),
  dump_default: any(),
  key: binary(),
  load_default: any(),
  name: atom(),
  on_dump: (... -> any()) | nil,
  on_load: (... -> any()) | nil,
  required: boolean(),
  type: Parameter.Types.t(),
  validator: (... -> any()) | nil,
  virtual: boolean()
}