mirror of
https://github.com/XRPLF/clio.git
synced 2025-12-06 17:27:58 +00:00
@@ -52,6 +52,16 @@ template <typename Expected>
|
||||
if (not value.is_double())
|
||||
hasError = true;
|
||||
}
|
||||
else if constexpr (std::is_same_v<Expected, boost::json::array>)
|
||||
{
|
||||
if (not value.is_array())
|
||||
hasError = true;
|
||||
}
|
||||
else if constexpr (std::is_same_v<Expected, boost::json::object>)
|
||||
{
|
||||
if (not value.is_object())
|
||||
hasError = true;
|
||||
}
|
||||
else if constexpr (
|
||||
std::is_convertible_v<Expected, uint64_t> or
|
||||
std::is_convertible_v<Expected, int64_t>)
|
||||
@@ -59,11 +69,6 @@ template <typename Expected>
|
||||
if (not value.is_int64() && not value.is_uint64())
|
||||
hasError = true;
|
||||
}
|
||||
else if constexpr (std::is_same_v<Expected, boost::json::array>)
|
||||
{
|
||||
if (not value.is_array())
|
||||
hasError = true;
|
||||
}
|
||||
|
||||
return not hasError;
|
||||
}
|
||||
@@ -308,6 +313,70 @@ public:
|
||||
verify(boost::json::value const& value, std::string_view key) const;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief A meta-validator that specifies a list of requirements to run against
|
||||
* when the type matches the template parameter
|
||||
*/
|
||||
template <typename Type>
|
||||
class IfType final
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Constructs a validator that validates the specs if the type
|
||||
* matches
|
||||
* @param requirements The requirements to validate against
|
||||
*/
|
||||
template <Requirement... Requirements>
|
||||
IfType(Requirements&&... requirements)
|
||||
{
|
||||
validator_ = [... r = std::forward<Requirements>(requirements)](
|
||||
boost::json::value const& j,
|
||||
std::string_view key) -> MaybeError {
|
||||
// clang-format off
|
||||
std::optional<RPC::Status> 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();
|
||||
}(), ...);
|
||||
// clang-format on
|
||||
|
||||
if (firstFailure)
|
||||
return Error{firstFailure.value()};
|
||||
|
||||
return {};
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Verify that the element is valid
|
||||
* according 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
|
||||
*/
|
||||
[[nodiscard]] MaybeError
|
||||
verify(boost::json::value const& value, std::string_view key) const
|
||||
{
|
||||
if (not value.is_object() or not value.as_object().contains(key.data()))
|
||||
return {}; // ignore. field does not exist, let 'required' fail
|
||||
// instead
|
||||
|
||||
if (not checkType<Type>(value.as_object().at(key.data())))
|
||||
return {}; // ignore if type does not match
|
||||
|
||||
return validator_(value, key);
|
||||
}
|
||||
|
||||
private:
|
||||
std::function<MaybeError(boost::json::value const&, std::string_view)>
|
||||
validator_;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief A meta-validator that allows to specify a custom validation function
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user