Boost C++ Libraries

PrevUpHomeNext

Struct template constrained

boost::taar::handler::constrained — Argument provider wrapper that validates the converted value against constraints.

Synopsis

// In header: <boost/taar/handler/constrained.hpp>

template<typename ArgProviderType> 
struct constrained {
  // types
  typedef ArgProviderType arg_provider_type;        
  typedef unspecified     arg_provider_request_type;
  typedef unspecified     arg_provider_result_type; 

  // public member functions
  constrained(arg_provider_type, constraints);
  std::string name() const;
  std::string description() const;
  decltype(auto) 
  operator()(arg_provider_request_type const &, matcher::context const &) const;
  constraints const & rules() const noexcept;
  template<typename T> void check(T const &) const;

  // private member functions
  void fail(std::string) const;

  // public data members
  arg_provider_type arg_provider_;
  constraints constraints_;
  std::optional< std::regex > regex_;
};

Description

Wraps any REST, JSON-RPC or MCP provider, including one already wrapped in with_default: rest(&list, constrained(with_default(query_arg("page"), 1), {.minimum = 1})) A violation is thrown as boost::taar::exception carrying error::constraint_violated with a message naming the argument and the rule: 400 on REST, -32602 (invalid params) on JSON-RPC and MCP, the message in data. An absent optional value is not checked.

constrained public member functions

  1. constrained(arg_provider_type arg_provider, constraints rules);
  2. std::string name() const;
  3. std::string description() const;
  4. decltype(auto) 
    operator()(arg_provider_request_type const & request, 
               matcher::context const & context) const;
  5. constraints const & rules() const noexcept;
  6. template<typename T> void check(T const & value) const;
    Validate value.

    Throws:

    boost::taar::exception with error::constraint_violated, message <name> <rule> (e.g. "page must be >= 1") and data {"argument": name}.

constrained private member functions

  1. void fail(std::string what) const;

PrevUpHomeNext