File: json_schema.hpp

package info (click to toggle)
jsoncons 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,584 kB
  • sloc: cpp: 136,382; sh: 33; makefile: 5
file content (236 lines) | stat: -rw-r--r-- 8,406 bytes parent folder | download
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright 2013-2025 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_EXT_JSONSCHEMA_JSON_SCHEMA_HPP
#define JSONCONS_EXT_JSONSCHEMA_JSON_SCHEMA_HPP

#include <functional>

#include <jsoncons/config/compiler_support.hpp>
#include <jsoncons/config/jsoncons_config.hpp>

#include <jsoncons_ext/jsonpointer/jsonpointer.hpp>
#include <jsoncons_ext/jsonschema/common/schema_validator.hpp>
#include <jsoncons_ext/jsonschema/jsonschema_error.hpp>

namespace jsoncons {
namespace jsonschema {

    class validation_message_to_json_events 
    {
        json_visitor* visitor_ptr_;
    public:
        validation_message_to_json_events(json_visitor& visitor)
            : visitor_ptr_(std::addressof(visitor))
        {
        }

        walk_result operator()(const validation_message& message)
        {
            write_error(message);
            return walk_result::advance;
        }

        void write_error(const validation_message& message)
        {
            visitor_ptr_->begin_object();

            visitor_ptr_->key("valid");
            visitor_ptr_->bool_value(false);

            visitor_ptr_->key("evaluationPath");
            visitor_ptr_->string_value(message.eval_path().string());

            visitor_ptr_->key("schemaLocation");
            visitor_ptr_->string_value(message.schema_location().string());

            visitor_ptr_->key("instanceLocation");
            visitor_ptr_->string_value(message.instance_location().string());

            visitor_ptr_->key("error");
            visitor_ptr_->string_value(message.message());

            if (!message.details().empty())
            {
                visitor_ptr_->key("details");
                visitor_ptr_->begin_array();
                for (const auto& detail : message.details())
                {
                    write_error(detail);
                }
                visitor_ptr_->end_array();
            }

            visitor_ptr_->end_object();
        }
    };

    class throwing_error_listener : public error_reporter
    {
        walk_result do_error(const validation_message& msg) override
        {
            JSONCONS_THROW(validation_error(msg.instance_location().string() + ": " + msg.message()));
        }
    };
  
    class fail_early_reporter : public error_reporter
    {
        walk_result do_error(const validation_message&) override
        {
            return walk_result::abort;
        }
    };

    using error_reporter_t = std::function<walk_result(const validation_message& msg)>;

    struct error_reporter_adaptor : public error_reporter
    {
        error_reporter_t reporter_;

        error_reporter_adaptor(const error_reporter_t& reporter)
            : reporter_(reporter)
        {
        }
    private:
        walk_result do_error(const validation_message& e) override
        {
            return reporter_(e);
        }
    };
       
    template <typename Json>
    class json_validator;
    
    template <typename Json>
    class json_schema
    {
        using keyword_validator_ptr_type = std::unique_ptr<keyword_validator<Json>>;
        using document_schema_validator_type = std::unique_ptr<document_schema_validator<Json>>;

        document_schema_validator_type root_;
        
        friend class json_validator<Json>;
    public:
        json_schema(document_schema_validator_type&& root)
            : root_(std::move(root))
        {
            if (root_ == nullptr)
                JSONCONS_THROW(schema_error("There is no root schema to validate an instance against"));
        }

        json_schema(const json_schema&) = delete;
        json_schema(json_schema&&) = default;
        json_schema& operator=(const json_schema&) = delete;
        json_schema& operator=(json_schema&&) = default;

        // Validate input JSON against a JSON Schema with a default throwing error reporter
        Json validate(const Json& instance) const
        {
            throwing_error_listener reporter;
            jsonpointer::json_pointer instance_location{};
            Json patch(json_array_arg);

            eval_context<Json> context;
            evaluation_results results;
            root_->validate(context, instance, instance_location, results, 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);

            eval_context<Json> context;
            evaluation_results results;
            root_->validate(context, instance, instance_location, results, reporter, patch);
            return reporter.error_count() == 0;
        }

        // Validate input JSON against a JSON Schema with a provided error reporter
        template <typename MsgReporter>
        typename std::enable_if<extension_traits::is_unary_function_object_exact<MsgReporter,walk_result,validation_message>::value,void>::type
        validate(const Json& instance, const MsgReporter& reporter) const
        {
            jsonpointer::json_pointer instance_location{};
            Json patch(json_array_arg);

            error_reporter_adaptor adaptor(reporter);
            eval_context<Json> context;
            evaluation_results results;
            root_->validate(context, instance, instance_location, results, adaptor, patch);
        }

        // Validate input JSON against a JSON Schema with a provided error reporter
        template <typename MsgReporter>
        typename std::enable_if<extension_traits::is_unary_function_object_exact<MsgReporter,walk_result,validation_message>::value,void>::type
        validate(const Json& instance, MsgReporter&& reporter, Json& patch) const
        {
            jsonpointer::json_pointer instance_location{};
            patch = Json(json_array_arg);

            error_reporter_adaptor adaptor(std::forward<MsgReporter>(reporter));
            eval_context<Json> context;
            evaluation_results results;
            root_->validate(context, instance, instance_location, results, adaptor, patch);
        }

        // Validate input JSON against a JSON Schema with a provided error reporter
        void validate(const Json& instance, Json& patch) const
        {
            jsonpointer::json_pointer instance_location{};
            patch = Json(json_array_arg);

            fail_early_reporter reporter;
            eval_context<Json> context;
            evaluation_results results;
            root_->validate(context, instance, instance_location, results, reporter, patch);
        }

        // Validate input JSON against a JSON Schema with a provided json_visitor
        void validate(const Json& instance, json_visitor& visitor) const
        {
            visitor.begin_array();
            jsonpointer::json_pointer instance_location{};
            Json patch{json_array_arg};

            validation_message_to_json_events adaptor{ visitor };
            eval_context<Json> context;
            evaluation_results results;
            error_reporter_adaptor reporter(adaptor);
            root_->validate(context, instance, instance_location, results, reporter, patch);
            visitor.end_array();
            visitor.flush();
        }
        
        template <typename WalkReporter>
        void walk(const Json& instance, const WalkReporter& reporter) const
        {
            jsonpointer::json_pointer instance_location{};

            root_->walk(eval_context<Json>{}, instance, instance_location, reporter);
        }
        
    private:
        // Validate input JSON against a JSON Schema with a provided error reporter
        void validate2(const Json& instance, error_reporter& reporter, Json& patch) const
        {
            jsonpointer::json_pointer instance_location{};
            patch = Json(json_array_arg);

            eval_context<Json> context;
            evaluation_results results;
            root_->validate(context, instance, instance_location, results, reporter, patch);
        }
    };


} // namespace jsonschema
} // namespace jsoncons

#endif // JSONCONS_EXT_JSONSCHEMA_SCHEMA_HPP