23#include "rpc/common/Types.hpp"
24#include "rpc/common/ValidationHelpers.hpp"
26#include <boost/json/array.hpp>
27#include <boost/json/object.hpp>
28#include <boost/json/value.hpp>
29#include <fmt/format.h>
30#include <xrpl/basics/base_uint.h>
31#include <xrpl/protocol/ErrorCodes.h>
36#include <initializer_list>
42namespace rpc::validation {
56 verify(boost::json::value
const& value, std::string_view key);
65template <
typename... T>
93 verify(boost::json::value
const& value, std::string_view key)
const
95 if (value.is_object() and value.as_object().contains(key)) {
96 using boost::json::value_to;
97 auto const res = value_to<T>(value.as_object().at(key));
100 RippledError::rpcNOT_SUPPORTED,
101 fmt::format(
"Not supported field '{}'s value '{}'", std::string{key}, res)
123 verify(boost::json::value
const& value, std::string_view key)
125 if (value.is_object() and value.as_object().contains(key))
126 return Error{
Status{RippledError::rpcNOT_SUPPORTED,
"Not supported field '" + std::string{key} +
'\''}};
135template <
typename... T>
136NotSupported(T&&... t) -> NotSupported<T...>;
141template <
typename... Types>
153 verify(boost::json::value& value, std::string_view key)
const
155 if (not value.is_object() or not value.as_object().contains(key))
158 auto& res = value.as_object().at(key);
159 auto const convertible = (checkTypeAndClamp<Types>(res) || ...);
162 return Error{
Status{RippledError::rpcINVALID_PARAMS}};
171template <
typename Type>
195 verify(boost::json::value
const& value, std::string_view key)
const
197 using boost::json::value_to;
199 if (not value.is_object() or not value.as_object().contains(key))
202 auto const res = value_to<Type>(value.as_object().at(key));
206 if (res < min_ || res > max_)
207 return Error{
Status{RippledError::rpcINVALID_PARAMS}};
216template <
typename Type>
238 verify(boost::json::value
const& value, std::string_view key)
const
240 using boost::json::value_to;
242 if (not value.is_object() or not value.as_object().contains(key))
245 auto const res = value_to<Type>(value.as_object().at(key));
248 return Error{
Status{RippledError::rpcINVALID_PARAMS}};
257template <
typename Type>
279 verify(boost::json::value
const& value, std::string_view key)
const
281 using boost::json::value_to;
283 if (not value.is_object() or not value.as_object().contains(key))
286 auto const res = value_to<Type>(value.as_object().at(key));
289 return Error{
Status{RippledError::rpcINVALID_PARAMS}};
319 verify(boost::json::value
const& value, std::string_view key)
const;
325template <
typename Type>
347 verify(boost::json::value
const& value, std::string_view key)
const
349 using boost::json::value_to;
351 if (not value.is_object() or not value.as_object().contains(key))
354 auto const res = value_to<Type>(value.as_object().at(key));
355 if (res != original_)
356 return Error{
Status{RippledError::rpcINVALID_PARAMS}};
365EqualTo(
char const*) -> EqualTo<std::string>;
370template <
typename Type>
372 std::vector<Type> options_;
380 explicit OneOf(std::initializer_list<Type> options) : options_{options}
389 template <
typename InputIt>
390 explicit OneOf(InputIt begin, InputIt end) : options_{begin, end}
402 verify(boost::json::value
const& value, std::string_view key)
const
404 using boost::json::value_to;
406 if (not value.is_object() or not value.as_object().contains(key))
409 auto const res = value_to<Type>(value.as_object().at(key));
410 if (std::find(std::begin(options_), std::end(options_), res) == std::end(options_))
411 return Error{
Status{RippledError::rpcINVALID_PARAMS, fmt::format(
"Invalid field '{}'.", key)}};
420OneOf(std::initializer_list<char const*>) -> OneOf<std::string>;
426 std::function<
MaybeError(boost::json::value
const&, std::string_view)> validator_;
435 template <
typename Fn>
436 requires std::invocable<Fn, boost::json::value const&, std::string_view>
449 verify(boost::json::value
const& value, std::string_view key)
const;
459checkIsU32Numeric(std::string_view sv);
461template <
class HexType>
463 std::is_same_v<HexType, ripple::uint160> || std::is_same_v<HexType, ripple::uint192> ||
464 std::is_same_v<HexType, ripple::uint256>
467makeHexStringValidator(boost::json::value
const& value, std::string_view key)
469 if (!value.is_string())
470 return Error{
Status{RippledError::rpcINVALID_PARAMS, std::string(key) +
"NotString"}};
473 if (!parsedInt.parseHex(value.as_string().c_str()))
474 return Error{Status{RippledError::rpcINVALID_PARAMS, std::string(key) +
"Malformed"}};
610 verify(boost::json::value
const& value, std::string_view key)
612 if (not value.is_object() or not value.as_object().contains(key))
615 auto const& res = value.as_object().at(key);
618 for (
auto const& elem : res.as_array()) {
620 if (!elem.is_string() || !num.parseHex(elem.as_string())) {
621 return Error{
Status{RippledError::rpcINVALID_PARAMS,
"Item is not a valid uint256 type."}};
MaybeError verify(boost::json::value const &value, std::string_view key) const
Verify that the JSON value is within a certain range.
Definition Validators.hpp:195
Between(Type min, Type max)
Construct the validator storing min and max values.
Definition Validators.hpp:183
A meta-validator that allows to specify a custom validation function.
Definition Validators.hpp:425
MaybeError verify(boost::json::value const &value, std::string_view key) const
Verify that the JSON value is valid according to the custom validation function stored.
Definition Validators.cpp:79
CustomValidator(Fn &&fn)
Constructs a custom validator from any supported callable.
Definition Validators.hpp:437
MaybeError verify(boost::json::value const &value, std::string_view key) const
Verify that the JSON value is equal to the stored original.
Definition Validators.hpp:347
EqualTo(Type original)
Construct the validator with stored original value.
Definition Validators.hpp:335
Max(Type max)
Construct the validator storing max value.
Definition Validators.hpp:267
MaybeError verify(boost::json::value const &value, std::string_view key) const
Verify that the JSON value is not greater than max.
Definition Validators.hpp:279
MaybeError verify(boost::json::value const &value, std::string_view key) const
Verify that the JSON value is not smaller than min.
Definition Validators.hpp:238
Min(Type min)
Construct the validator storing min value.
Definition Validators.hpp:226
MaybeError verify(boost::json::value const &value, std::string_view key) const
Verify whether the field is supported or not.
Definition Validators.hpp:93
NotSupported(T val)
Constructs a new NotSupported validator.
Definition Validators.hpp:81
static MaybeError verify(boost::json::value const &value, std::string_view key)
Verify whether the field is supported or not.
Definition Validators.hpp:123
A validator that forbids a field to be present.
Definition Validators.hpp:66
OneOf(std::initializer_list< Type > options)
Construct the validator with stored options of initializer list.
Definition Validators.hpp:380
OneOf(InputIt begin, InputIt end)
Construct the validator with stored options of other container.
Definition Validators.hpp:390
MaybeError verify(boost::json::value const &value, std::string_view key) const
Verify that the JSON value is one of the stored options.
Definition Validators.hpp:402
std::expected< void, Status > MaybeError
Return type used for Validators that can return error but don't have specific value to return.
Definition Types.hpp:55
std::unexpected< Status > Error
The type that represents just the error part of MaybeError.
Definition Types.hpp:75
A status returned from any RPC handler.
Definition Errors.hpp:83
A group of custom validation functions.
Definition Validators.hpp:482
static CustomValidator uint160HexStringValidator
Provides a commonly used validator for uint160(AccountID) hex string.
Definition Validators.hpp:535
static CustomValidator credentialTypeValidator
Provides a validator for validating credential_type.
Definition Validators.hpp:594
static CustomValidator accountValidator
Provides a commonly used validator for accounts.
Definition Validators.hpp:504
static CustomValidator ledgerTypeValidator
Provides a validator for ledger type.
Definition Validators.hpp:497
static CustomValidator currencyValidator
Provides a commonly used validator for currency, including standard currency code and token code.
Definition Validators.hpp:556
static CustomValidator accountTypeValidator
Provides a validator for account type.
Definition Validators.hpp:527
static CustomValidator accountMarkerValidator
Provides a commonly used validator for markers.
Definition Validators.hpp:519
static CustomValidator issuerValidator
Provides a commonly used validator for issuer type.
Definition Validators.hpp:563
static CustomValidator ledgerIndexValidator
Provides a commonly used validator for ledger index.
Definition Validators.hpp:489
static CustomValidator currencyIssueValidator
Validates an asset (ripple::Issue).
Definition Validators.hpp:580
static CustomValidator subscribeAccountsValidator
Provides a validator for validating accounts used in subscribe/unsubscribe.
Definition Validators.hpp:573
static CustomValidator authorizeCredentialValidator
Provides a validator for validating authorized_credentials json array.
Definition Validators.hpp:587
static CustomValidator uint192HexStringValidator
Provides a commonly used validator for uint192 hex string.
Definition Validators.hpp:543
static CustomValidator uint256HexStringValidator
Provides a commonly used validator for uint256 hex string.
Definition Validators.hpp:551
static CustomValidator subscribeStreamValidator
Provides a validator for validating streams used in subscribe/unsubscribe.
Definition Validators.hpp:568
static CustomValidator accountBase58Validator
Provides a commonly used validator for accounts.
Definition Validators.hpp:511
Validates that the elements of the array is of type Hex256 uint.
Definition Validators.hpp:600
static MaybeError verify(boost::json::value const &value, std::string_view key)
Validates given the prerequisite that the type of the json value is an array, verifies all values wit...
Definition Validators.hpp:610
A validator that simply requires a field to be present.
Definition Validators.hpp:47
static MaybeError verify(boost::json::value const &value, std::string_view key)
Verify that the JSON value is present and not null.
Definition Validators.cpp:52
Validates that the type of the value is one of the given types.
Definition Validators.hpp:142
MaybeError verify(boost::json::value &value, std::string_view key) const
Verify that the JSON value is (one) of specified type(s).
Definition Validators.hpp:153