mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 10:46:18 +08:00
Merge branch 'main' of github.com:getml/reflect-cpp
This commit is contained in:
+22
-12
@@ -20,27 +20,37 @@
|
||||
|
||||
1.8) [Standard containers](https://github.com/getml/reflect-cpp/blob/main/docs/standard_containers.md) - Describes how reflect-cpp treats containers in the standard library.
|
||||
|
||||
## 2) Custom classes
|
||||
## 2) Validation
|
||||
|
||||
2.1) [Custom classes](https://github.com/getml/reflect-cpp/blob/main/docs/custom_classes.md) - For custom classes with private fields.
|
||||
2.1) [Regex patterns](https://github.com/getml/reflect-cpp/blob/main/docs/patterns.md) - For requiring that strings follow used-defined regex patterns.
|
||||
|
||||
2.2) [Custom parsers for your classes](https://github.com/getml/reflect-cpp/blob/main/docs/custom_parser.md) - For custom classes with private fields that you want to leave absolutely untouched.
|
||||
2.2) [Validating numbers](https://github.com/getml/reflect-cpp/blob/main/docs/validating_numbers.md) - For imposing constraints on numbers.
|
||||
|
||||
## 3) Useful helper functions and classes
|
||||
2.3) [Composing validators](https://github.com/getml/reflect-cpp/blob/main/docs/composing_validators.md) - For defining more complex validators using operators such as `rfl::AllOf` or `rfl::AnyOf`.
|
||||
|
||||
3.1) [rfl::replace](https://github.com/getml/reflect-cpp/blob/main/docs/replace.md) - For replacing one or several fields in a struct.
|
||||
2.4) [Size validation](https://github.com/getml/reflect-cpp/blob/main/docs/size_validation.md) - For imposing size constraints on containers such as `std::vector` or `std::string`.
|
||||
|
||||
3.2) [rfl::as](https://github.com/getml/reflect-cpp/blob/main/docs/as.md) - For casting structs as other structs.
|
||||
## 3) Custom classes
|
||||
|
||||
3.3) [rfl::NamedTuple](https://github.com/getml/reflect-cpp/blob/main/docs/named_tuple.md) - For structural typing
|
||||
3.1) [Custom classes](https://github.com/getml/reflect-cpp/blob/main/docs/custom_classes.md) - For custom classes with private fields.
|
||||
|
||||
3.4) [rfl::Result](https://github.com/getml/reflect-cpp/blob/main/docs/result.md) - For error handling without exceptions
|
||||
3.2) [Custom parsers for your classes](https://github.com/getml/reflect-cpp/blob/main/docs/custom_parser.md) - For custom classes with private fields that you want to leave absolutely untouched.
|
||||
|
||||
## 4) Supported formats
|
||||
## 4) Useful helper functions and classes
|
||||
|
||||
4.1) [JSON](https://github.com/getml/reflect-cpp/blob/main/docs/json.md)
|
||||
4.1) [rfl::replace](https://github.com/getml/reflect-cpp/blob/main/docs/replace.md) - For replacing one or several fields in a struct.
|
||||
|
||||
## 5) Advanced topics
|
||||
4.2) [rfl::as](https://github.com/getml/reflect-cpp/blob/main/docs/as.md) - For casting structs as other structs.
|
||||
|
||||
5.1) [Supporting your own format](https://github.com/getml/reflect-cpp/blob/main/docs/supporting_your_own_format.md) - For supporting your own serialization and deserialization formats.
|
||||
4.3) [rfl::NamedTuple](https://github.com/getml/reflect-cpp/blob/main/docs/named_tuple.md) - For structural typing
|
||||
|
||||
4.4) [rfl::Result](https://github.com/getml/reflect-cpp/blob/main/docs/result.md) - For error handling without exceptions
|
||||
|
||||
## 5) Supported formats
|
||||
|
||||
5.1) [JSON](https://github.com/getml/reflect-cpp/blob/main/docs/json.md)
|
||||
|
||||
## 6) Advanced topics
|
||||
|
||||
6.1) [Supporting your own format](https://github.com/getml/reflect-cpp/blob/main/docs/supporting_your_own_format.md) - For supporting your own serialization and deserialization formats.
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
# 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::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"age", Age> age;
|
||||
};
|
||||
```
|
||||
|
||||
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">>>;
|
||||
```
|
||||
@@ -0,0 +1,27 @@
|
||||
# `rfl::Pattern`
|
||||
|
||||
`rfl::Pattern` can be used to verify that a string fits a particular pattern, the latter of which is expressed as a regular expression.
|
||||
|
||||
A common problem for backend applications or data pipelines is SQL injection. `rfl::Pattern` can be used to limit the allowed characters
|
||||
users are able to pass. For instance, for table names, we might only allow uppercase characters and
|
||||
underscores:
|
||||
|
||||
```cpp
|
||||
using TableName = rfl::Pattern<R"(^[A-Z]+(?:_[A-Z]+)*$)", "TableName">;
|
||||
```
|
||||
|
||||
Because the regex is encoded at compile time, you can be certain that whenever you use the type `TableName`, the string contained
|
||||
therein will fit the pattern. This is a very strong safeguard against SQL injection.
|
||||
|
||||
Note that use also have to give a reasonable name to the pattern. That is, because reflect-cpp emphasizes readable error messages and regex patterns can be somewhat hard to decipher at times.
|
||||
|
||||
To retrieve the underlying string, you can use the `.value()` method.
|
||||
|
||||
reflect-cpp currently contains the following predefined regex patterns:
|
||||
|
||||
- `rfl::Base64Encoded`
|
||||
- `rfl::Email`
|
||||
- `rfl::UUIDv1`
|
||||
- `rfl::UUIDv2`
|
||||
- `rfl::UUIDv3`
|
||||
- `rfl::UUIDv4`
|
||||
@@ -0,0 +1,18 @@
|
||||
# `rfl::Size`
|
||||
|
||||
`rfl::Size` can be used to impose constraints on any container that contains the method `.size()`, which includes `std::string`.
|
||||
|
||||
For instance, you can a require a vector to be non-empty as follows:
|
||||
|
||||
```cpp
|
||||
using NonEmptyStringVector =
|
||||
rfl::Validator<std::vector<std::string>, rfl::Size<rfl::Minimum<1>>>;
|
||||
```
|
||||
|
||||
Or you could require it to contain exactly zero or three elements, using validator composition:
|
||||
|
||||
```cpp
|
||||
using NoneOrThree =
|
||||
rfl::Validator<std::vector<std::string>,
|
||||
rfl::Size<rfl::AnyOf<rfl::EqualTo<0>, rfl::EqualTo<3>>>>;
|
||||
```
|
||||
@@ -0,0 +1,22 @@
|
||||
# Validating numbers
|
||||
|
||||
reflect-cpp can also be used to impose condition on numbers. For instance,
|
||||
if you want an integer that is greater or equal to 10, you can do the following:
|
||||
|
||||
```cpp
|
||||
using IntGreaterThan10 = rfl::Validator<int, rfl::Minimum<10>>;
|
||||
```
|
||||
|
||||
When you then use the type `IntGreaterThan10` inside you `rfl::Field`, the condition will be automatically
|
||||
validated.
|
||||
|
||||
The underlying value can be retrieved using the `.value()` method.
|
||||
|
||||
The current conditions are currently supported by reflect-cpp:
|
||||
|
||||
- `rfl::EqualTo`
|
||||
- `rfl::NotEqualTo`
|
||||
- `rfl::Minimum`
|
||||
- `rfl::ExclusiveMinimum`
|
||||
- `rfl::Maximum`
|
||||
- `rfl::ExclusiveMaximum`
|
||||
Reference in New Issue
Block a user