3#include <boost/json/array.hpp>
4#include <boost/json/object.hpp>
5#include <boost/json/value.hpp>
13namespace rpc::validation {
16template <std::
unsigned_
integral Expected>
18clampAs(boost::json::value& value)
20 if (value.is_uint64()) {
21 auto const valueUint = value.as_uint64();
22 if (valueUint >
static_cast<uint64_t
>(std::numeric_limits<Expected>::max()))
23 value = std::numeric_limits<Expected>::max();
24 }
else if (value.is_int64()) {
25 auto const valueInt = value.as_int64();
26 if (valueInt >
static_cast<int64_t
>(std::numeric_limits<Expected>::max()))
27 value = std::numeric_limits<Expected>::max();
31template <std::
signed_
integral Expected>
33clampAs(boost::json::value& value)
35 if (value.is_uint64()) {
36 auto const valueUint = value.as_uint64();
37 if (valueUint >
static_cast<uint64_t
>(std::numeric_limits<Expected>::max()))
38 value = std::numeric_limits<Expected>::max();
39 }
else if (value.is_int64()) {
40 auto const valueInt = value.as_int64();
41 if (valueInt >
static_cast<int64_t
>(std::numeric_limits<Expected>::max())) {
42 value = std::numeric_limits<Expected>::max();
43 }
else if (valueInt <
static_cast<int64_t
>(std::numeric_limits<Expected>::min())) {
44 value = std::numeric_limits<Expected>::min();
58template <
typename Expected>
60checkType(boost::json::value
const& value)
62 auto hasError =
false;
63 if constexpr (std::is_same_v<Expected, bool>) {
64 if (not value.is_bool())
66 }
else if constexpr (std::is_same_v<Expected, std::string>) {
67 if (not value.is_string())
69 }
else if constexpr (std::is_same_v<Expected, double> or std::is_same_v<Expected, float>) {
70 if (not value.is_double())
72 }
else if constexpr (std::is_same_v<Expected, boost::json::array>) {
73 if (not value.is_array())
75 }
else if constexpr (std::is_same_v<Expected, boost::json::object>) {
76 if (not value.is_object())
79 std::is_convertible_v<Expected, uint64_t> or std::is_convertible_v<Expected, int64_t>
81 if (not value.is_int64() && not value.is_uint64())
84 if constexpr (std::is_unsigned_v<Expected>) {
85 if (value.is_int64() and value.as_int64() < 0)
104template <
typename Expected>
106checkTypeAndClamp(boost::json::value& value)
108 if (not checkType<Expected>(value))
111 if constexpr (std::is_integral_v<Expected> and not std::is_same_v<Expected, bool>)
112 impl::clampAs<Expected>(value);