Skip to content

Commit 6c3f739

Browse files
committed
Add more examples
1 parent 402d544 commit 6c3f739

17 files changed

Lines changed: 776 additions & 28 deletions

docs/index.md

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -651,16 +651,17 @@ See examples below.
651651
```cpp
652652
#include <dracosha/validator/validator.hpp>
653653
#include <dracosha/validator/validate.hpp>
654+
#include <dracosha/validator/operators/lexicographical.hpp>
654655
#include <dracosha/validator/prevalidation/set_validated.hpp>
655656

656657
// define structure with member variables and member setter method
657658
struct Foo
658659
{
659660
std::string bar_value;
660-
661+
661662
uint32_t other_value;
662663
size_t some_size;
663-
664+
664665
void set_bar_value(std::string val)
665666
{
666667
bar_value=std::move(val);
@@ -694,39 +695,34 @@ using namespace DRACOSHA_VALIDATOR_NAMESPACE;
694695

695696
int main()
696697
{
698+
// define validator of custom properties
699+
auto v=validator(
700+
_[bar_value](ilex_ne,"UNKNOWN"), // case insensitive lexicographical not equal
701+
_[other_value](gte,1000)
702+
);
697703

698-
// define validator of custom properties
699-
auto v=validator(
700-
_[bar_value](ilex_ne,"UNKNOWN"), // case insensitive lexicographical not equal
701-
_[other_value](gte,1000)
702-
);
704+
Foo foo_instance;
703705

704-
Foo foo_instance;
706+
error_report err;
705707

706-
error_report err;
708+
// call setter with valid data
709+
set_validated(foo_instance,_[bar_value],"Hello world",v,err);
710+
assert(!err);
707711

708-
// call setter with valid data
709-
set_validated(foo_instance,bar_value,"Hello world",v,err);
710-
if (!err)
711-
{
712-
// object's member is set
713-
}
712+
// call setter with invalid data
713+
set_validated(foo_instance,_[bar_value],"unknown",v,err);
714+
assert(err);
715+
if (err)
716+
{
717+
// object's member is not set
718+
std::cerr << err.message() << std::endl;
719+
assert(err.message()==std::string("bar_value must be not equal to UNKNOWN"));
720+
}
714721

715-
// call setter with invalid data
716-
set_validated(foo_instance,bar_value,"unknown",v,err);
717-
if (err)
718-
{
719-
// object's member is not set
720-
std::cerr << err.message() << std::endl;
721-
/* prints:
722-
723-
"bar_value must be not equal to UNKNOWN"
724-
725-
*/
722+
std::cout << "Example 26 done" << std::endl;
723+
return 0;
726724
}
727725

728-
return 0;
729-
}
730726
```
731727
732728
#### unset_validated
@@ -2094,6 +2090,7 @@ To use aggregation with modifier the modifier must be provided as an argument to
20942090
See examples below.
20952091

20962092
```cpp
2093+
#include <map>
20972094
#include <dracosha/validator/validator.hpp>
20982095
#include <dracosha/validator/properties/pair.hpp>
20992096

@@ -2512,6 +2509,8 @@ See example below.
25122509
#include <dracosha/validator/value_transformer.hpp>
25132510
#include <dracosha/validator/validator.hpp>
25142511

2512+
using namespace DRACOSHA_VALIDATOR_NAMESPACE;
2513+
25152514
namespace {
25162515
// define handler that returns a size of provided string
25172516
size_t string_size(const std::string& val) noexcept

examples/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@ SET(SOURCES
1313
example10_custom_property_flag.cpp
1414
example11_heterogeneous_property_implicit.cpp
1515
example12_heterogeneous_property_explicit.cpp
16+
example13_variadic_property.cpp
17+
example14_variadic_property_aggregation.cpp
18+
example15_operator_in.cpp
19+
example16_operator_negation.cpp
20+
example17_custom_operator.cpp
21+
example18_move_copy.cpp
22+
example19_lazy_operands.cpp
23+
example20_operand_other_member.cpp
24+
example21_operand_sample_object.cpp
25+
example22_element_aggregation.cpp
26+
example23_tree_validation.cpp
27+
example24_partial_validation.cpp
28+
example25_validate_evaluation_result.cpp
29+
example26_prevalidate_set_member.cpp
30+
example27_prevalidate_set_custom_property.cpp
1631
)
1732

1833
ENABLE_TESTING(true)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#undef NDEBUG
2+
3+
#include <dracosha/validator/variadic_property.hpp>
4+
#include <dracosha/validator/validator.hpp>
5+
#include <dracosha/validator/adapters/reporting_adapter.hpp>
6+
using namespace DRACOSHA_VALIDATOR_NAMESPACE;
7+
8+
// define a structure with variadic properties
9+
struct WithChild
10+
{
11+
// method with one argument
12+
int child(int val) const noexcept
13+
{
14+
return val+1;
15+
}
16+
17+
// method with two arguments
18+
int child_word(int val, const std::string& word) const noexcept
19+
{
20+
return val+static_cast<int>(word.size());
21+
}
22+
23+
// method to check if object "has" child_world with provided arguments
24+
bool has_child_word(int val, const std::string& word) const noexcept
25+
{
26+
return val>10 && word.size()>=5;
27+
}
28+
};
29+
30+
// define variadic property
31+
DRACOSHA_VALIDATOR_VARIADIC_PROPERTY(child)
32+
// define variadic property to be used as "exists" checker
33+
DRACOSHA_VALIDATOR_VARIADIC_PROPERTY(has_child_word)
34+
// define variadic property with "exists" checker
35+
DRACOSHA_VALIDATOR_VARIADIC_PROPERTY_HAS(child_word,has_child_word)
36+
37+
int main()
38+
{
39+
// define object
40+
WithChild o1;
41+
42+
// define validation adapter with text reports
43+
std::string rep;
44+
auto ra1=make_reporting_adapter(o1,rep);
45+
46+
// variadic property with single argument and member notation
47+
auto v1=validator(
48+
_[child][varg(1)](eq,30)
49+
);
50+
assert(!v1.apply(ra1));
51+
assert(rep==std::string("child(1) must be equal to 30"));
52+
rep.clear();
53+
54+
// variadic property with single argument and property notation
55+
auto v2=validator(
56+
child(1)(eq,30)
57+
);
58+
assert(!v2.apply(ra1));
59+
assert(rep==std::string("child(1) must be equal to 30"));
60+
rep.clear();
61+
62+
// variadic property with two arguments and member notation
63+
auto v3=validator(
64+
_[child_word][varg(20)][varg("hello")](eq,30)
65+
);
66+
assert(!v3.apply(ra1));
67+
assert(rep==std::string("child_word(20,hello) must be equal to 30"));
68+
rep.clear();
69+
70+
// variadic property with two arguments and property notation
71+
auto v4=validator(
72+
child_word(20,"hello")(eq,30)
73+
);
74+
assert(!v4.apply(ra1));
75+
assert(rep==std::string("child_word(20,hello) must be equal to 30"));
76+
rep.clear();
77+
78+
// check if variadic property exists
79+
auto v5=validator(
80+
_[child_word][20]["hello"](exists,true)
81+
);
82+
assert(v5.apply(o1));
83+
auto v6=validator(
84+
_[child_word][5]["hello"](exists,true)
85+
);
86+
assert(!v6.apply(o1));
87+
88+
std::cout << "Example 13 done" << std::endl;
89+
return 0;
90+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#undef NDEBUG
2+
3+
#include <dracosha/validator/variadic_property.hpp>
4+
#include <dracosha/validator/validator.hpp>
5+
using namespace DRACOSHA_VALIDATOR_NAMESPACE;
6+
7+
// sample struct
8+
struct Matrix
9+
{
10+
size_t element(size_t i, size_t j) const noexcept
11+
{
12+
return i+j;
13+
}
14+
15+
size_t count_i() const noexcept
16+
{
17+
return 1000;
18+
}
19+
20+
size_t count_j() const noexcept
21+
{
22+
return 100;
23+
}
24+
};
25+
26+
// define variadic property
27+
DRACOSHA_VALIDATOR_VARIADIC_PROPERTY(element)
28+
29+
// define "end" properties
30+
DRACOSHA_VALIDATOR_PROPERTY(count_i)
31+
DRACOSHA_VALIDATOR_PROPERTY(count_j)
32+
33+
int main()
34+
{
35+
Matrix m1;
36+
37+
// check if each element is greater than 100 - fails
38+
auto v1=validator(
39+
_[element][varg(ALL,count_i)][varg(ALL,count_j)](gt,100)
40+
);
41+
assert(!v1.apply(m1));
42+
43+
// check if at least one element is greater than 100 - succeeds
44+
auto v2=validator(
45+
_[element][varg(ANY,count_i)][varg(ANY,count_j)](gt,100)
46+
);
47+
assert(v2.apply(m1));
48+
49+
std::cout << "Example 14 done" << std::endl;
50+
return 0;
51+
}

examples/example15_operator_in.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#undef NDEBUG
2+
3+
#include <dracosha/validator/validator.hpp>
4+
#include <dracosha/validator/operators.hpp>
5+
#include <dracosha/validator/range.hpp>
6+
#include <dracosha/validator/interval.hpp>
7+
using namespace DRACOSHA_VALIDATOR_NAMESPACE;
8+
9+
int main()
10+
{
11+
// define value for validation
12+
size_t val=90;
13+
14+
// check if the value is in range
15+
auto v1=validator(in,range({70,80,90,100}));
16+
assert(v1.apply(val));
17+
18+
// check if the value is in interval
19+
auto v2=validator(in,interval(80,100));
20+
assert(v2.apply(val));
21+
22+
std::cout << "Example 15 done" << std::endl;
23+
return 0;
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#undef NDEBUG
2+
3+
#include <map>
4+
#include <dracosha/validator/validator.hpp>
5+
#include <dracosha/validator/operators.hpp>
6+
using namespace DRACOSHA_VALIDATOR_NAMESPACE;
7+
8+
int main()
9+
{
10+
// container to validate
11+
std::map<std::string,uint32_t> m1={{"key1",100}};
12+
13+
// validator with original operator
14+
auto v1=validator(
15+
_["key1"](eq,100)
16+
);
17+
assert(v1.apply(m1));
18+
19+
// validator with negated operator
20+
auto v2=validator(
21+
_["key1"](_n(eq),100)
22+
);
23+
assert(!v2.apply(m1));
24+
25+
std::cout << "Example 16 done" << std::endl;
26+
return 0;
27+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#undef NDEBUG
2+
3+
#include <dracosha/validator/validator.hpp>
4+
#include <dracosha/validator/validate.hpp>
5+
#include <dracosha/validator/operators/operator.hpp>
6+
using namespace DRACOSHA_VALIDATOR_NAMESPACE;
7+
8+
// new operator implementation
9+
struct simple_eq_t : public op<simple_eq_t>
10+
{
11+
// comparison operator with two templated arguments
12+
template <typename T1, typename T2>
13+
constexpr bool operator() (const T1& a, const T2& b) const
14+
{
15+
return a==b;
16+
}
17+
18+
// direct error description
19+
constexpr static const char* description="must be simply equal to";
20+
// negated error description
21+
constexpr static const char* n_description="must be not simply equal to";
22+
};
23+
// callable object to be used in validators
24+
constexpr simple_eq_t simple_eq{};
25+
26+
int main()
27+
{
28+
// define validator with custom operator
29+
auto v1=validator(simple_eq,100);
30+
31+
error_report err;
32+
33+
// validate good value
34+
validate(100,v1,err);
35+
assert(!err);
36+
37+
// validate bad value
38+
validate(90,v1,err);
39+
assert(err);
40+
std::cerr << err.message() << std::endl;
41+
assert(err.message()==std::string("must be simply equal to 100"));
42+
43+
std::cout << "Example 17 done" << std::endl;
44+
return 0;
45+
}

0 commit comments

Comments
 (0)