4#include "rpc/common/Types.hpp"
5#include "rpc/common/ValidationHelpers.hpp"
7#include <boost/json/array.hpp>
8#include <boost/json/object.hpp>
9#include <boost/json/value.hpp>
10#include <fmt/format.h>
11#include <xrpl/basics/base_uint.h>
12#include <xrpl/protocol/ErrorCodes.h>
17#include <initializer_list>
23namespace rpc::validation {
37 verify(boost::json::value
const& value, std::string_view key);
46template <
typename... T>
75 verify(boost::json::value
const& value, std::string_view key)
const
77 if (value.is_object() and value.as_object().contains(key)) {
78 using boost::json::value_to;
79 auto const res = value_to<T>(value.as_object().at(key));
82 RippledError::rpcNOT_SUPPORTED,
83 fmt::format(
"Not supported field '{}'s value '{}'", std::string{key}, res)
106 verify(boost::json::value
const& value, std::string_view key)
108 if (value.is_object() and value.as_object().contains(key)) {
110 RippledError::rpcNOT_SUPPORTED,
"Not supported field '" + std::string{key} +
'\''
121template <
typename... T>
122NotSupported(T&&... t) -> NotSupported<T...>;
127template <
typename... Types>
140 verify(boost::json::value& value, std::string_view key)
const
142 if (not value.is_object() or not value.as_object().contains(key))
145 auto& res = value.as_object().at(key);
146 auto const convertible = (checkTypeAndClamp<Types>(res) || ...);
149 return Error{
Status{RippledError::rpcINVALID_PARAMS}};
158template <
typename Type>
183 verify(boost::json::value
const& value, std::string_view key)
const
185 using boost::json::value_to;
187 if (not value.is_object() or not value.as_object().contains(key))
190 auto const res = value_to<Type>(value.as_object().at(key));
194 if (res < min_ || res > max_)
195 return Error{
Status{RippledError::rpcINVALID_PARAMS}};
204template <
typename Type>
227 verify(boost::json::value
const& value, std::string_view key)
const
229 using boost::json::value_to;
231 if (not value.is_object() or not value.as_object().contains(key))
234 auto const res = value_to<Type>(value.as_object().at(key));
237 return Error{
Status{RippledError::rpcINVALID_PARAMS}};
246template <
typename Type>
269 verify(boost::json::value
const& value, std::string_view key)
const
271 using boost::json::value_to;
273 if (not value.is_object() or not value.as_object().contains(key))
276 auto const res = value_to<Type>(value.as_object().at(key));
279 return Error{
Status{RippledError::rpcINVALID_PARAMS}};
310 verify(boost::json::value
const& value, std::string_view key)
const;
316template <
typename Type>
339 verify(boost::json::value
const& value, std::string_view key)
const
341 using boost::json::value_to;
343 if (not value.is_object() or not value.as_object().contains(key))
346 auto const res = value_to<Type>(value.as_object().at(key));
347 if (res != original_)
348 return Error{
Status{RippledError::rpcINVALID_PARAMS}};
358EqualTo(
char const*) -> EqualTo<std::string>;
363template <
typename Type>
365 std::vector<Type> options_;
373 explicit OneOf(std::initializer_list<Type> options) : options_{options}
382 template <
typename InputIt>
383 explicit OneOf(InputIt begin, InputIt end) : options_{begin, end}
396 verify(boost::json::value
const& value, std::string_view key)
const
398 using boost::json::value_to;
400 if (not value.is_object() or not value.as_object().contains(key))
403 auto const res = value_to<Type>(value.as_object().at(key));
404 if (std::find(std::begin(options_), std::end(options_), res) == std::end(options_)) {
406 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>
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"}};
612 verify(boost::json::value
const& value, std::string_view key)
614 if (not value.is_object() or not value.as_object().contains(key))
617 auto const& res = value.as_object().at(key);
620 for (
auto const& elem : res.as_array()) {
622 if (!elem.is_string() || !num.parseHex(elem.as_string())) {
624 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:183
Between(Type min, Type max)
Construct the validator storing min and max values.
Definition Validators.hpp:170
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:64
CustomValidator(Fn &&fn)
Constructs a custom validator from any supported callable.
Definition Validators.hpp:435
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:339
EqualTo(Type original)
Construct the validator with stored original value.
Definition Validators.hpp:326
Max(Type max)
Construct the validator storing max value.
Definition Validators.hpp:256
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:269
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:227
Min(Type min)
Construct the validator storing min value.
Definition Validators.hpp:214
MaybeError verify(boost::json::value const &value, std::string_view key) const
Verify whether the field is supported or not.
Definition Validators.hpp:75
NotSupported(T val)
Constructs a new NotSupported validator.
Definition Validators.hpp:63
static MaybeError verify(boost::json::value const &value, std::string_view key)
Verify whether the field is supported or not.
Definition Validators.hpp:106
A validator that forbids a field to be present.
Definition Validators.hpp:47
OneOf(std::initializer_list< Type > options)
Construct the validator with stored options of initializer list.
Definition Validators.hpp:373
OneOf(InputIt begin, InputIt end)
Construct the validator with stored options of other container.
Definition Validators.hpp:383
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:396
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:36
std::unexpected< Status > Error
The type that represents just the error part of MaybeError.
Definition Types.hpp:56
A status returned from any RPC handler.
Definition Errors.hpp:65
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:595
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:557
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:564
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:581
static CustomValidator subscribeAccountsValidator
Provides a validator for validating accounts used in subscribe/unsubscribe.
Definition Validators.hpp:574
static CustomValidator authorizeCredentialValidator
Provides a validator for validating authorized_credentials json array.
Definition Validators.hpp:588
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:569
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:601
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:612
A validator that simply requires a field to be present.
Definition Validators.hpp:28
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:33
Validates that the type of the value is one of the given types.
Definition Validators.hpp:128
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:140