2
3
4
5
6
7
8
9#include <nlohmann/json-schema.hpp>
21 if (uri
.location() ==
"http://json-schema.org/draft-07/schema") {
26 std::string fn = JSON_SCHEMA_TEST_SUITE_PATH;
29 std::cerr << fn <<
"\n";
31 std::fstream s(fn.c_str());
33 throw std::invalid_argument(
"could not open " + uri
.url() +
" for schema loading\n");
37 }
catch (std::exception &e) {
48 std::vector<
int> T(256, -1);
49 for (
int i = 0; i < 64; i++)
50 T[
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[i]] = i;
54 for (uint8_t c : in) {
59 throw std::invalid_argument(
"base64-decode: unexpected character in encode string: '" + std::string(1, c) +
"'");
61 val = (val << 6) + T[c];
64 out.push_back(
char((val >> valb) & 0xFF));
71static void content(
const std::string &contentEncoding,
const std::string &contentMediaType,
const json &instance)
73 std::string content = instance;
75 if (contentEncoding ==
"base64")
77 else if (contentEncoding !=
"")
78 throw std::invalid_argument(
"unable to check for contentEncoding '" + contentEncoding +
"'");
80 if (contentMediaType ==
"application/json")
81 auto dummy = json::parse(content);
82 else if (contentMediaType !=
"")
83 throw std::invalid_argument(
"unable to check for contentMediaType '" + contentMediaType +
"'");
91 std::cin >> validation;
92 }
catch (std::exception &e) {
93 std::cout << e.what() <<
"\n";
97 size_t total_failed = 0,
100 for (
auto &test_group : validation) {
101 size_t group_failed = 0,
104 std::cout <<
"Testing Group " << test_group[
"description"] <<
"\n";
106 const auto &schema = test_group[
"schema"];
114 for (
auto &test_case : test_group[
"tests"]) {
115 std::cout <<
" Testing Case " << test_case[
"description"] <<
"\n";
121 }
catch (
const std::out_of_range &e) {
123 std::cout <<
" Test Case Exception (out of range): " << e.what() <<
"\n";
125 }
catch (
const std::invalid_argument &e) {
127 std::cout <<
" Test Case Exception (invalid argument): " << e.what() <<
"\n";
129 }
catch (
const std::logic_error &e) {
130 valid = !test_case[
"valid"];
131 std::cout <<
" Not yet implemented: " << e.what() <<
"\n";
134 if (valid == test_case[
"valid"])
135 std::cout <<
" --> Test Case exited with " << valid <<
" as expected.\n";
138 std::cout <<
" --> Test Case exited with " << valid <<
" NOT expected.\n";
143 total_failed += group_failed;
144 total += group_total;
145 std::cout <<
"Group RESULT: " << test_group[
"description"] <<
" "
146 << (group_total - group_failed) <<
" of " << group_total
147 <<
" have succeeded - " << group_failed <<
" failed\n";
148 std::cout <<
"-------------\n";
151 std::cout <<
"Total RESULT: " << (total - total_failed) <<
" of " << total <<
" have succeeded - " << total_failed <<
" failed\n";
void set_root_schema(const json &)
json_validator(schema_loader=nullptr, format_checker=nullptr, content_checker=nullptr)
json validate(const json &) const
std::string location() const
const std::string & path() const
static void loader(const json_uri &uri, json &schema)
static void content(const std::string &contentEncoding, const std::string &contentMediaType, const json &instance)
static std::string base64_decode(const std::string &in)
void default_string_format_check(const std::string &format, const std::string &value)
json draft7_schema_builtin