Configuration
v0.1.4
This page lists the application config keys of GuardedStruct 0.1.4, the options of use GuardedStruct, the packages that some features need, and the callbacks that produce error messages.
Application config
Set these under config :guarded_struct.
| Key | Value | Default | Effect |
|---|---|---|---|
derive_extensions | List of modules | [] | Extension modules whose ops every guarded module can use. See Add your own derive ops. The list is read at run time and cached. |
validate_derive | Module or list of modules | none | Fallback modules for validate ops. Each defines validate(op, value, field). Read at run time. The validate_derive option of the guardedstruct section is tried first. |
sanitize_derive | Module or list of modules | none | Fallback modules for sanitize ops. Each defines sanitize(value, op). Read at run time. The sanitize_derive option of the guardedstruct section is tried first. |
message_backend | Module | GuardedStruct.Messages | The module that returns every error message. Read when GuardedStruct compiles. |
# config/config.exs
config :guarded_struct,
derive_extensions: [MyApp.Derives],
validate_derive: [MyApp.MoreValidators],
sanitize_derive: [MyApp.MoreSanitizers],
message_backend: MyApp.MessagesOptions of use GuardedStruct
| Option | Value | Effect |
|---|---|---|
derive_extensions | List of modules, which can include :config once | Chooses the extensions for this module. :config stands for the application config list. [] means none. A value that is not a list raises ArgumentError. |
Packages
GuardedStruct 0.1.4 needs Elixir 1.17 or later. The first four packages in this table come with GuardedStruct. Add the others to your own deps when you use the feature next to them.
| Package | Version in mix.exs | Needed for |
|---|---|---|
spark | ~> 2.7 | The DSL. Always installed. |
splode | ~> 0.3 | GuardedStruct.Errors. Always installed. |
telemetry | ~> 1.0 | Build events. Always installed. |
html_sanitize_ex | ~> 1.5 | The HTML sanitize ops. Always installed. |
jason | ~> 1.4 | json: true, unless you use the built-in JSON module of Elixir 1.18 and later. |
email_checker | ~> 0.2.4 | The email validate op. |
ex_url | ~> 2.0.2 | The tell, geo_url and location validate ops. |
ex_phone_number | ~> 0.4.11 | The tell=code form, together with ex_url. |
ash | ~> 3.0 | The GuardedStruct.AshResource extension. |
igniter | ~> 0.8.0 | mix igniter.install guarded_struct. |
An optional package is looked up when the op runs, so the order of your dependencies does not matter, in mix.exs or in Mix.install. An op whose package is missing fails with its own name as the action, and the message names the package to add.
Message callbacks
A module that uses GuardedStruct.Messages gets a default for every callback, and you override the ones you want. Each callback returns a string.
| Callback | Argument | Message for |
|---|---|---|
required_fields/0 | none | required_fields |
authorized_fields/0 | none | authorized_fields |
builder/0 | none | bad_parameters |
list_builder_type/0 | none | A list was expected for a list-valued child of a conditional field. |
message_exception/0, message_exception/1 | none; the exception | The first line of the generated Error exception. |
check_dependent_keys/1 | {field, path}, where path is a list of atoms | dependent_keys |
domain_field_status/1, force_domain_field_status/1 | The field name | domain_parameters, without and with a missing required path |
is_type/1 | {field, type} | The type ops such as string and integer. |
max_len_binary/1, max_len_integer/1, max_len_range/1, max_len_list/1 | {field, length} | max_len, by kind of value |
min_len_binary/1, min_len_integer/1, min_len_range/1, min_len_list/1 | {field, length} | min_len, by kind of value |
max_len/1, min_len/1 | The field name | max_len or min_len written without a number |
not_empty_binary/1, not_empty_list/1, not_empty_map/1, not_empty/1 | The field name | not_empty, by kind of value |
url_scheme/1, url_host/1, url_gethostbyname/1, url/1 | The field name | url, by the cause of the failure |
convert_enum_output/1, enum/1 | The field name | enum, for a value outside the list and for a value of the wrong type |
validate_unexpected/1 | The field name | type |
date_binary/1 | The field name | date |
location_url/1 | The field name | geo_url and location |
| Every other callback | The field name | The op with the same name: uuid/1, email_r/1, slug/1, hostname/1, port_number/1, semver/1, each/1, either/1, custom/1 and so on. |
The behaviour also declares register_struct/0, list_builder/0, list_builder_field_exception/0, field/1 and field_type/1. Version 0.1.4 does not call them.