Validating Our Templates
Let’s learn how to validate our templates.
The template fields represent a sterner test. The checker and generators fields will require us to validate lists and functions. Still, our simple framework that’s based on composition will make quick work of them.
Declaring the main module and core functions
Let’s start with the basic errors function that composes validations over each field. As before, we’ll enumerate required and optional fields, in lib/mastery/boundary/template_validator.ex, like this:
The technique works exactly as it did in the QuizValidator.
Let’s try out a few validations on the template as a whole:
Executable
Output
iex(1)> alias Mastery.Boundary.{TemplateValidator}
[Mastery.Boundary.TemplateValidator]
iex(2)> checker = fn(sub, answer) ->
...(2)> sub[:left] + sub[:right] == String.to_integer(answer)
...(2)> end
#Function<13.126501267/2 in :erl_eval.expr/5>
iex(3)> template =
...(3)> [
...(3)> name: :single_digit_addition,
...(3)> ...