AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
constraints.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++23 standard
3 Copyright (C) 2026 Vincent Lachenal
4
5 This file is part of ANCH Framework.
6
7 ANCH Framework is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 ANCH Framework is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with ANCH Framework. If not, see <https://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include "anch-val-export.hpp"
23
24#include <chrono>
25#include <concepts>
26#include <cstddef>
27#include <cstdint>
28#include <functional>
29#include <initializer_list>
30#include <memory>
31#include <optional>
32#include <ranges>
33#include <string>
34#include <string_view>
35#include <tuple>
36#include <utility>
37#include <variant>
38#include <iosfwd>
39#include <type_traits>
40#include <vector>
41
42
43namespace anch::val {
44
53 class ANCH_VAL_EXPORT Path {
54 public:
56 using Segment = std::variant<std::string, std::size_t>;
57
58 // Attributes +
59 private:
61 std::vector<Segment> _segments;
62 // Attributes -
63
64 // Constructors +
65 public:
67 Path() = default;
68
74 explicit Path(std::vector<Segment> segments);
75 // Constructors -
76
77 // Methods +
78 public:
84 const std::vector<Segment>& segments() const noexcept;
85
91 bool empty() const noexcept;
92
99 std::string pointer() const;
100
108 bool operator==(const Path& other) const = default;
109 // Methods -
110 };
111
120 ANCH_VAL_EXPORT std::ostream& operator<<(std::ostream& out, const anch::val::Path& path);
121
129 struct ANCH_VAL_EXPORT Violation {
132
134 std::string message;
135 };
136
146 class ANCH_VAL_EXPORT ValidationContext {
147
148 // Attributes +
149 private:
151 std::vector<anch::val::Path::Segment> _path;
152
154 std::vector<anch::val::Violation> _violations;
155 // Attributes -
156
157 // Methods +
158 public:
164 void enter(std::string_view field);
165
171 void enter(std::size_t index);
172
176 void leave();
177
183 void add(const std::string& message);
184
190 std::vector<anch::val::Violation>& violations();
191 // Methods -
192
193 };
194
195 // Items constraint early declaration
196 template<typename... C>
197 struct Items;
198
207 template<typename N>
208 concept Number = std::floating_point<N>
209 || (std::integral<N> && !std::same_as<N, bool> && !std::same_as<N, char> && !std::same_as<N, wchar_t>
210 && !std::same_as<N, char8_t> && !std::same_as<N, char16_t> && !std::same_as<N, char32_t>);
211
219 template<typename V>
220 concept StringLike = std::convertible_to<const V&, std::string_view>;
221
229 template<typename V>
230 concept MapLike = std::ranges::sized_range<const V> && requires {
231 typename V::key_type;
232 typename V::mapped_type;
233 };
234
242 template<typename V>
243 concept Collection = std::ranges::sized_range<const V> && !StringLike<V> && !MapLike<V>;
244
245} // anch::val
246
247// Value types of other libraries (declared without dependency) +
248namespace anch {
249 class UUID;
250}
251
252namespace anch::date {
253 class Date;
254 class DateTime;
255 class Time;
256}
257// Value types of other libraries -
258
259namespace anch::val {
260
273 template<typename T>
274 struct IsScalar: std::bool_constant<std::is_arithmetic_v<T> || std::is_enum_v<T> || anch::val::StringLike<T>> {};
275
277 template<>
278 struct IsScalar<anch::UUID>: std::true_type {};
279
281 template<>
282 struct IsScalar<anch::date::DateTime>: std::true_type {};
283
285 template<>
286 struct IsScalar<anch::date::Date>: std::true_type {};
287
289 template<>
290 struct IsScalar<anch::date::Time>: std::true_type {};
291
293 template<typename C, typename D>
294 struct IsScalar<std::chrono::time_point<C, D>>: std::true_type {};
295
297 template<typename R, typename P>
298 struct IsScalar<std::chrono::duration<R, P>>: std::true_type {};
299
307 template<typename T>
309
310} // anch::val
311
312namespace anch::val {
313
330 template<typename X>
332
333} // anch::val
334
335namespace anch::val::detail {
336
338 template<typename P>
339 struct IsNullable: std::is_pointer<P> {};
340
342 template<typename P>
343 struct IsNullable<std::optional<P>>: std::true_type {};
344
346 template<typename P>
347 struct IsNullable<std::shared_ptr<P>>: std::true_type {};
348
350 template<typename P, typename D>
351 struct IsNullable<std::unique_ptr<P,D>>: std::true_type {};
352
354 template<typename P>
355 struct UnwrapType {
357 using type = P;
358 };
359
361 template<typename P>
362 struct UnwrapType<std::optional<P>> {
364 using type = P;
365 };
366
368 template<typename P>
369 struct UnwrapType<std::shared_ptr<P>> {
371 using type = P;
372 };
373
375 template<typename P, typename D>
376 struct UnwrapType<std::unique_ptr<P,D>> {
378 using type = P;
379 };
380
382 template<typename P>
383 struct UnwrapType<P*> {
385 using type = P;
386 };
387
389 template<typename P>
390 using Unwrap = typename UnwrapType<std::remove_cvref_t<P>>::type;
391
393 template<typename C, typename V>
394 struct IsValueConstraint: std::bool_constant<requires(const C& constraint, const V& value, std::string& message) {
395 { constraint.check(value, message) } -> std::same_as<bool>;
396 }> {};
397
406 template<typename V, typename... C>
407 constexpr bool itemsApplicable() {
408 if constexpr (anch::val::MapLike<V>) {
409 return (IsValueConstraint<C, Unwrap<typename V::mapped_type>>::value && ...);
410 } else if constexpr (anch::val::Collection<V>) {
411 return (IsValueConstraint<C, Unwrap<std::ranges::range_value_t<V>>>::value && ...);
412 } else {
413 return false;
414 }
415 }
416
418 template<typename... C, typename V>
419 struct IsValueConstraint<anch::val::Items<C...>, V>: std::bool_constant<itemsApplicable<V, C...>()> {};
420
429 ANCH_VAL_EXPORT bool matchesFormat(const std::string& name, std::string_view value);
430
438 ANCH_VAL_EXPORT std::size_t codePoints(std::string_view str);
439
448 ANCH_VAL_EXPORT std::optional<std::pair<std::size_t, std::size_t>> decimalDigits(std::string_view decimal);
449
460 template<typename V>
461 std::string decimalOf(const V& value);
462
472 template<typename V>
473 std::string formatValue(const V& value);
474
486 template<typename C, typename P, typename X>
487 void applyConstraint(const C& constraint, const P& value, X& context);
488
501 template<typename P, typename X, typename... C>
502 void checkConstraints(const P& value, X& context, const C&... constraints);
503
504} // anch::val::detail
505
506namespace anch::val {
507
523 struct Required {};
524
526 inline constexpr anch::val::Required required{};
527
537 struct ANCH_VAL_EXPORT MinLength {
539 std::size_t value;
540
551 template<anch::val::StringLike V>
552 bool check(const V& str, std::string& message) const;
553 };
554
564 struct ANCH_VAL_EXPORT MaxLength {
566 std::size_t value;
567
578 template<anch::val::StringLike V>
579 bool check(const V& str, std::string& message) const;
580 };
581
593 template<anch::val::Number N>
594 struct Minimum {
597
599 bool exclusive = false;
600
611 template<anch::val::Number V>
612 bool check(const V& number, std::string& message) const;
613 };
614
626 template<anch::val::Number N>
627 struct Maximum {
630
632 bool exclusive = false;
633
644 template<anch::val::Number V>
645 bool check(const V& number, std::string& message) const;
646 };
647
660 template<anch::val::Number N>
661 struct MultipleOf {
664
675 template<anch::val::Number V>
676 bool check(const V& number, std::string& message) const;
677 };
678
686 struct ANCH_VAL_EXPORT MinItems {
688 std::size_t value;
689
700 template<anch::val::Collection V>
701 bool check(const V& items, std::string& message) const;
702 };
703
711 struct ANCH_VAL_EXPORT MaxItems {
713 std::size_t value;
714
725 template<anch::val::Collection V>
726 bool check(const V& items, std::string& message) const;
727 };
728
736 struct ANCH_VAL_EXPORT MinProperties {
738 std::size_t value;
739
750 template<anch::val::MapLike V>
751 bool check(const V& map, std::string& message) const;
752 };
753
761 struct ANCH_VAL_EXPORT MaxProperties {
763 std::size_t value;
764
775 template<anch::val::MapLike V>
776 bool check(const V& map, std::string& message) const;
777 };
778
789 struct ANCH_VAL_EXPORT UniqueItems {
800 template<anch::val::Collection V>
801 requires std::equality_comparable<std::ranges::range_value_t<V>>
802 bool check(const V& items, std::string& message) const;
803 };
804
807
817 template<typename A>
818 struct Allowed {
820 std::vector<A> values;
821
832 template<typename V>
833 requires std::equality_comparable_with<V, A>
834 bool check(const V& value, std::string& message) const;
835 };
836
851 template<typename... C>
852 struct Items {
854 std::tuple<C...> constraints;
855
865 template<typename V, typename X>
866 void apply(const V& items, X& context) const;
867 };
868
879 struct ANCH_VAL_EXPORT Length {
881 std::size_t value;
882
893 template<anch::val::StringLike V>
894 bool check(const V& str, std::string& message) const;
895
906 template<anch::val::Collection V>
907 bool check(const V& items, std::string& message) const;
908 };
909
922 struct ANCH_VAL_EXPORT TotalDigits {
924 std::size_t value;
925
936 template<typename V>
938 bool check(const V& decimal, std::string& message) const;
939 };
940
950 struct ANCH_VAL_EXPORT FractionDigits {
952 std::size_t value;
953
964 template<typename V>
966 bool check(const V& decimal, std::string& message) const;
967 };
968
974 using PatternMatcher = std::function<bool(std::string_view)>;
975
981 using PatternCompiler = std::function<anch::val::PatternMatcher(std::string_view)>;
982
994
1008 ANCH_VAL_EXPORT void setPatternCompiler(anch::val::PatternCompiler compiler);
1009
1025
1037 struct ANCH_VAL_EXPORT Pattern {
1039 std::shared_ptr<const anch::val::PatternMatcher> matcher;
1040
1042 std::string source;
1043
1046
1057 template<anch::val::StringLike V>
1058 bool check(const V& str, std::string& message) const;
1059 };
1060
1066 using FormatValidator = std::function<bool(std::string_view)>;
1067
1085 ANCH_VAL_EXPORT void registerFormat(const std::string& name, anch::val::FormatValidator validator);
1086
1097 struct ANCH_VAL_EXPORT Format {
1099 std::string name;
1100
1111 template<anch::val::StringLike V>
1112 bool check(const V& str, std::string& message) const;
1113 };
1114
1124 template<typename V>
1125 struct Custom {
1127 std::function<bool(const V&)> predicate;
1128
1130 std::string message;
1131
1140 bool check(const V& value, std::string& error) const;
1141 };
1142
1151 template<typename C, typename V>
1152 concept ValueConstraint = anch::val::detail::IsValueConstraint<std::remove_cvref_t<C>, V>::value;
1153
1163 template<typename C, typename P>
1164 concept FieldConstraint = std::same_as<std::remove_cvref_t<C>, anch::val::Required>
1166
1176 template<typename... C>
1177 constexpr bool isRequired() {
1178 return (std::same_as<std::remove_cvref_t<C>, anch::val::Required> || ...);
1179 }
1180
1190 template<typename... C>
1191 constexpr bool hasValueConstraints() {
1192 return (!std::same_as<std::remove_cvref_t<C>, anch::val::Required> || ...);
1193 }
1194
1204 template<typename P>
1205 constexpr bool isNullable() {
1206 return anch::val::detail::IsNullable<std::remove_cvref_t<P>>::value;
1207 }
1208
1209 // Constraints factories +
1219 constexpr anch::val::MinLength minLength(std::size_t length);
1220
1230 constexpr anch::val::MaxLength maxLength(std::size_t length);
1231
1244 template<anch::val::Number N>
1245 constexpr anch::val::Minimum<N> minimum(N value, bool exclusive = false);
1246
1258 template<anch::val::Number N>
1260
1273 template<anch::val::Number N>
1274 constexpr anch::val::Maximum<N> maximum(N value, bool exclusive = false);
1275
1287 template<anch::val::Number N>
1289
1303 template<anch::val::Number N>
1305
1315 constexpr anch::val::MinItems minItems(std::size_t count);
1316
1326 constexpr anch::val::MaxItems maxItems(std::size_t count);
1327
1337 constexpr anch::val::MinProperties minProperties(std::size_t count);
1338
1348 constexpr anch::val::MaxProperties maxProperties(std::size_t count);
1349
1361 ANCH_VAL_EXPORT anch::val::Pattern pattern(std::string_view source);
1362
1376 ANCH_VAL_EXPORT anch::val::Pattern xsdPattern(std::string_view source);
1377
1387 constexpr anch::val::Length length(std::size_t length);
1388
1398 constexpr anch::val::TotalDigits totalDigits(std::size_t digits);
1399
1409 constexpr anch::val::FractionDigits fractionDigits(std::size_t digits);
1410
1420 anch::val::Format format(std::string name);
1421
1433 template<typename A>
1434 auto allowed(std::initializer_list<A> values);
1435
1447 template<typename... C>
1449
1462 template<typename V>
1463 anch::val::Custom<V> custom(std::function<bool(const V&)> predicate, std::string message);
1464 // Constraints factories -
1465
1466} // anch::val
1467
1468#include "val/impl/constraints.hpp"
UUID generator and parser.
Definition uuid.hpp:51
Calendar day (RFC 3339 full-date , JSON schema and OpenAPI date format, XSD xs:date ).
Definition date.hpp:63
Point in time.
Definition dateTime.hpp:89
Time of day (RFC 3339 partial-time , SQL TIME , XSD xs:time without time zone).
Definition time.hpp:48
Path of a value from the root value (validation violations): segments are field names or map keys and...
Definition constraints.hpp:53
Path()=default
Path(std::vector< Segment > segments)
std::variant< std::string, std::size_t > Segment
Definition constraints.hpp:56
bool empty() const noexcept
std::string pointer() const
const std::vector< Segment > & segments() const noexcept
Validation context.
Definition constraints.hpp:146
std::vector< anch::val::Violation > & violations()
void enter(std::string_view field)
void enter(std::size_t index)
void add(const std::string &message)
Definition constraints.hpp:243
Definition constraints.hpp:1164
Definition constraints.hpp:230
Definition constraints.hpp:208
Scalar type (see anch::val::IsScalar ).
Definition constraints.hpp:308
Definition constraints.hpp:220
Definition constraints.hpp:1152
Dates namespace.
AnCH validation library namespace.
Definition mapper.hpp:150
PatternSyntax
Definition constraints.hpp:988
@ XSD
Definition constraints.hpp:992
@ ECMASCRIPT
Definition constraints.hpp:990
anch::val::Format format(std::string name)
constexpr anch::val::Minimum< N > minimum(N value, bool exclusive=false)
constexpr anch::val::MinItems minItems(std::size_t count)
std::function< bool(std::string_view)> PatternMatcher
Definition constraints.hpp:974
constexpr anch::val::MinProperties minProperties(std::size_t count)
std::function< anch::val::PatternMatcher(std::string_view)> PatternCompiler
Definition constraints.hpp:981
constexpr bool hasValueConstraints()
Definition constraints.hpp:1191
anch::val::Items< std::decay_t< C >... > items(C &&... constraints)
constexpr anch::val::MinLength minLength(std::size_t length)
auto allowed(std::initializer_list< A > values)
constexpr bool isRequired()
Definition constraints.hpp:1177
constexpr anch::val::UniqueItems uniqueItems
Definition constraints.hpp:806
constexpr anch::val::MaxProperties maxProperties(std::size_t count)
constexpr anch::val::Length length(std::size_t length)
void registerFormat(const std::string &name, anch::val::FormatValidator validator)
constexpr anch::val::TotalDigits totalDigits(std::size_t digits)
constexpr anch::val::Required required
Definition constraints.hpp:526
void setPatternCompiler(anch::val::PatternCompiler compiler)
std::function< bool(std::string_view)> FormatValidator
Definition constraints.hpp:1066
constexpr anch::val::Maximum< N > maximum(N value, bool exclusive=false)
anch::val::Custom< V > custom(std::function< bool(const V &)> predicate, std::string message)
anch::val::MultipleOf< N > multipleOf(N value)
constexpr anch::val::Minimum< N > exclusiveMinimum(N value)
constexpr anch::val::Maximum< N > exclusiveMaximum(N value)
constexpr bool isNullable()
Definition constraints.hpp:1205
anch::val::Pattern pattern(std::string_view source)
anch::val::Pattern xsdPattern(std::string_view source)
constexpr anch::val::MaxItems maxItems(std::size_t count)
constexpr anch::val::MaxLength maxLength(std::size_t length)
constexpr anch::val::FractionDigits fractionDigits(std::size_t digits)
AnCH framework base namespace.
Definition app.hpp:30
Allowed values constraint ( enum ).
Definition constraints.hpp:818
bool check(const V &value, std::string &message) const
std::vector< A > values
Definition constraints.hpp:820
Context in which constraints are checked.
Definition constraints.hpp:331
Custom constraint.
Definition constraints.hpp:1125
std::function< bool(const V &)> predicate
Definition constraints.hpp:1127
std::string message
Definition constraints.hpp:1130
bool check(const V &value, std::string &error) const
String format constraint ( format ).
Definition constraints.hpp:1097
bool check(const V &str, std::string &message) const
std::string name
Definition constraints.hpp:1099
Maximum number of fraction digits constraint (XSD fractionDigits facet).
Definition constraints.hpp:950
bool check(const V &decimal, std::string &message) const
std::size_t value
Definition constraints.hpp:952
Scalar type: value type checked as a whole, without fields (it is not described by anch::Mapping ).
Definition constraints.hpp:274
Items constraints.
Definition constraints.hpp:852
void apply(const V &items, X &context) const
std::tuple< C... > constraints
Definition constraints.hpp:854
Exact length constraint (XSD length facet).
Definition constraints.hpp:879
bool check(const V &items, std::string &message) const
std::size_t value
Definition constraints.hpp:881
bool check(const V &str, std::string &message) const
Maximum number of items constraint ( maxItems ).
Definition constraints.hpp:711
std::size_t value
Definition constraints.hpp:713
bool check(const V &items, std::string &message) const
Maximum string length constraint ( maxLength ).
Definition constraints.hpp:564
bool check(const V &str, std::string &message) const
std::size_t value
Definition constraints.hpp:566
Maximum number of map entries constraint ( maxProperties ).
Definition constraints.hpp:761
std::size_t value
Definition constraints.hpp:763
bool check(const V &map, std::string &message) const
Maximum value constraint ( maximum and exclusiveMaximum ).
Definition constraints.hpp:627
bool check(const V &number, std::string &message) const
N value
Definition constraints.hpp:629
bool exclusive
Definition constraints.hpp:632
Minimum number of items constraint ( minItems ).
Definition constraints.hpp:686
bool check(const V &items, std::string &message) const
std::size_t value
Definition constraints.hpp:688
Minimum string length constraint ( minLength ).
Definition constraints.hpp:537
bool check(const V &str, std::string &message) const
std::size_t value
Definition constraints.hpp:539
Minimum number of map entries constraint ( minProperties ).
Definition constraints.hpp:736
bool check(const V &map, std::string &message) const
std::size_t value
Definition constraints.hpp:738
Minimum value constraint ( minimum and exclusiveMinimum ).
Definition constraints.hpp:594
bool check(const V &number, std::string &message) const
bool exclusive
Definition constraints.hpp:599
N value
Definition constraints.hpp:596
Multiple of constraint ( multipleOf ).
Definition constraints.hpp:661
bool check(const V &number, std::string &message) const
N value
Definition constraints.hpp:663
String pattern constraint ( pattern ).
Definition constraints.hpp:1037
bool check(const V &str, std::string &message) const
std::string source
Definition constraints.hpp:1042
anch::val::PatternSyntax syntax
Definition constraints.hpp:1045
std::shared_ptr< const anch::val::PatternMatcher > matcher
Definition constraints.hpp:1039
Required field constraint.
Definition constraints.hpp:523
Maximum number of digits constraint (XSD totalDigits facet).
Definition constraints.hpp:922
std::size_t value
Definition constraints.hpp:924
bool check(const V &decimal, std::string &message) const
Unique items constraint ( uniqueItems ).
Definition constraints.hpp:789
bool check(const V &items, std::string &message) const
Constraint violation found by a validation (see anch::val::ValidationContext ).
Definition constraints.hpp:129
std::string message
Definition constraints.hpp:134
anch::val::Path path
Definition constraints.hpp:131