Regexp type with calculation

Objectives

Regexp is a variable specialize type which allows for easy validation of a variable from a regular expression.

Prerequisites

  • We assume that Rougail’s library is installed 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 rougail-tutorials git repository, this workshop page corresponds to the tags v1.1_200 to v1.1_201

git clone https://forge.cloud.silique.fr/stove/rougail-tutorials.git
git switch --detach v1.1_200

A regexp variable

We have already seen the validator mechanism for managing the quality of data values.

This mechanism is undeniably interesting and powerful. But for strings that can be validated using a regular expression, it is preferable to use this type. In this specific case, nothing beats this particular type.

Obviously, the values assigned to a regexp type variable must be of type string.

Here we will add a color variable which will allow us to quickly identify the current proxy via a color code. The expected value is a hex color code (like those used in CSS or HTML).

This regrex must validate that the string begins with a hash symbol (#) followed by a RGB triplet in hexadecimal format (so 3 or 6 lowercase hexadecimal digits (0–9, a–f)).

Here are some examples: #a3f09c or #a3f.

The regular expression is ^#(?:[0-9a-f]{3}){1,2}$.

Let’s write this variable:

The regexp variable color.
%YAML 1.2
---
version: 1.1

proxies:
  description: Proxy configuration
  type: sequence

  title:
    description: Title or Description
    mandatory: false

  color:
    description: Color
    regexp: ^#(?:[0-9a-f]{3}){1,2}$
...

Now let’s test with the following user data file:

The config/02/config.yml user data
1---
2foxyproxy:
3  proxies:
4    - title: My company
5      color: '#66cc66'

With this Rougail CLI:

rougail -m firefox/ -s Firefox -xn FoxyProxy -xd 0 foxyproxy/ --types types/proxy --modes_level basic standard advanced -u yaml -yf config/02/config.yml

We have this output:

╭──────── Caption ────────╮
│ Variable Default value  │
│          Modified value │
╰─────────────────────────╯
Variables:
┣━━ 📂 firefox (Firefox)
┣━━ 📓 proxy_mode (Configure Proxy Access to the Internet): No proxy
┗━━ 📂 dns_over_https (DNS over HTTPS)
    ┗━━ 📓 enable_dns_over_https (Enable DNS over HTTPS): false
┗━━ 📂 foxyproxy (FoxyProxy)
    ┗━━ 📂 proxies (Proxy configuration)
        ┗━━ 📂 title (Title or Description)
            ┣━━ 📓 title (Title or Description): My company ◀ loaded from the 
            YAML file "config/02/config.yml"
            ┗━━ 📓 color (Color): #66cc66 ◀ loaded from the YAML file 
                "config/02/config.yml"

What happens if the value is not valid?

The value is invalid:

🛑 Caution
┗━━ foxyproxy (FoxyProxy)
    ┗━━ proxies (Proxy configuration)
        ┗━━ color (Color)
            ┣━━ 🔔 the value "not a color" is an invalid string, invalid value, 
            it will be ignored when loading from the YAML file 
            "config/03/config.yml"
            ┗━━ 🛑 mandatory variable at index "0" but has no value

Calculate a random color

For those who follow the tutorial with the help of the git repository

Now you need to checkout the v1.1_201 version:

git switch --detach v1.1_201

Why not offer a default value to the user?

If the user has a specific color in mind, they can specify it. Otherwise, Rougail will suggest a default value.

We’ve already seen how to use Jinja for validation and access control calculations.

We’ve also seen how to calculate the default value by retrieving the value of another variable.

It’s entirely possible to calculate the default value from a Jinja template:

Calculate the default variable color.
%YAML 1.2
---
version: 1.1

proxies:
  description: Proxy configuration
  type: sequence

  title:
    description: Title or Description
    mandatory: false

  color:
    description: Color
    regexp: ^#(?:[0-9a-f]{3}){1,2}$
    default:
      jinja: >-
        #{%- for i in range(6) -%}{{- '0123456789abcdef' | random -}}{%- endfor -%}
      description: random color value
...

Now let’s test with the following user data file:

The config/02/config.yml user data
1---
2foxyproxy:
3  proxies:
4    - title: My company

With this Rougail CLI:

rougail -m firefox/ -s Firefox -xn FoxyProxy -xd 0 foxyproxy/ --types types/proxy --modes_level basic standard advanced -u yaml -yf config/02/config.yml

We have this output:

╭──────── Caption ────────╮
│ Variable Default value  │
│          Modified value │
╰─────────────────────────╯
Variables:
┣━━ 📂 firefox (Firefox)
┣━━ 📓 proxy_mode (Configure Proxy Access to the Internet): No proxy
┗━━ 📂 dns_over_https (DNS over HTTPS)
    ┗━━ 📓 enable_dns_over_https (Enable DNS over HTTPS): false
┗━━ 📂 foxyproxy (FoxyProxy)
    ┗━━ 📂 proxies (Proxy configuration)
        ┗━━ 📂 title (Title or Description)
            ┣━━ 📓 title (Title or Description): My company ◀ loaded from the 
            YAML file "config/02/config.yml"
            ┗━━ 📓 color (Color): #65de4c

As you can see, Rougail offers a default value!

Key points

  • We can easily create a variable that valid value via a regexp

  • We can calculate a default value with Jinja template