From 1d055261b4144dbf86b2658437015b15d4dd9bff Mon Sep 17 00:00:00 2001 From: Richard Date: Sun, 4 Sep 2022 00:32:56 +0100 Subject: initial --- include/jsoncons_ext/jsonschema/json_validator.hpp | 120 +++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 include/jsoncons_ext/jsonschema/json_validator.hpp (limited to 'include/jsoncons_ext/jsonschema/json_validator.hpp') 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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; + + 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_validator + { + std::shared_ptr> root_; + + public: + json_validator(std::shared_ptr> 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 + typename std::enable_if::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 -- cgit v1.2.3