dd v0.0.3 DD.Type.Behaviour behaviour
Link to this section Summary
Callbacks
Convert a string external representation of a value into into it’s internal representation
A record is defined using something like
Convert a value to a string that will be suitable for display (for example as the value of an tag
Validates that the given value is consistent with a type and that it obeys any constraints in it’s specs
Link to this section Callbacks
Convert a string external representation of a value into into it’s internal representation
from_options(atom(), keyword()) :: {module(), atom(), Keyword.t()}
A record is defined using something like
defrecord Person do
string(:name, min: 2, max: 50)
date(:dob)
end
The defrecord
code calls the type module’s from_spec
function
for each field (so the type modules DD.Type.String and
DD.Type.Date would be called about).
This function returns a field definition structure, having first validated (and possibly manipulated) the arguments.
In the first example above, from_spec
would we called with
DD.String.from_options(:name, [ min: 2, max: 50 ])
Values that are stored in the field structure have potentially been
updated to work with this type. For example, if you pass a string as
an option to DD.Type.String’s matches:
option, it will be returned
as a Regex.
Convert a value to a string that will be suitable for display (for example as the value of an tag.
Validates that the given value is consistent with a type and that it obeys any constraints in it’s specs.
For example, the validation for DD.Type.String is:
def validate(value, _) when not is_binary(value) do
"should be a string"
end
def validate(value, specs) do
validate_length(String.length(value), specs[:min], specs[:max])
|| validate_matches(value, specs[:matches])
end