Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
ValidationHelpers.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2024, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include <boost/json/array.hpp>
23#include <boost/json/object.hpp>
24#include <boost/json/value.hpp>
25
26#include <concepts>
27#include <cstdint>
28#include <limits>
29#include <string>
30#include <type_traits>
31
32namespace rpc::validation {
33namespace impl {
34
35template <std::unsigned_integral Expected>
36void
37clampAs(boost::json::value& value)
38{
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();
47 }
48}
49
50template <std::signed_integral Expected>
51void
52clampAs(boost::json::value& value)
53{
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();
64 }
65 }
66}
67
68} // namespace impl
69
77template <typename Expected>
78[[nodiscard]] bool
79checkType(boost::json::value const& value)
80{
81 auto hasError = false;
82 if constexpr (std::is_same_v<Expected, bool>) {
83 if (not value.is_bool())
84 hasError = true;
85 } else if constexpr (std::is_same_v<Expected, std::string>) {
86 if (not value.is_string())
87 hasError = true;
88 } else if constexpr (std::is_same_v<Expected, double> or std::is_same_v<Expected, float>) {
89 if (not value.is_double())
90 hasError = true;
91 } else if constexpr (std::is_same_v<Expected, boost::json::array>) {
92 if (not value.is_array())
93 hasError = true;
94 } else if constexpr (std::is_same_v<Expected, boost::json::object>) {
95 if (not value.is_object())
96 hasError = true;
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())
99 hasError = true;
100 // if the type specified is unsigned, it should not be negative
101 if constexpr (std::is_unsigned_v<Expected>) {
102 if (value.is_int64() and value.as_int64() < 0)
103 hasError = true;
104 }
105 }
106
107 return not hasError;
108}
109
121template <typename Expected>
122[[nodiscard]] bool
123checkTypeAndClamp(boost::json::value& value)
124{
125 if (not checkType<Expected>(value))
126 return false; // fails basic type check
127
128 if constexpr (std::is_integral_v<Expected> and not std::is_same_v<Expected, bool>)
129 impl::clampAs<Expected>(value);
130
131 return true;
132}
133
134} // namespace rpc::validation