View Source Parameter.Field (Parameter v0.8.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
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.
NOTE: Validation only occurs on
Parameter.load/3
. By desgin, data passed intoParameter.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