View Source Parameter.Field (Parameter v0.14.1)
The field inside a Parameter Schema have the following structure:
field :name, :type, opts
:name
- Atom key that defines the field name:type
- Type fromParameter.Types
. For custom types check theParameter.Parametrizable
behaviour.:opts
- Keyword with field 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 mappingfirst_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.
- If an input field use
: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 withParameter.load/3
function. This option should not be used at the same time asdefault
option.:dump_default
- Default value of the field when no value is given when loading withParameter.dump/3
function. This option should not be used at the same time asdefault
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
- Iftrue
the field will be ignored onParameter.load/3
andParameter.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 intoParameter.dump/3
are considered valid.
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