File: composing_validators.md

package info (click to toggle)
reflect-cpp 0.18.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 12,524 kB
  • sloc: cpp: 44,484; python: 131; makefile: 30; sh: 3
file content (47 lines) | stat: -rw-r--r-- 1,780 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Composing validators

Sometimes the conditions you want to impose are more complex than a simple minimum or a simple regex pattern. That is why reflect-cpp allows you to compose them using the operators `rfl::AnyOf`, `rfl::AllOf` and `rfl::OneOf`.

- `rfl::AnyOf` requires that *at least one* of the conditions contained therein be true. It thus works like an OR operator.
- `rfl::AllOf` requires that *all* of the conditions contained therein be true. It thus works like an AND operator.
- `rfl::OneOf` requires that *exactly one* of the conditions contained therein be true. It thus works like a XOR operator.

For instance, if you want to ensure that a person's age is a reasonable number, you can impose two conditions as follows:

```cpp
using Age = rfl::Validator<int,
                           rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;

struct Person {
  rfl::Rename<"firstName", std::string> first_name;
  rfl::Rename<"lastName", std::string> last_name;
  Age age;
};
```

However, if you pass more than one constraint to `rfl::Validator`, they will be automatically connected using `rfl::AllOf`,
so you might as well write:

```cpp
using Age = rfl::Validator<int, rfl::Minimum<0>, rfl::Maximum<130>>;

...
```

You can also nest composed validators. For instance, in this case the age must either be 0-10 or 40-130.

```cpp
using Age = rfl::Validator<
    int,
    rfl::AnyOf<rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<10>>,
               rfl::AllOf<rfl::Minimum<40>, rfl::Maximum<130>>>>;
```

You can also compose regex patterns:

```cpp
using TableOrColumnName = rfl::Validator<
    std::string,
    rfl::AnyOf<rfl::PatternValidator<R"(^[A-Z]+(?:_[A-Z]+)*$)", "TableName">,
               rfl::PatternValidator<R"(^[a-z]+(?:_[a-z]+)*$)", "ColumnName">>>;
```