//------------------------------------------------------------------------------ /* This file is part of clio: https://github.com/XRPLF/clio Copyright (c) 2023, the clio developers. Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ //============================================================================== #pragma once #include "rpc/Errors.hpp" #include "rpc/common/Concepts.hpp" #include "rpc/common/Specs.hpp" #include "rpc/common/Types.hpp" #include #include #include #include #include #include #include #include #include namespace rpc::meta { /** * @brief A meta-processor that acts as a spec for a sub-object/section. */ class Section final { std::vector specs_; public: /** * @brief Construct new section validator from a list of specs. * * @param specs List of specs @ref FieldSpec */ explicit Section(std::initializer_list specs) : specs_{specs} { } /** * @brief Verify that the JSON value representing the section is valid according to the given specs. * * @param value The JSON value representing the outer object * @param key The key used to retrieve the section from the outer object * @return Possibly an error */ [[nodiscard]] MaybeError verify(boost::json::value& value, std::string_view key) const; }; /** * @brief A meta-processor that specifies a list of specs to run against the object at the given index in the array. */ class ValidateArrayAt final { std::size_t idx_; std::vector specs_; public: /** * @brief Constructs a processor that validates the specified element of a JSON array. * * @param idx The index inside the array to validate * @param specs The specifications to validate against */ ValidateArrayAt(std::size_t idx, std::initializer_list specs) : idx_{idx}, specs_{specs} { } /** * @brief Verify that the JSON array element at given index is valid according the stored specs. * * @param value The JSON value representing the outer object * @param key The key used to retrieve the array from the outer object * @return Possibly an error */ [[nodiscard]] MaybeError verify(boost::json::value& value, std::string_view key) const; }; /** * @brief A meta-processor that specifies a list of requirements to run against when the type matches the template * parameter. */ template class IfType final { public: /** * @brief Constructs a validator that validates the specs if the type matches. * @param requirements The requirements to validate against */ template explicit IfType(Requirements&&... requirements) : processor_( [... r = std::forward(requirements)](boost::json::value& j, std::string_view key) -> MaybeError { std::optional firstFailure = std::nullopt; // the check logic is the same as fieldspec ( [&j, &key, &firstFailure, req = &r]() { if (firstFailure) return; if (auto const res = req->verify(j, key); not res) firstFailure = res.error(); }(), ... ); if (firstFailure) return Error{firstFailure.value()}; return {}; } ) { } IfType(IfType const&) = default; IfType(IfType&&) = default; /** * @brief Verify that the element is valid according to the stored requirements when type matches. * * @param value The JSON value representing the outer object * @param key The key used to retrieve the element from the outer object * @return Possibly an error */ [[nodiscard]] MaybeError verify(boost::json::value& value, std::string_view key) const { if (not value.is_object() or not value.as_object().contains(key)) return {}; // ignore. field does not exist, let 'required' fail instead if (not rpc::validation::checkType(value.as_object().at(key))) return {}; // ignore if type does not match return processor_(value, key); } private: std::function processor_; }; /** * @brief A meta-processor that wraps a validator and produces a custom error in case the wrapped validator fails. */ template requires SomeRequirement or SomeModifier class WithCustomError final { RequirementOrModifierType reqOrModifier_; Status error_; public: /** * @brief Constructs a validator that calls the given validator `req` and returns a custom error `err` in case `req` * fails. * * @param reqOrModifier The requirement to validate against * @param err The custom error to return in case `req` fails */ WithCustomError(RequirementOrModifierType reqOrModifier, Status err) : reqOrModifier_{std::move(reqOrModifier)}, error_{std::move(err)} { } /** * @brief Runs the stored validator and produces a custom error if the wrapped validator fails. * * @param value The JSON value representing the outer object * @param key The key used to retrieve the element from the outer object * @return Possibly an error */ [[nodiscard]] MaybeError verify(boost::json::value const& value, std::string_view key) const requires SomeRequirement { if (auto const res = reqOrModifier_.verify(value, key); not res) return Error{error_}; return {}; } /** * @brief Runs the stored validator and produces a custom error if the wrapped validator fails. This is an overload * for the requirement which can modify the value. Such as IfType. * * @param value The JSON value representing the outer object, this value can be modified by the requirement inside * @param key The key used to retrieve the element from the outer object * @return Possibly an error */ [[nodiscard]] MaybeError verify(boost::json::value& value, std::string_view key) const requires SomeRequirement { if (auto const res = reqOrModifier_.verify(value, key); not res) return Error{error_}; return {}; } /** * @brief Runs the stored modifier and produces a custom error if the wrapped modifier fails. * * @param value The JSON value representing the outer object. This value can be modified by the modifier. * @param key The key used to retrieve the element from the outer object * @return Possibly an error */ MaybeError modify(boost::json::value& value, std::string_view key) const requires SomeModifier { if (auto const res = reqOrModifier_.modify(value, key); not res) return Error{error_}; return {}; } }; } // namespace rpc::meta