Implement the Clamp modifier (#740)

Fixes #750
This commit is contained in:
Alex Kremer
2023-07-10 16:09:20 +01:00
committed by GitHub
parent 271323b0f4
commit ba8e7188ca
23 changed files with 523 additions and 359 deletions

View File

@@ -28,28 +28,6 @@
namespace RPC::validation {
[[nodiscard]] MaybeError
Section::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
auto const& res = value.at(key.data());
// if it is not a json object, let other validators fail
if (!res.is_object())
return {};
for (auto const& spec : specs)
{
if (auto const ret = spec.validate(res); not ret)
return Error{ret.error()};
}
return {};
}
[[nodiscard]] MaybeError
Required::verify(boost::json::value const& value, std::string_view key) const
{
@@ -59,34 +37,11 @@ Required::verify(boost::json::value const& value, std::string_view key) const
return {};
}
[[nodiscard]] MaybeError
ValidateArrayAt::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 value.as_object().at(key.data()).is_array())
return Error{Status{RippledError::rpcINVALID_PARAMS}};
auto const& arr = value.as_object().at(key.data()).as_array();
if (idx_ >= arr.size())
return Error{Status{RippledError::rpcINVALID_PARAMS}};
auto const& res = arr.at(idx_);
for (auto const& spec : specs_)
if (auto const ret = spec.validate(res); not ret)
return Error{ret.error()};
return {};
}
[[nodiscard]] MaybeError
CustomValidator::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
return {}; // ignore. field does not exist, let 'required' fail instead
return validator_(value.as_object().at(key.data()), key);
}