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>
94 verify(boost::json::value
const& value, std::string_view key)
const
96 if (value.is_object() and value.as_object().contains(key)) {
97 using boost::json::value_to;
98 auto const res = value_to<T>(value.as_object().at(key));
101 RippledError::rpcNOT_SUPPORTED,
102 fmt::format(
"Not supported field '{}'s value '{}'", std::string{key}, res)
125 verify(boost::json::value
const& value, std::string_view key)
127 if (value.is_object() and value.as_object().contains(key))
129 RippledError::rpcNOT_SUPPORTED,
"Not supported field '" + std::string{key} +
'\''
139template <
typename... T>
140NotSupported(T&&... t) -> NotSupported<T...>;
145template <
typename... Types>
158 verify(boost::json::value& value, std::string_view key)
const
160 if (not value.is_object() or not value.as_object().contains(key))
163 auto& res = value.as_object().at(key);
164 auto const convertible = (checkTypeAndClamp<Types>(res) || ...);
167 return Error{
Status{RippledError::rpcINVALID_PARAMS}};
176template <
typename Type>
201 verify(boost::json::value
const& value, std::string_view key)
const
203 using boost::json::value_to;
205 if (not value.is_object() or not value.as_object().contains(key))
208 auto const res = value_to<Type>(value.as_object().at(key));
212 if (res < min_ || res > max_)
213 return Error{
Status{RippledError::rpcINVALID_PARAMS}};
222template <
typename Type>
245 verify(boost::json::value
const& value, std::string_view key)
const
247 using boost::json::value_to;
249 if (not value.is_object() or not value.as_object().contains(key))
252 auto const res = value_to<Type>(value.as_object().at(key));
255 return Error{
Status{RippledError::rpcINVALID_PARAMS}};
264template <
typename Type>
287 verify(boost::json::value
const& value, std::string_view key)
const
289 using boost::json::value_to;
291 if (not value.is_object() or not value.as_object().contains(key))
294 auto const res = value_to<Type>(value.as_object().at(key));
297 return Error{
Status{RippledError::rpcINVALID_PARAMS}};
328 verify(boost::json::value
const& value, std::string_view key)
const;
334template <
typename Type>
357 verify(boost::json::value
const& value, std::string_view key)
const
359 using boost::json::value_to;
361 if (not value.is_object() or not value.as_object().contains(key))
364 auto const res = value_to<Type>(value.as_object().at(key));
365 if (res != original_)
366 return Error{
Status{RippledError::rpcINVALID_PARAMS}};
376EqualTo(
char const*) -> EqualTo<std::string>;
381template <
typename Type>
383 std::vector<Type> options_;
391 explicit OneOf(std::initializer_list<Type> options) : options_{options}
400 template <
typename InputIt>
401 explicit OneOf(InputIt begin, InputIt end) : options_{begin, end}
414 verify(boost::json::value
const& value, std::string_view key)
const
416 using boost::json::value_to;
418 if (not value.is_object() or not value.as_object().contains(key))
421 auto const res = value_to<Type>(value.as_object().at(key));
422 if (std::find(std::begin(options_), std::end(options_), res) == std::end(options_))
424 Status{RippledError::rpcINVALID_PARAMS, fmt::format(
"Invalid field '{}'.", key)}
435OneOf(std::initializer_list<char const*>) -> OneOf<std::string>;
441 std::function<
MaybeError(boost::json::value
const&, std::string_view)> validator_;
450 template <
typename Fn>
451 requires std::invocable<Fn, boost::json::value const&, std::string_view>
466 verify(boost::json::value
const& value, std::string_view key)
const;
476checkIsU32Numeric(std::string_view sv);
478template <
class HexType>
480 std::is_same_v<HexType, ripple::uint160> || std::is_same_v<HexType, ripple::uint192> ||
481 std::is_same_v<HexType, ripple::uint256>
484makeHexStringValidator(boost::json::value
const& value, std::string_view key)
486 if (!value.is_string())
487 return Error{
Status{RippledError::rpcINVALID_PARAMS, std::string(key) +
"NotString"}};
490 if (!parsedInt.parseHex(value.as_string().c_str()))
491 return Error{Status{RippledError::rpcINVALID_PARAMS, std::string(key) +
"Malformed"}};
629 verify(boost::json::value
const& value, std::string_view key)
631 if (not value.is_object() or not value.as_object().contains(key))
634 auto const& res = value.as_object().at(key);
637 for (
auto const& elem : res.as_array()) {
639 if (!elem.is_string() || !num.parseHex(elem.as_string())) {
641 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:201
Between(Type min, Type max)
Construct the validator storing min and max values.
Definition Validators.hpp:188
A meta-validator that allows to specify a custom validation function.
Definition Validators.hpp:440
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:82
CustomValidator(Fn &&fn)
Constructs a custom validator from any supported callable.
Definition Validators.hpp:452
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:357
EqualTo(Type original)
Construct the validator with stored original value.
Definition Validators.hpp:344
Max(Type max)
Construct the validator storing max value.
Definition Validators.hpp:274
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:287
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:245
Min(Type min)
Construct the validator storing min value.
Definition Validators.hpp:232
MaybeError verify(boost::json::value const &value, std::string_view key) const
Verify whether the field is supported or not.
Definition Validators.hpp:94
NotSupported(T val)
Constructs a new NotSupported validator.
Definition Validators.hpp:82
static MaybeError verify(boost::json::value const &value, std::string_view key)
Verify whether the field is supported or not.
Definition Validators.hpp:125
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:391
OneOf(InputIt begin, InputIt end)
Construct the validator with stored options of other container.
Definition Validators.hpp:401
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:414
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:84
A group of custom validation functions.
Definition Validators.hpp:499
static CustomValidator uint160HexStringValidator
Provides a commonly used validator for uint160(AccountID) hex string.
Definition Validators.hpp:552
static CustomValidator credentialTypeValidator
Provides a validator for validating credential_type.
Definition Validators.hpp:612
static CustomValidator accountValidator
Provides a commonly used validator for accounts.
Definition Validators.hpp:521
static CustomValidator ledgerTypeValidator
Provides a validator for ledger type.
Definition Validators.hpp:514
static CustomValidator currencyValidator
Provides a commonly used validator for currency, including standard currency code and token code.
Definition Validators.hpp:574
static CustomValidator accountTypeValidator
Provides a validator for account type.
Definition Validators.hpp:544
static CustomValidator accountMarkerValidator
Provides a commonly used validator for markers.
Definition Validators.hpp:536
static CustomValidator issuerValidator
Provides a commonly used validator for issuer type.
Definition Validators.hpp:581
static CustomValidator ledgerIndexValidator
Provides a commonly used validator for ledger index.
Definition Validators.hpp:506
static CustomValidator currencyIssueValidator
Validates an asset (ripple::Issue).
Definition Validators.hpp:598
static CustomValidator subscribeAccountsValidator
Provides a validator for validating accounts used in subscribe/unsubscribe.
Definition Validators.hpp:591
static CustomValidator authorizeCredentialValidator
Provides a validator for validating authorized_credentials json array.
Definition Validators.hpp:605
static CustomValidator uint192HexStringValidator
Provides a commonly used validator for uint192 hex string.
Definition Validators.hpp:560
static CustomValidator uint256HexStringValidator
Provides a commonly used validator for uint256 hex string.
Definition Validators.hpp:568
static CustomValidator subscribeStreamValidator
Provides a validator for validating streams used in subscribe/unsubscribe.
Definition Validators.hpp:586
static CustomValidator accountBase58Validator
Provides a commonly used validator for accounts.
Definition Validators.hpp:528
Validates that the elements of the array is of type Hex256 uint.
Definition Validators.hpp:618
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:629
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:146
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:158