AnCH validation library defines fields constraints independently of formats, and validates described objects ( anch::Mapping , anch-utils) without serializing them. anch-json checks the constraints when it reads and writes values ; other formats (CSV, XML) and schema generators (JSON Schema, XSD) use the same constraints (see anch-val.todo ).
CMake target anch::val (built with anch-json, which depends on it). uuid , date-time and date formats are built with ANCH_UUID and ANCH_DATE (anch-uuid and anch-date are optional dependencies):
See installation.
Constraints are checked at compile time against the value type: minLength on an integer does not compile. null and absent values are not checked ( anch::val::required is a field constraint, checked by formats).
| Constraint | Field type | JSON Schema keyword |
|---|---|---|
anch::val::minLength(n) , anch::val::maxLength(n) | string (length in Unicode code points) | minLength , maxLength |
anch::val::pattern(regex) | string (searched, not anchored) | pattern |
anch::val::format(name) | string (registered formats only) | format |
anch::val::minimum(v) , anch::val::maximum(v) , anch::val::exclusiveMinimum(v) , anch::val::exclusiveMaximum(v) | number | minimum , maximum , exclusiveMinimum , exclusiveMaximum |
anch::val::multipleOf(v) | number (exact for integers, relative tolerance for floating point numbers) | multipleOf |
anch::val::minItems(n) , anch::val::maxItems(n) , anch::val::uniqueItems | collection | minItems , maxItems , uniqueItems |
anch::val::minProperties(n) , anch::val::maxProperties(n) | map | minProperties , maxProperties |
anch::val::allowed({...}) | any comparable type | enum |
anch::val::items(constraints...) | collection or map: constraints are checked on every item | items , additionalProperties |
anch::val::custom<T>(predicate, message) | T |
Patterns use std::regex (ECMAScript grammar, on bytes: no UTF-8 character classes) by default: anch::val::setPatternCompiler plugs another engine. Formats are checked by validators registered with anch::val::registerFormat ( email , hostname , ipv4 , ipv6 , uri , byte , and uuid , date-time , date with ANCH_UUID / ANCH_DATE are built-in) ; unknown formats are not checked.
Integers bounds are compared without conversion (an uint64_t field accepts minimum(-1) ).
| XSD facet | Constraint |
|---|---|
length , minLength , maxLength | anch::val::length(n) , anch::val::minLength(n) , anch::val::maxLength(n) (strings: code points ; length also counts list items) |
pattern | anch::val::xsdPattern(regex) (matches the whole value) |
enumeration | anch::val::allowed({...}) |
minInclusive , maxInclusive , minExclusive , maxExclusive | anch::val::minimum(v) , anch::val::maximum(v) , anch::val::exclusiveMinimum(v) , anch::val::exclusiveMaximum(v) |
totalDigits , fractionDigits | anch::val::totalDigits(n) , anch::val::fractionDigits(n) (numbers and decimal strings) |
minOccurs , maxOccurs | anch::val::required , anch::val::minItems(n) , anch::val::maxItems(n) |
whiteSpace is a normalization, applied by formats. XSD patterns have their own compiler ( setPatternCompiler): the default one translates them to std::regex when the translation keeps their meaning ( ^ and $ are ordinary characters, \d is ASCII digits) and refuses Unicode properties ( \p{..} , \i , \c , \w ) and character class subtractions:
A constraint is a value with a check(value, message) function ; its parameters are public, for schema generators:
Usage with anch-json:
anch::val::validate checks a value without serializing it: constraints of the fields of described objects and of their nested values (objects, collections items, maps values, std::optional and pointers values). It does not stop on the first violation: every violation is returned with its path ( Path: field names, map keys and items indexes, formatted as a JSON Pointer by pointer() ), in fields registration order. Fields registered with a setter only are not checked, and required fields are not checked (an object always has its fields). anch::val::isValid returns true when there is no violation.
Values are validatable when they are scalars ( anch::val::IsScalar : numbers, enumerations, strings, UUID, dates, ... ; it can be specialized for other value types), described objects, or std::optional , pointers, collections and maps of them: a class which is neither described nor a scalar is refused at compile time, as its description is probably not visible (see anch::Mapping ).
Constraints are checked in a context ( anch::val::CheckContext ): it enables or disables checks and reports violations. anch::val::ValidationContext collects every violation with its path:
A format specializes anch::val::CheckContext for its contexts (anch-json: reader and writer contexts, which raise anch::json::MappingError ):