aboutsummaryrefslogtreecommitdiff
path: root/include/jsoncons_ext/jsonschema/json_validator.hpp
diff options
context:
space:
mode:
authorRichard <q@1bpm.net>2022-09-04 00:32:56 +0100
committerRichard <q@1bpm.net>2022-09-04 00:32:56 +0100
commit1d055261b4144dbf86b2658437015b15d4dd9bff (patch)
tree6049b19d1bf953a650383de1a5e438b8b82679f6 /include/jsoncons_ext/jsonschema/json_validator.hpp
downloadcsound-json-1d055261b4144dbf86b2658437015b15d4dd9bff.tar.gz
csound-json-1d055261b4144dbf86b2658437015b15d4dd9bff.tar.bz2
csound-json-1d055261b4144dbf86b2658437015b15d4dd9bff.zip
initial
Diffstat (limited to 'include/jsoncons_ext/jsonschema/json_validator.hpp')
-rw-r--r--include/jsoncons_ext/jsonschema/json_validator.hpp120
1 files changed, 120 insertions, 0 deletions
diff --git a/include/jsoncons_ext/jsonschema/json_validator.hpp b/include/jsoncons_ext/jsonschema/json_validator.hpp
new file mode 100644
index 0000000..87bec58
--- /dev/null
+++ b/include/jsoncons_ext/jsonschema/json_validator.hpp
@@ -0,0 +1,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