AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
anch::val Namespace Reference

AnCH validation library namespace. More...

Classes

struct  Allowed
 Allowed values constraint ( enum ). More...
struct  CheckContext
 Context in which constraints are checked. More...
struct  CheckContext< anch::csv::detail::ReadContext >
 anch-val constraints checked when CSV fields are read ( Options::readCheckConstraints )
struct  CheckContext< anch::csv::detail::WriteContext >
 anch-val constraints checked when CSV fields are written ( Options::writeCheckConstraints )
struct  CheckContext< anch::json::ReaderContext >
 anch-val constraints checked on deserialization: deserialize_check_constraints option, violations raise anch::json::MappingError ( CONSTRAINT_VIOLATION , with the path of the value) More...
struct  CheckContext< anch::json::WriterContext >
 anch-val constraints checked on serialization: serialize_check_constraints option, violations raise anch::json::MappingError ( CONSTRAINT_VIOLATION ) More...
struct  Custom
 Custom constraint. More...
struct  Format
 String format constraint ( format ). More...
struct  FractionDigits
 Maximum number of fraction digits constraint (XSD fractionDigits facet). More...
struct  IsScalar
 Scalar type: value type checked as a whole, without fields (it is not described by anch::Mapping ). More...
struct  IsScalar< anch::date::Date >
struct  IsScalar< anch::date::DateTime >
struct  IsScalar< anch::date::Time >
struct  IsScalar< anch::UUID >
struct  IsScalar< std::chrono::duration< R, P > >
struct  IsScalar< std::chrono::time_point< C, D > >
struct  Items
 Items constraints. More...
struct  Length
 Exact length constraint (XSD length facet). More...
struct  Maximum
 Maximum value constraint ( maximum and exclusiveMaximum ). More...
struct  MaxItems
 Maximum number of items constraint ( maxItems ). More...
struct  MaxLength
 Maximum string length constraint ( maxLength ). More...
struct  MaxProperties
 Maximum number of map entries constraint ( maxProperties ). More...
struct  Minimum
 Minimum value constraint ( minimum and exclusiveMinimum ). More...
struct  MinItems
 Minimum number of items constraint ( minItems ). More...
struct  MinLength
 Minimum string length constraint ( minLength ). More...
struct  MinProperties
 Minimum number of map entries constraint ( minProperties ). More...
struct  MultipleOf
 Multiple of constraint ( multipleOf ). More...
class  Path
 Path of a value from the root value (validation violations): segments are field names or map keys and collection items indexes. Formats format it their way (JSON Pointer with pointer , XPath for XML, ...). More...
struct  Pattern
 String pattern constraint ( pattern ). More...
struct  Required
 Required field constraint. More...
struct  TotalDigits
 Maximum number of digits constraint (XSD totalDigits facet). More...
struct  UniqueItems
 Unique items constraint ( uniqueItems ). More...
class  ValidationContext
 Validation context. More...
class  ValidationMapper
 Mapper which validates the fields of a described type ( anch::Mapping ). More...
struct  Violation
 Constraint violation found by a validation (see anch::val::ValidationContext ). More...

Concepts

concept  Collection
concept  FieldConstraint
concept  MapLike
concept  Number
concept  Scalar
 Scalar type (see anch::val::IsScalar ).
concept  StringLike
concept  Validatable
 Type which can be validated: scalar ( anch::val::IsScalar ), described object ( anch::Mapping ), or std::optional , pointer, collection or map of them.
concept  ValueConstraint

Typedefs

using PatternMatcher = std::function<bool(std::string_view)>
using PatternCompiler = std::function<anch::val::PatternMatcher(std::string_view)>
using FormatValidator = std::function<bool(std::string_view)>

Enumerations

enum class  PatternSyntax { ECMASCRIPT , XSD }

Functions

std::ostream & operator<< (std::ostream &out, const anch::val::Path &path)
void setPatternCompiler (anch::val::PatternCompiler compiler)
void setPatternCompiler (anch::val::PatternSyntax syntax, anch::val::PatternCompiler compiler)
void registerFormat (const std::string &name, anch::val::FormatValidator validator)
template<typename... C>
constexpr bool isRequired ()
template<typename... C>
constexpr bool hasValueConstraints ()
template<typename P>
constexpr bool isNullable ()
constexpr anch::val::MinLength minLength (std::size_t length)
constexpr anch::val::MaxLength maxLength (std::size_t length)
template<anch::val::Number N>
constexpr anch::val::Minimum< N > minimum (N value, bool exclusive=false)
template<anch::val::Number N>
constexpr anch::val::Minimum< N > exclusiveMinimum (N value)
template<anch::val::Number N>
constexpr anch::val::Maximum< N > maximum (N value, bool exclusive=false)
template<anch::val::Number N>
constexpr anch::val::Maximum< N > exclusiveMaximum (N value)
template<anch::val::Number N>
anch::val::MultipleOf< N > multipleOf (N value)
constexpr anch::val::MinItems minItems (std::size_t count)
constexpr anch::val::MaxItems maxItems (std::size_t count)
constexpr anch::val::MinProperties minProperties (std::size_t count)
constexpr anch::val::MaxProperties maxProperties (std::size_t count)
anch::val::Pattern pattern (std::string_view source)
anch::val::Pattern xsdPattern (std::string_view source)
constexpr anch::val::Length length (std::size_t length)
constexpr anch::val::TotalDigits totalDigits (std::size_t digits)
constexpr anch::val::FractionDigits fractionDigits (std::size_t digits)
anch::val::Format format (std::string name)
template<typename A>
auto allowed (std::initializer_list< A > values)
template<typename... C>
anch::val::Items< std::decay_t< C >... > items (C &&... constraints)
template<typename V>
anch::val::Custom< V > custom (std::function< bool(const V &)> predicate, std::string message)
template<typename T>
std::vector< anch::val::Violation > validate (const T &value)
template<typename T>
bool isValid (const T &value)

Variables

constexpr anch::val::Required required {}
constexpr anch::val::UniqueItems uniqueItems {}

Detailed Description

AnCH validation library namespace.

Fields constraints shared by formats. See AnCH validation library documentation.

Typedef Documentation

◆ FormatValidator

using anch::val::FormatValidator = std::function<bool(std::string_view)>

Format validator: returns true when the value is valid

Since
0.2

◆ PatternCompiler

using anch::val::PatternCompiler = std::function<anch::val::PatternMatcher(std::string_view)>

Pattern compiler: builds the matcher of a pattern (ECMA-262 regular expression for JSON Schema)

Since
0.2

◆ PatternMatcher

using anch::val::PatternMatcher = std::function<bool(std::string_view)>

Pattern matcher: returns true when the value matches the pattern

Since
0.2

Enumeration Type Documentation

◆ PatternSyntax

enum class anch::val::PatternSyntax
strong

Pattern syntax

Since
0.3
Enumerator
ECMASCRIPT 

ECMA-262 regular expression (JSON Schema): the pattern is searched in the value

XSD 

XML Schema regular expression (XSD pattern facet): the pattern matches the whole value

Function Documentation

◆ allowed()

template<typename A>
auto anch::val::allowed ( std::initializer_list< A > values)

Allowed values constraint ( enum ). Character strings literals are stored as std::string .

Template Parameters
Athe values type
Parameters
valuesthe allowed values
Returns
the constraint
Since
0.2
Here is the call graph for this function:

◆ custom()

template<typename V>
anch::val::Custom< V > anch::val::custom ( std::function< bool(const V &)> predicate,
std::string message )

Custom constraint

Template Parameters
Vthe value type
Parameters
predicatethe predicate (true when value is valid)
messagethe error message
Returns
the constraint
Since
0.2
Here is the call graph for this function:

◆ exclusiveMaximum()

template<anch::val::Number N>
anch::val::Maximum< N > anch::val::exclusiveMaximum ( N value)
constexpr

exclusiveMaximum constraint: value has to be lower than bound

Template Parameters
Nthe bound type
Parameters
valuethe bound
Returns
the constraint
Since
0.2
Here is the call graph for this function:

◆ exclusiveMinimum()

template<anch::val::Number N>
anch::val::Minimum< N > anch::val::exclusiveMinimum ( N value)
constexpr

exclusiveMinimum constraint: value has to be greater than bound

Template Parameters
Nthe bound type
Parameters
valuethe bound
Returns
the constraint
Since
0.2
Here is the call graph for this function:

◆ format()

anch::val::Format anch::val::format ( std::string name)

format constraint

Parameters
namethe format name
Returns
the constraint
Since
0.2
Here is the call graph for this function:

◆ fractionDigits()

anch::val::FractionDigits anch::val::fractionDigits ( std::size_t digits)
constexpr

XSD fractionDigits facet

Parameters
digitsthe maximum number of fraction digits
Returns
the constraint
Since
0.3
Here is the call graph for this function:

◆ hasValueConstraints()

template<typename... C>
bool anch::val::hasValueConstraints ( )
constexpr

Check if constraints contain value constraints (other than anch::val::Required )

Template Parameters
Cthe constraints types
Returns
true when one of the constraints is not anch::val::Required
Since
0.2
Here is the call graph for this function:

◆ isNullable()

template<typename P>
bool anch::val::isNullable ( )
constexpr

Check if type can hold null value: std::optional , std::shared_ptr , std::unique_ptr or pointer

Template Parameters
Pthe type
Returns
true when type can hold null value
Since
0.2
Here is the call graph for this function:

◆ isRequired()

template<typename... C>
bool anch::val::isRequired ( )
constexpr

Check if constraints contain Required

Template Parameters
Cthe constraints types
Returns
true when one of the constraints is Required
Since
0.2
Here is the call graph for this function:

◆ isValid()

template<typename T>
bool anch::val::isValid ( const T & value)

Check if value is valid ( anch::val::validate returns no violation)

Template Parameters
Tthe value type
Parameters
valuethe value
Returns
true when value is valid
Since
0.3
Here is the call graph for this function:

◆ items()

template<typename... C>
anch::val::Items< std::decay_t< C >... > anch::val::items ( C &&... constraints)

Items constraints

Template Parameters
Cthe items constraints types
Parameters
constraintsthe constraints to check on every item
Returns
the constraint
Since
0.2
Here is the call graph for this function:

◆ length()

anch::val::Length anch::val::length ( std::size_t length)
constexpr

XSD length facet

Parameters
lengththe length (code points of a string, items of a collection)
Returns
the constraint
Since
0.3
Here is the call graph for this function:

◆ maximum()

template<anch::val::Number N>
anch::val::Maximum< N > anch::val::maximum ( N value,
bool exclusive = false )
constexpr

maximum constraint

Template Parameters
Nthe bound type
Parameters
valuethe maximum value
exclusivevalue has to be lower than value
Returns
the constraint
Since
0.2
Here is the call graph for this function:

◆ maxItems()

anch::val::MaxItems anch::val::maxItems ( std::size_t count)
constexpr

maxItems constraint

Parameters
countthe maximum number of items
Returns
the constraint
Since
0.2
Here is the call graph for this function:

◆ maxLength()

anch::val::MaxLength anch::val::maxLength ( std::size_t length)
constexpr

maxLength constraint

Parameters
lengththe maximum length (Unicode code points)
Returns
the constraint
Since
0.2
Here is the call graph for this function:

◆ maxProperties()

anch::val::MaxProperties anch::val::maxProperties ( std::size_t count)
constexpr

maxProperties constraint (map entries)

Parameters
countthe maximum number of entries
Returns
the constraint
Since
0.2
Here is the call graph for this function:

◆ minimum()

template<anch::val::Number N>
anch::val::Minimum< N > anch::val::minimum ( N value,
bool exclusive = false )
constexpr

minimum constraint

Template Parameters
Nthe bound type
Parameters
valuethe minimum value
exclusivevalue has to be greater than value
Returns
the constraint
Since
0.2
Here is the call graph for this function:

◆ minItems()

anch::val::MinItems anch::val::minItems ( std::size_t count)
constexpr

minItems constraint

Parameters
countthe minimum number of items
Returns
the constraint
Since
0.2
Here is the call graph for this function:

◆ minLength()

anch::val::MinLength anch::val::minLength ( std::size_t length)
constexpr

minLength constraint

Parameters
lengththe minimum length (Unicode code points)
Returns
the constraint
Since
0.2
Here is the call graph for this function:

◆ minProperties()

anch::val::MinProperties anch::val::minProperties ( std::size_t count)
constexpr

minProperties constraint (map entries)

Parameters
countthe minimum number of entries
Returns
the constraint
Since
0.2
Here is the call graph for this function:

◆ multipleOf()

template<anch::val::Number N>
anch::val::MultipleOf< N > anch::val::multipleOf ( N value)

multipleOf constraint

Template Parameters
Nthe divisor type
Parameters
valuethe divisor
Returns
the constraint
Exceptions
std::invalid_argumentdivisor is not strictly positive
Since
0.2
Here is the call graph for this function:

◆ operator<<()

std::ostream & anch::val::operator<< ( std::ostream & out,
const anch::val::Path & path )

Write path as a JSON Pointer

Parameters
outthe output stream
paththe path
Returns
the output stream

◆ pattern()

anch::val::Pattern anch::val::pattern ( std::string_view source)

pattern constraint

Parameters
sourcethe pattern (ECMA-262 regular expression with the default compiler)
Returns
the constraint
Exceptions
std::regex_errorinvalid pattern (default compiler)
Since
0.2
Here is the call graph for this function:

◆ registerFormat()

void anch::val::registerFormat ( const std::string & name,
anch::val::FormatValidator validator )

Register format validator (thread safe). It replaces the validator already registered with this name.
Registered formats:

  • email : dot-atom local part (64 characters at most) and host name domain (no quoted local part nor address literal) ;
  • hostname : RFC 1123 host name (labels of 63 characters at most, 253 characters at most) ;
  • ipv4 and ipv6 : inet_pton (no leading zero in IPv4 addresses, no IPv6 zone) ;
  • uri : RFC 3986 scheme followed by URI characters (percent encoding is checked, not the URI structure) ;
  • byte : base64 with padding (RFC 4648) ;
  • uuid (with ANCH_UUID ), date-time and date (with ANCH_DATE , RFC 3339, anch-date range: years 1678 to 2261 for date-time ).
Parameters
namethe format name
validatorthe validator, nullptr to remove the format
Since
0.2
Here is the call graph for this function:

◆ setPatternCompiler() [1/2]

void anch::val::setPatternCompiler ( anch::val::PatternCompiler compiler)

Set the pattern compiler used by anch::val::pattern . The default compiler uses std::regex (ECMAScript grammar) and std::regex_search . Another engine (RE2, PCRE2, ...) can be used for performance or for UTF-8 support: std::regex works on bytes ( . matches one byte, character classes do not contain multi-bytes characters).
The compiler is used when patterns are built: it has to be set before mappers registration (mappers are registered on their first use). A pattern is compiled once for every format (each format registers the description of a type): its matcher is shared until the compiler of its syntax is changed.

Parameters
compilerthe compiler, nullptr to restore the default compiler
Since
0.2
Here is the call graph for this function:

◆ setPatternCompiler() [2/2]

void anch::val::setPatternCompiler ( anch::val::PatternSyntax syntax,
anch::val::PatternCompiler compiler )

Set the pattern compiler of a syntax (see setPatternCompiler). The default XSD compiler translates XSD patterns to std::regex , matched on the whole value ( std::regex_match ), when the translation keeps their meaning: ^ and $ are ordinary characters, \s is [ \t\n\r] , \d is [0-9] (ASCII digits only). Constructs which need Unicode properties ( \p{..} , \P{..} , \i , \I , \c , \C , \w , \W ) and character class subtractions ( [a-z-[aeiou]] ) are refused ( std::invalid_argument ): another compiler (ICU, PCRE2, ...) can be plugged for them.

Parameters
syntaxthe pattern syntax
compilerthe compiler, nullptr to restore the default compiler
Since
0.3
Here is the call graph for this function:

◆ totalDigits()

anch::val::TotalDigits anch::val::totalDigits ( std::size_t digits)
constexpr

XSD totalDigits facet

Parameters
digitsthe maximum number of digits
Returns
the constraint
Since
0.3
Here is the call graph for this function:

◆ validate()

template<typename T>
std::vector< anch::val::Violation > anch::val::validate ( const T & value)

Validate value without serializing it: constraints of the fields of described objects ( anch::Mapping ) and of their nested values. Every violation is returned with its path ( anch::val::Path , JSON Pointer with pointer() ), in fields registration order.

// Every violation of a described object, without serialization
const std::vector<Pet> pets{{"Rex", {"dog"}}, {"", {"cat", ""}}};
for(const anch::val::Violation& violation : anch::val::validate(pets)) {
std::cout << violation.path << ": " << violation.message << std::endl;
}
// /1/name: Length 0 is lower than minLength 1
// /1/tags/1: Length 0 is lower than minLength 1
Template Parameters
Tthe value type (described object, collection, map, std::optional , ...)
Parameters
valuethe value
Returns
the violations, empty when value is valid
Since
0.3
Here is the call graph for this function:

◆ xsdPattern()

anch::val::Pattern anch::val::xsdPattern ( std::string_view source)

XSD pattern facet: the pattern matches the whole value

Parameters
sourcethe pattern (XML Schema regular expression)
Returns
the constraint
Exceptions
std::invalid_argumentpattern not supported by the default compiler (see setPatternCompiler)
std::regex_errorinvalid pattern (default compiler)
Since
0.3
Here is the call graph for this function:

Variable Documentation

◆ required

anch::val::Required anch::val::required {}
inlineconstexpr

Required constraint instance

◆ uniqueItems

anch::val::UniqueItems anch::val::uniqueItems {}
inlineconstexpr

UniqueItems constraint instance