64 lines
2.6 KiB
ReStructuredText
64 lines
2.6 KiB
ReStructuredText
Validating a variable's value
|
||
================================
|
||
|
||
.. objectives:: Objectives
|
||
|
||
We will see how to create strong validations for the values of our variables,
|
||
that is, validations that go beyond those inherent in the initial type definition.
|
||
|
||
.. prerequisites:: Prerequisites
|
||
|
||
- We assume that Rougail's library is :ref:`installed <installation>` on your computer.
|
||
|
||
- It is possible to retrieve the current state of the various Rougail files manipulated in this tutorial step
|
||
by checking out the corresponding tag of the `rougail-tutorials` git repository.
|
||
Each tag corresponds to a stage of progress in the tutorial.
|
||
Of course, you can also decide to copy/paste or download the tutorial files contents while following the tutorial steps.
|
||
|
||
If you want to follow this tutorial with the help of the corresponding :tutorial:`rougail-tutorials git repository <src/branch/v1.1>`,
|
||
this workshop page corresponds to the tag :tutorial:`v1.1_170 <src/tag/v1.1_170/README.md>`
|
||
in the repository.
|
||
|
||
::
|
||
|
||
git clone https://forge.cloud.silique.fr/stove/rougail-tutorials.git
|
||
git switch --detach v1.1_170
|
||
|
||
A variable with custom validation
|
||
-----------------------------------
|
||
|
||
First, we would like the :term:`operator` to remember not to forget the http protocol, that is to put `http://` at the beginning of our `web_address` variable's value.
|
||
Actually the `web_address`\ 's type requires that the address include the HTTP protocol, namely:
|
||
|
||
::
|
||
|
||
http(s)://<domain_name><directory>
|
||
|
||
|
||
However, in our use case, we want *to force* the HTTPS protocol.
|
||
We don't want `http://`, only `https://`.
|
||
|
||
.. note:: This URL is for DNS over HTTPS, providing greater privacy than using the ISP's DNS,
|
||
which can filter traffic. If it uses `http://`, it doesn't make sense.
|
||
|
||
But there's no existing type for this specific use case (forcing HTTPS).
|
||
|
||
We just said that a valid value must start with `https://` instead of `http://`.
|
||
It is possible, instead of creating a new type, to add custom validation of the variable's value.
|
||
|
||
We call it a :term:`validator`.
|
||
|
||
.. glossary::
|
||
|
||
validator
|
||
|
||
A validator is a Jinja code that parses a variable's value and checks
|
||
that this value corresponds to some stated criteria.
|
||
This is a `validator` parameter of our variable, and another `jinja` sub-parameter
|
||
contains validation code for the variable's value.
|
||
|
||
.. extinclude:: https://forge.cloud.silique.fr/stove/rougail-tutorials/raw/commit/v1.1_170/firefox/60-dns_over_https.yml
|
||
:language: yaml
|
||
:caption: The :file:`firefox/60-dns_over_https.yml` with the jinja validator
|
||
|
||
|