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>
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>
151 verify(boost::json::value
const& value, std::string_view key)
const
153 if (not value.is_object() or not value.as_object().contains(key))
156 auto const& res = value.as_object().at(key);
157 auto const convertible = (checkType<Types>(res) || ...);
160 return Error{
Status{RippledError::rpcINVALID_PARAMS}};
169template <
typename Type>
193 verify(boost::json::value
const& value, std::string_view key)
const
195 using boost::json::value_to;
197 if (not value.is_object() or not value.as_object().contains(key))
200 auto const res = value_to<Type>(value.as_object().at(key));
204 if (res < min_ || res > max_)
205 return Error{
Status{RippledError::rpcINVALID_PARAMS}};
214template <
typename Type>
236 verify(boost::json::value
const& value, std::string_view key)
const
238 using boost::json::value_to;
240 if (not value.is_object() or not value.as_object().contains(key))
243 auto const res = value_to<Type>(value.as_object().at(key));
246 return Error{
Status{RippledError::rpcINVALID_PARAMS}};
255template <
typename Type>
277 verify(boost::json::value
const& value, std::string_view key)
const
279 using boost::json::value_to;
281 if (not value.is_object() or not value.as_object().contains(key))
284 auto const res = value_to<Type>(value.as_object().at(key));
287 return Error{
Status{RippledError::rpcINVALID_PARAMS}};
317 verify(boost::json::value
const& value, std::string_view key)
const;
323template <
typename Type>
345 verify(boost::json::value
const& value, std::string_view key)
const
347 using boost::json::value_to;
349 if (not value.is_object() or not value.as_object().contains(key))
352 auto const res = value_to<Type>(value.as_object().at(key));
353 if (res != original_)
354 return Error{
Status{RippledError::rpcINVALID_PARAMS}};
363EqualTo(
char const*) -> EqualTo<std::string>;
368template <
typename Type>
370 std::vector<Type> options_;
378 explicit OneOf(std::initializer_list<Type> options) : options_{options}
387 template <
typename InputIt>
388 explicit OneOf(InputIt begin, InputIt end) : options_{begin, end}
400 verify(boost::json::value
const& value, std::string_view key)
const
402 using boost::json::value_to;
404 if (not value.is_object() or not value.as_object().contains(key))
407 auto const res = value_to<Type>(value.as_object().at(key));
408 if (std::find(std::begin(options_), std::end(options_), res) == std::end(options_))
409 return Error{
Status{RippledError::rpcINVALID_PARAMS, fmt::format(
"Invalid field '{}'.", key)}};
418OneOf(std::initializer_list<char const*>) -> OneOf<std::string>;
424 std::function<
MaybeError(boost::json::value
const&, std::string_view)> validator_;
433 template <
typename Fn>
434 requires std::invocable<Fn, boost::json::value const&, std::string_view>
447 verify(boost::json::value
const& value, std::string_view key)
const;
457checkIsU32Numeric(std::string_view sv);
459template <
class HexType>
460 requires(std::is_same_v<HexType, ripple::uint160> || std::is_same_v<HexType, ripple::uint192> || std::is_same_v<HexType, ripple::uint256>)
462makeHexStringValidator(boost::json::value
const& value, std::string_view key)
464 if (!value.is_string())
465 return Error{
Status{RippledError::rpcINVALID_PARAMS, std::string(key) +
"NotString"}};
468 if (!parsedInt.parseHex(value.as_string().c_str()))
469 return Error{Status{RippledError::rpcINVALID_PARAMS, std::string(key) +
"Malformed"}};
589 verify(boost::json::value
const& value, std::string_view key)
591 if (not value.is_object() or not value.as_object().contains(key))
594 auto const& res = value.as_object().at(key);
597 for (
auto const& elem : res.as_array()) {
599 if (!elem.is_string() || !num.parseHex(elem.as_string())) {
600 return Error{
Status{RippledError::rpcINVALID_PARAMS,
"Item is not a valid uint256 type."}};
Validate that value is between specified min and max.
Definition Validators.hpp:170
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:193
Between(Type min, Type max)
Construct the validator storing min and max values.
Definition Validators.hpp:181
A meta-validator that allows to specify a custom validation function.
Definition Validators.hpp:423
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:77
CustomValidator(Fn &&fn)
Constructs a custom validator from any supported callable.
Definition Validators.hpp:435
Validates that the value is equal to the one passed in.
Definition Validators.hpp:324
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:345
EqualTo(Type original)
Construct the validator with stored original value.
Definition Validators.hpp:333
Validate that value is not greater than max.
Definition Validators.hpp:256
Max(Type max)
Construct the validator storing max value.
Definition Validators.hpp:265
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:277
Validate that value is equal or greater than the specified min.
Definition Validators.hpp:215
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:236
Min(Type min)
Construct the validator storing min value.
Definition Validators.hpp:224
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
Validates that the value is one of the values passed in.
Definition Validators.hpp:369
OneOf(std::initializer_list< Type > options)
Construct the validator with stored options of initializer list.
Definition Validators.hpp:378
OneOf(InputIt begin, InputIt end)
Construct the validator with stored options of other container.
Definition Validators.hpp:388
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:400
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:82
A group of custom validation functions.
Definition Validators.hpp:477
static CustomValidator uint160HexStringValidator
Provides a commonly used validator for uint160(AccountID) hex string.
Definition Validators.hpp:514
static CustomValidator credentialTypeValidator
Provides a validator for validating credential_type.
Definition Validators.hpp:573
static CustomValidator accountValidator
Provides a commonly used validator for accounts.
Definition Validators.hpp:491
static CustomValidator currencyValidator
Provides a commonly used validator for currency, including standard currency code and token code.
Definition Validators.hpp:535
static CustomValidator accountMarkerValidator
Provides a commonly used validator for markers.
Definition Validators.hpp:506
static CustomValidator issuerValidator
Provides a commonly used validator for issuer type.
Definition Validators.hpp:542
static CustomValidator ledgerIndexValidator
Provides a commonly used validator for ledger index.
Definition Validators.hpp:484
static CustomValidator currencyIssueValidator
Validates an asset (ripple::Issue).
Definition Validators.hpp:559
static CustomValidator subscribeAccountsValidator
Provides a validator for validating accounts used in subscribe/unsubscribe.
Definition Validators.hpp:552
static CustomValidator authorizeCredentialValidator
Provides a validator for validating authorized_credentials json array.
Definition Validators.hpp:566
static CustomValidator uint192HexStringValidator
Provides a commonly used validator for uint192 hex string.
Definition Validators.hpp:522
static CustomValidator uint256HexStringValidator
Provides a commonly used validator for uint256 hex string.
Definition Validators.hpp:530
static CustomValidator subscribeStreamValidator
Provides a validator for validating streams used in subscribe/unsubscribe.
Definition Validators.hpp:547
static CustomValidator accountBase58Validator
Provides a commonly used validator for accounts.
Definition Validators.hpp:498
Validates that the elements of the array is of type Hex256 uint.
Definition Validators.hpp:579
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:589
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:50
Validates that the type of the value is one of the given types.
Definition Validators.hpp:142
MaybeError verify(boost::json::value const &value, std::string_view key) const
Verify that the JSON value is (one) of specified type(s).
Definition Validators.hpp:151