//------------------------------------------------------------------------------ /* This file is part of clio: https://github.com/XRPLF/clio Copyright (c) 2024, 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 #include #include #include #include #include #include #include namespace rpc::validation { namespace impl { template void clampAs(boost::json::value& value) { if (value.is_uint64()) { auto const valueUint = value.as_uint64(); if (valueUint > static_cast(std::numeric_limits::max())) value = std::numeric_limits::max(); } else if (value.is_int64()) { auto const valueInt = value.as_int64(); if (valueInt > static_cast(std::numeric_limits::max())) value = std::numeric_limits::max(); } } template void clampAs(boost::json::value& value) { if (value.is_uint64()) { auto const valueUint = value.as_uint64(); if (valueUint > static_cast(std::numeric_limits::max())) value = std::numeric_limits::max(); } else if (value.is_int64()) { auto const valueInt = value.as_int64(); if (valueInt > static_cast(std::numeric_limits::max())) { value = std::numeric_limits::max(); } else if (valueInt < static_cast(std::numeric_limits::min())) { value = std::numeric_limits::min(); } } } } // namespace impl /** * @brief Check that the type is the same as what was expected. * * @tparam Expected The expected type that value should be convertible to * @param value The json value to check the type of * @return true if convertible; false otherwise */ template [[nodiscard]] bool checkType(boost::json::value const& value) { auto hasError = false; if constexpr (std::is_same_v) { if (not value.is_bool()) hasError = true; } else if constexpr (std::is_same_v) { if (not value.is_string()) hasError = true; } else if constexpr (std::is_same_v or std::is_same_v) { if (not value.is_double()) hasError = true; } else if constexpr (std::is_same_v) { if (not value.is_array()) hasError = true; } else if constexpr (std::is_same_v) { if (not value.is_object()) hasError = true; } else if constexpr (std::is_convertible_v or std::is_convertible_v) { if (not value.is_int64() && not value.is_uint64()) hasError = true; // if the type specified is unsigned, it should not be negative if constexpr (std::is_unsigned_v) { if (value.is_int64() and value.as_int64() < 0) hasError = true; } } return not hasError; } /** * @brief Check that the type is the same as what was expected optionally clamping it into range. * * This is used to automatically clamp the value into the range available to the specified type. It is needed in * order to avoid Min, Max and other validators throw "not exact" error from Boost.Json library if the value does not * fit in the specified type. * * @tparam Expected The expected type that value should be convertible to * @param value The json value to check the type of * @return true if convertible; false otherwise */ template [[nodiscard]] bool checkTypeAndClamp(boost::json::value& value) { if (not checkType(value)) return false; // fails basic type check if constexpr (std::is_integral_v and not std::is_same_v) impl::clampAs(value); return true; } } // namespace rpc::validation