22#include <boost/json/array.hpp>
23#include <boost/json/object.hpp>
24#include <boost/json/value.hpp>
32namespace rpc::validation {
35template <std::
unsigned_
integral Expected>
37clampAs(boost::json::value& value)
39 if (value.is_uint64()) {
40 auto const valueUint = value.as_uint64();
41 if (valueUint >
static_cast<uint64_t
>(std::numeric_limits<Expected>::max()))
42 value = std::numeric_limits<Expected>::max();
43 }
else if (value.is_int64()) {
44 auto const valueInt = value.as_int64();
45 if (valueInt >
static_cast<int64_t
>(std::numeric_limits<Expected>::max()))
46 value = std::numeric_limits<Expected>::max();
50template <std::
signed_
integral Expected>
52clampAs(boost::json::value& value)
54 if (value.is_uint64()) {
55 auto const valueUint = value.as_uint64();
56 if (valueUint >
static_cast<uint64_t
>(std::numeric_limits<Expected>::max()))
57 value = std::numeric_limits<Expected>::max();
58 }
else if (value.is_int64()) {
59 auto const valueInt = value.as_int64();
60 if (valueInt >
static_cast<int64_t
>(std::numeric_limits<Expected>::max())) {
61 value = std::numeric_limits<Expected>::max();
62 }
else if (valueInt <
static_cast<int64_t
>(std::numeric_limits<Expected>::min())) {
63 value = std::numeric_limits<Expected>::min();
77template <
typename Expected>
79checkType(boost::json::value
const& value)
81 auto hasError =
false;
82 if constexpr (std::is_same_v<Expected, bool>) {
83 if (not value.is_bool())
85 }
else if constexpr (std::is_same_v<Expected, std::string>) {
86 if (not value.is_string())
88 }
else if constexpr (std::is_same_v<Expected, double> or std::is_same_v<Expected, float>) {
89 if (not value.is_double())
91 }
else if constexpr (std::is_same_v<Expected, boost::json::array>) {
92 if (not value.is_array())
94 }
else if constexpr (std::is_same_v<Expected, boost::json::object>) {
95 if (not value.is_object())
97 }
else if constexpr (std::is_convertible_v<Expected, uint64_t> or std::is_convertible_v<Expected, int64_t>) {
98 if (not value.is_int64() && not value.is_uint64())
101 if constexpr (std::is_unsigned_v<Expected>) {
102 if (value.is_int64() and value.as_int64() < 0)
121template <
typename Expected>
123checkTypeAndClamp(boost::json::value& value)
125 if (not checkType<Expected>(value))
128 if constexpr (std::is_integral_v<Expected> and not std::is_same_v<Expected, bool>)
129 impl::clampAs<Expected>(value);