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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
// Copyright 2020 Daniel Parker
// Distributed under the Boost license, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// See https://github.com/danielaparker/jsoncons for latest version
#ifndef JSONCONS_JSONSCHEMA_JSON_VALIDATOR_HPP
#define JSONCONS_JSONSCHEMA_JSON_VALIDATOR_HPP
#include <jsoncons/config/jsoncons_config.hpp>
#include <jsoncons/uri.hpp>
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpointer/jsonpointer.hpp>
#include <jsoncons_ext/jsonschema/keyword_validator_factory.hpp>
#include <cassert>
#include <set>
#include <sstream>
#include <iostream>
#include <cassert>
#include <functional>
namespace jsoncons {
namespace jsonschema {
class throwing_error_reporter : public error_reporter
{
void do_error(const validation_output& o) override
{
JSONCONS_THROW(validation_error(o.message()));
}
};
class fail_early_reporter : public error_reporter
{
void do_error(const validation_output&) override
{
}
public:
fail_early_reporter()
: error_reporter(true)
{
}
};
using error_reporter_t = std::function<void(const validation_output& o)>;
struct error_reporter_adaptor : public error_reporter
{
error_reporter_t reporter_;
error_reporter_adaptor(const error_reporter_t& reporter)
: reporter_(reporter)
{
}
private:
void do_error(const validation_output& e) override
{
reporter_(e);
}
};
template <class Json>
class json_validator
{
std::shared_ptr<json_schema<Json>> root_;
public:
json_validator(std::shared_ptr<json_schema<Json>> root)
: root_(root)
{
}
json_validator(json_validator &&) = default;
json_validator &operator=(json_validator &&) = default;
json_validator(json_validator const &) = delete;
json_validator &operator=(json_validator const &) = delete;
~json_validator() = default;
// Validate input JSON against a JSON Schema with a default throwing error reporter
Json validate(const Json& instance) const
{
throwing_error_reporter reporter;
jsonpointer::json_pointer instance_location("#");
Json patch(json_array_arg);
root_->validate(instance, instance_location, reporter, patch);
return patch;
}
// Validate input JSON against a JSON Schema
bool is_valid(const Json& instance) const
{
fail_early_reporter reporter;
jsonpointer::json_pointer instance_location("#");
Json patch(json_array_arg);
root_->validate(instance, instance_location, reporter, patch);
return reporter.error_count() == 0;
}
// Validate input JSON against a JSON Schema with a provided error reporter
template <class Reporter>
typename std::enable_if<type_traits::is_unary_function_object_exact<Reporter,void,validation_output>::value,Json>::type
validate(const Json& instance, const Reporter& reporter) const
{
jsonpointer::json_pointer instance_location("#");
Json patch(json_array_arg);
error_reporter_adaptor adaptor(reporter);
root_->validate(instance, instance_location, adaptor, patch);
return patch;
}
};
} // namespace jsonschema
} // namespace jsoncons
#endif // JSONCONS_JSONSCHEMA_JSON_VALIDATOR_HPP
|