MQTTSuite
Loading...
Searching...
No Matches
issue-117-format-error.cpp
Go to the documentation of this file.
1// issue-00-format-error.cpp
2
3#include "nlohmann/json-schema.hpp"
4#include "nlohmann/json.hpp"
5#include <iostream>
6
7static int error_count = 0;
8
9#define CHECK_THROW(x, msg)
10 {
11 bool fail = false;
12 try {
13 x;
14 } catch (std::exception &) {
15 fail = true;
16 }
17 if (fail == false) {
18 ++error_count;
19 std::cout << msg << std::endl;
20 }
21 }
22
23#define CHECK_NO_THROW(x, msg)
24 {
25 bool fail = false;
26 std::string exception_error;
27 try {
28 x;
29 } catch (std::exception & e) {
30 fail = true;
31 exception_error = e.what();
32 }
33 if (fail == true) {
34 ++error_count;
35 std::cout << msg << ": " << exception_error << std::endl;
36 }
37 }
38
39using json = nlohmann::json;
41
43{
44 "type": "object",
45 "properties": {
46 "str": {
47 "type": "string",
48 "format": "placeholder"
49 }
50 }
51}
52)");
53
54int main()
55{
56 // check that if we get validator without format checker we get error at schema loading
57 validator without_format_checker;
58
59 CHECK_THROW(without_format_checker.set_root_schema(schema_with_format), "validator without format checker must fail at schema loading");
60
61 // check that with format checker all works fine
62 validator with_format_checker{nullptr, [](const std::string &, const std::string &) {}};
63
64 CHECK_NO_THROW(with_format_checker.set_root_schema(schema_with_format), "schema must be succesed by validator with format checker");
65
66 CHECK_NO_THROW(with_format_checker.validate(json{{"str", "placeholder"}}), "validator must not throw while validation schema with format");
67
68 return error_count;
69}
nlohmann::json_schema::json_validator validator
nlohmann::json json
json_validator(schema_loader=nullptr, format_checker=nullptr, content_checker=nullptr)
int main()
Definition format.cpp:34
#define CHECK_THROW(x, msg)
static int error_count
#define CHECK_NO_THROW(x, msg)
json schema_with_format