View Source Parameter.Field (Parameter v0.8.2)
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.Parametrizablebehaviour.: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
camelCasefor 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/3function. This option should not be used at the same time asdefaultoption.:dump_default- Default value of the field when no value is given when loading withParameter.dump/3function. This option should not be used at the same time asdefaultoption.:required- Defines if the field needs to be present when parsing the input.Parameter.load/3will return an error if the value is missing from the input data.:validator- Validation function that will validate the field after loading.:virtual- Iftruethe field will be ignored onParameter.load/3andParameter.dump/3functions.
NOTE: Validation only occurs on
Parameter.load/3. By desgin, data passed intoParameter.dump/3are 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