format.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. #pragma once
  2. #include <jvalidate/_macro.h>
  3. #include <cctype>
  4. #include <chrono>
  5. #include <cstddef>
  6. #include <cstring>
  7. #include <ctime>
  8. #include <string>
  9. #include <string_view>
  10. #include <unordered_map>
  11. #include <utility>
  12. #ifdef JVALIDATE_HAS_IDNA
  13. #include <ada/idna/to_unicode.h>
  14. #include <ada/idna/validity.h>
  15. #endif
  16. #include <jvalidate/detail/expect.h>
  17. #include <jvalidate/detail/idna_special_cases.h>
  18. #include <jvalidate/detail/pointer.h>
  19. #include <jvalidate/detail/relative_pointer.h>
  20. #include <jvalidate/detail/string.h>
  21. #include <jvalidate/forward.h>
  22. #define CONSTRUCTS(TYPE) format::ctor_as_valid<detail::TYPE>
  23. #define UTF32(FN) JVALIDATE_IIF(JVALIDATE_HAS_IDNA, format::utf32<format::FN<char32_t>>, nullptr)
  24. namespace jvalidate::format {
  25. bool date(std::string_view dt);
  26. bool time(std::string_view dt);
  27. bool date_time(std::string_view dt);
  28. bool duration(std::string_view dur);
  29. template <typename CharT = char> bool uri(std::basic_string_view<CharT> uri);
  30. template <typename CharT = char> bool uri_reference(std::basic_string_view<CharT> uri);
  31. bool uri_template(std::u32string_view uri);
  32. bool uuid(std::string_view id);
  33. template <typename CharT = char> bool hostname(std::basic_string_view<CharT> name);
  34. bool ipv4(std::string_view ip);
  35. bool ipv6(std::string_view ip);
  36. template <typename CharT = char> bool email(std::basic_string_view<CharT> em);
  37. }
  38. namespace jvalidate::format::detail {
  39. struct result {
  40. ptrdiff_t consumed;
  41. bool valid;
  42. };
  43. inline bool is_leapyear(int y) { return (y % 400) == 0 || ((y % 4) == 0 && (y % 100) != 0); }
  44. inline bool illegal_date(int y, int m, int d) {
  45. static constexpr int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  46. if (is_leapyear(y) && m == 1) {
  47. --d;
  48. }
  49. return d > days[m];
  50. }
  51. inline result date(std::string_view dt) {
  52. struct tm tm;
  53. if (auto end = strptime(dt.data(), "%Y-%m-%d", &tm); end) {
  54. if ((end - dt.data()) != 10 || illegal_date(tm.tm_year + 1900, tm.tm_mon, tm.tm_mday)) {
  55. return {.consumed = 0, .valid = false};
  56. }
  57. return {.consumed = end - dt.data(), .valid = true};
  58. }
  59. return {.consumed = 0L, .valid = false};
  60. }
  61. inline bool is_leapsecond(std::tm tm) {
  62. if (tm.tm_sec != 60) {
  63. return true;
  64. }
  65. #if __cpp_lib_chrono >= 201907L
  66. tm.tm_isdst = -1;
  67. std::chrono::seconds time(std::mktime(&tm));
  68. auto const & leap_seconds = std::chrono::get_tzdb().leap_seconds;
  69. return std::ranges::find(leap_seconds, time) != leap_seconds.end();
  70. #else
  71. return false;
  72. #endif
  73. }
  74. // https://www.rfc-editor.org/rfc/rfc6570.html#section-1.5
  75. inline bool is_uschar(int c) {
  76. using P = std::pair<int, int>;
  77. constexpr std::array data{
  78. P{0xA0, 0xD7FF}, P{0xF900, 0xFDCF}, P{0xFDF0, 0xFFEF}, P{0x10000, 0x1FFFD},
  79. P{0x20000, 0x2FFFD}, P{0x30000, 0x3FFFD}, P{0x40000, 0x4FFFD}, P{0x50000, 0x5FFFD},
  80. P{0x60000, 0x6FFFD}, P{0x70000, 0x7FFFD}, P{0x80000, 0x8FFFD}, P{0x90000, 0x9FFFD},
  81. P{0xA0000, 0xAFFFD}, P{0xB0000, 0xBFFFD}, P{0xC0000, 0xCFFFD}, P{0xD0000, 0xDFFFD},
  82. P{0xE0000, 0xEFFFD},
  83. };
  84. return std::ranges::any_of(data,
  85. [c](auto & pair) { return c >= pair.first && c <= pair.second; });
  86. }
  87. template <typename CharT>
  88. inline bool is_pchar(std::basic_string_view<CharT> part, size_t & pos,
  89. std::string_view extra_valid_chars = ":@") {
  90. constexpr char const * g_hex_digits = "0123456789ABCDEFabcdef";
  91. if (std::isalnum(part[pos]) || is_uschar(part[pos]) ||
  92. std::strchr("-._~!$&'()*+,;=", part[pos])) {
  93. return true;
  94. }
  95. if (part[pos] == '%') {
  96. return pos + 2 < part.size() && std::strchr(g_hex_digits, part[++pos]) &&
  97. std::strchr(g_hex_digits, part[++pos]);
  98. }
  99. return extra_valid_chars.find(part[pos]) != part.npos;
  100. };
  101. inline bool is_uri_template_literal(std::u32string_view part, size_t & pos) {
  102. constexpr char const * g_hex_digits = "0123456789ABCDEFabcdef";
  103. if (part[pos] == '%') {
  104. return pos + 2 < part.size() && std::strchr(g_hex_digits, part[++pos]) &&
  105. std::strchr(g_hex_digits, part[++pos]);
  106. }
  107. return !std::strchr(R"( "'%<>\^`{|}`)", part[pos]) && part[pos] > 0x1F && part[pos] != 0x7F;
  108. }
  109. inline bool is_uri_template_varchar(std::u32string_view part, size_t & pos) {
  110. constexpr char const * g_hex_digits = "0123456789ABCDEFabcdef";
  111. if (part[pos] == '%') {
  112. return pos + 2 < part.size() && std::strchr(g_hex_digits, part[++pos]) &&
  113. std::strchr(g_hex_digits, part[++pos]);
  114. }
  115. return std::isalnum(part[pos]) || part[pos] == '_';
  116. }
  117. inline bool is_uri_template_expression(std::u32string_view part) {
  118. if (part.empty()) {
  119. return false;
  120. }
  121. if (std::strchr("+#./;?&=,!@|", part[0])) {
  122. part.remove_prefix(1);
  123. }
  124. for (size_t pos = part.find(','); !part.empty();
  125. part.remove_prefix(std::min(part.size(), pos)), pos = part.find(',')) {
  126. std::u32string_view varspec = part.substr(0, pos);
  127. std::u32string_view expand;
  128. if (size_t const mod = varspec.find_first_of(U":*"); mod != varspec.npos) {
  129. expand = varspec.substr(mod + 1);
  130. varspec.remove_suffix(expand.size() + 1);
  131. }
  132. if (expand.empty() || expand == U"*") {
  133. // No Modifier, or Explode
  134. } else if (expand.size() > 4 || expand[0] == '0' ||
  135. not std::ranges::all_of(expand, [](char c) { return std::isdigit(c); })) {
  136. return false;
  137. }
  138. for (size_t i = 0; i < varspec.size(); ++i) {
  139. RETURN_UNLESS(is_uri_template_varchar(varspec, i) || (i > 0 && varspec[i] == '.'), false);
  140. }
  141. }
  142. return true;
  143. }
  144. template <typename CharT> inline bool is_uri_authority(std::basic_string_view<CharT> uri) {
  145. if (size_t pos = uri.find('@'); pos != uri.npos && pos < uri.find('/')) {
  146. for (size_t i = 0; i < pos; ++i) {
  147. if (not is_pchar(uri, i, ":")) {
  148. return false;
  149. }
  150. }
  151. uri.remove_prefix(pos + 1);
  152. }
  153. if (uri[0] == '[') {
  154. size_t pos = uri.find(']');
  155. auto ip = uri.substr(1, pos - 1);
  156. uri.remove_prefix(pos + 1);
  157. if (not ipv6(to_u8(ip))) {
  158. return false;
  159. }
  160. }
  161. if (size_t pos = uri.find(':'); pos != uri.npos) {
  162. if (not std::ranges::all_of(uri.substr(pos + 1), [](auto c) { return std::isdigit(c); })) {
  163. return false;
  164. }
  165. uri.remove_suffix(uri.size() - pos + 1);
  166. }
  167. return ipv4(to_u8(uri)) || hostname(uri);
  168. }
  169. template <typename CharT> bool test_uri_part(std::basic_string_view<CharT> & uri, char delim) {
  170. size_t const pos = uri.find(delim);
  171. if (pos == uri.npos) {
  172. return true;
  173. }
  174. auto part = uri.substr(pos + 1);
  175. uri = uri.substr(0, pos);
  176. for (size_t pos = 0; pos < part.size(); ++pos) {
  177. RETURN_UNLESS(detail::is_pchar(part, pos, ":@/?"), false);
  178. }
  179. return true;
  180. };
  181. }
  182. namespace jvalidate::format {
  183. inline bool date(std::string_view dt) {
  184. auto [consumed, valid] = detail::date(dt);
  185. return valid && consumed == dt.size();
  186. }
  187. inline bool time(std::string_view dt) {
  188. std::tm tm;
  189. char const * end = strptime(dt.data(), "%T", &tm);
  190. if (end == nullptr || end == dt.end() || (end - dt.data()) < 8) {
  191. return false;
  192. }
  193. dt.remove_prefix(end - dt.begin());
  194. if (dt[0] == '.') {
  195. dt.remove_prefix(1);
  196. if (dt.empty() || not std::isdigit(dt[0])) {
  197. return false;
  198. }
  199. while (std::isdigit(dt[0])) {
  200. dt.remove_prefix(1);
  201. }
  202. }
  203. if (dt[0] == 'Z' || dt[0] == 'z') {
  204. return dt.size() == 1 && detail::is_leapsecond(tm);
  205. }
  206. if (std::strchr("+-", dt[0])) {
  207. return strptime(dt.data() + 1, "%R", &tm) == dt.end() && detail::is_leapsecond(tm);
  208. }
  209. return false;
  210. }
  211. inline bool date_time(std::string_view dt) {
  212. auto [size, good] = detail::date(dt);
  213. if (not good || std::strchr("Tt", dt[size]) == nullptr) {
  214. return false;
  215. }
  216. dt.remove_prefix(size + 1);
  217. return time(dt);
  218. }
  219. template <typename CharT> inline bool uri(std::basic_string_view<CharT> uri) {
  220. using delim = detail::char_delimiters<CharT>;
  221. // https://www.rfc-editor.org/rfc/rfc3986.html#appendix-A
  222. if (size_t const pos = uri.find(':'); pos != uri.npos) {
  223. RETURN_UNLESS(std::isalpha(uri[0]), false);
  224. for (size_t i = 1; i < pos; ++i) {
  225. RETURN_UNLESS(std::isalnum(uri[i]) || std::strchr("+-.", uri[i]), false);
  226. }
  227. uri.remove_prefix(pos + 1);
  228. } else {
  229. return false;
  230. }
  231. RETURN_UNLESS(detail::test_uri_part(uri, '#'), false);
  232. RETURN_UNLESS(detail::test_uri_part(uri, '?'), false);
  233. auto path = uri;
  234. if (uri.starts_with(delim::double_slash)) {
  235. uri.remove_prefix(2);
  236. path = uri.substr(std::min(uri.size(), uri.find('/')));
  237. uri.remove_suffix(path.size());
  238. RETURN_UNLESS(detail::is_uri_authority(uri), false);
  239. }
  240. for (size_t i = 0; i < path.size(); ++i) {
  241. RETURN_UNLESS(detail::is_pchar(path, i, "/:@"), false);
  242. }
  243. return true;
  244. }
  245. template <typename CharT> inline bool uri_reference(std::basic_string_view<CharT> uri) {
  246. using delim = detail::char_delimiters<CharT>;
  247. if (jvalidate::format::uri(uri)) {
  248. return true;
  249. }
  250. RETURN_UNLESS(detail::test_uri_part(uri, '#'), false);
  251. RETURN_UNLESS(detail::test_uri_part(uri, '?'), false);
  252. auto path = uri;
  253. if (uri.starts_with(delim::double_slash)) {
  254. uri.remove_prefix(2);
  255. path = uri.substr(std::min(uri.size(), uri.find('/')));
  256. uri.remove_suffix(path.size());
  257. RETURN_UNLESS(detail::is_uri_authority(uri), false);
  258. }
  259. if (size_t const pos = path.find('/'); pos != path.npos) {
  260. for (size_t i = 0; i < pos; ++i) {
  261. RETURN_UNLESS(detail::is_pchar(path, i, "@"), false);
  262. }
  263. path.remove_prefix(pos);
  264. }
  265. for (size_t i = 0; i < path.size(); ++i) {
  266. RETURN_UNLESS(detail::is_pchar(path, i, "/:@"), false);
  267. }
  268. return true;
  269. }
  270. inline bool uri_template(std::u32string_view uri) {
  271. for (size_t i = 0; i < uri.size(); ++i) {
  272. if (uri[i] != '{') {
  273. RETURN_UNLESS(detail::is_uri_template_literal(uri, i), false);
  274. continue;
  275. }
  276. std::u32string_view expr = uri.substr(i + 1);
  277. size_t const pos = expr.find('}');
  278. RETURN_UNLESS(pos != uri.npos, false);
  279. RETURN_UNLESS(detail::is_uri_template_expression(expr.substr(0, pos)), false);
  280. i += pos + 1;
  281. }
  282. return true;
  283. }
  284. inline bool uuid(std::string_view id) {
  285. constexpr char const * g_hex_digits = "0123456789ABCDEFabcdef";
  286. constexpr size_t g_uuid_len = 36;
  287. constexpr size_t g_uuid_tokens = 5;
  288. char tok0[9], tok1[5], tok2[5], tok3[5], tok4[13];
  289. auto is_hex = [](std::string_view s) {
  290. return s.find_first_not_of(g_hex_digits) == std::string::npos;
  291. };
  292. return id.size() == g_uuid_len &&
  293. sscanf(id.data(), "%8s-%4s-%4s-%4s-%12s", tok0, tok1, tok2, tok3, tok4) == g_uuid_tokens &&
  294. is_hex(tok0) && is_hex(tok1) && is_hex(tok2) && is_hex(tok3) && is_hex(tok4);
  295. }
  296. inline bool duration(std::string_view dur) {
  297. auto eat = [&dur](std::string_view text) {
  298. char type;
  299. unsigned int rep;
  300. if (sscanf(dur.data(), "%u%c", &rep, &type) != 2 || text.find(type) == std::string::npos) {
  301. return std::string::npos;
  302. }
  303. dur.remove_prefix(dur.find(type) + 1);
  304. return text.find(type);
  305. };
  306. if (dur[0] != 'P' || dur.size() == 1) {
  307. return false;
  308. }
  309. dur.remove_prefix(1);
  310. if (dur[0] != 'T') {
  311. if (eat("W") != std::string::npos) {
  312. return dur.empty();
  313. }
  314. std::string_view ymd{"YMD"};
  315. while (not ymd.empty() && not dur.empty()) {
  316. if (size_t n = eat(ymd); n != std::string::npos) {
  317. ymd.remove_prefix(n + 1);
  318. } else {
  319. return false;
  320. }
  321. }
  322. if (dur.empty()) {
  323. return true;
  324. }
  325. }
  326. if (dur[0] != 'T' || dur.size() == 1) {
  327. return false;
  328. }
  329. dur.remove_prefix(1);
  330. std::string_view hms{"HMS"};
  331. while (not hms.empty() && not dur.empty()) {
  332. if (size_t n = eat(hms); n != std::string::npos) {
  333. hms.remove_prefix(n + 1);
  334. } else {
  335. return false;
  336. }
  337. }
  338. return dur.empty();
  339. }
  340. template <typename CharT> bool is_invalid_host_char(CharT c) {
  341. return c != '-' && not(std::isalnum(c) || c > 0x7F);
  342. }
  343. template <typename CharT>
  344. bool is_invalid_size_or_boundary_hostname(std::basic_string_view<CharT> name) {
  345. using delim = detail::char_delimiters<CharT>;
  346. return (name.empty() || detail::length_u8(name) >= 64 ||
  347. (name.size() >= 4 && name.substr(2).starts_with(delim::illegal_dashes_ulabel)) ||
  348. name[0] == '-' || name.back() == '-');
  349. }
  350. #if !JVALIDATE_HAS_IDNA
  351. inline bool hostname_part(std::string_view name) {
  352. using delim = detail::char_delimiters<char>;
  353. if (is_invalid_size_or_boundary_hostname(name)) {
  354. return false;
  355. }
  356. return std::ranges::none_of(name, [](char c) { return c != '-' && not std::isalnum(c); });
  357. }
  358. #else
  359. template <typename CharT> inline bool hostname_part(std::basic_string_view<CharT> name) {
  360. using delim = detail::char_delimiters<CharT>;
  361. if (name.starts_with(delim::punycode_prefix)) {
  362. std::u32string decoded = detail::to_u32(ada::idna::to_unicode(detail::to_u8(name)));
  363. return (decoded != detail::to_u32(name)) && hostname_part<char32_t>(decoded);
  364. }
  365. if (is_invalid_size_or_boundary_hostname(name)) {
  366. return false;
  367. }
  368. if constexpr (std::is_same_v<char, CharT>) {
  369. return std::ranges::none_of(name, [](char c) { return c != '-' && not std::isalnum(c); });
  370. } else {
  371. return ada::idna::is_label_valid(name);
  372. }
  373. }
  374. #endif
  375. template <typename CharT> inline bool hostname(std::basic_string_view<CharT> name) {
  376. using delim = detail::char_delimiters<CharT>;
  377. if (name.find_first_of(delim::illegal_hostname_chars) != name.npos) {
  378. return false;
  379. }
  380. if (detail::to_u8(name).size() > (name.back() == '.' ? 254 : 253)) {
  381. return false;
  382. }
  383. if (not std::ranges::all_of(delim::special_cases,
  384. [name](auto & sc) { return sc.accepts(name); })) {
  385. return false;
  386. }
  387. for (size_t n = name.find('.'); n != std::string::npos;
  388. name.remove_prefix(n + 1), n = name.find('.')) {
  389. if (not hostname_part(name.substr(0, n))) {
  390. return false;
  391. }
  392. }
  393. return name.empty() || hostname_part(name);
  394. }
  395. inline bool ipv4(std::string_view ip) {
  396. unsigned int ip0, ip1, ip2, ip3;
  397. char eof;
  398. if (ip.find_first_not_of("0123456789.") != std::string::npos) {
  399. return false;
  400. }
  401. if (ip[0] == '0' && std::isdigit(ip[1])) {
  402. return false;
  403. }
  404. if (size_t n = ip.find(".0"); n != std::string::npos && std::isdigit(ip[n + 2])) {
  405. return false;
  406. }
  407. if (sscanf(std::string(ip).c_str(), "%3u.%3u.%3u.%3u%c", &ip0, &ip1, &ip2, &ip3, &eof) != 4) {
  408. return false;
  409. }
  410. return ip0 <= 0xFF && ip1 <= 0xFF && ip2 <= 0xFF && ip3 <= 0xFF;
  411. }
  412. inline bool ipv6(std::string_view ip) {
  413. int expected_spans = 8;
  414. if (size_t n = ip.find('.'); n != std::string::npos) {
  415. if (not ipv4(ip.substr(ip.find_last_of(':') + 1))) {
  416. return false;
  417. }
  418. // This is a cheat to allow e.g. ::127.0.0.1 to validate
  419. expected_spans = 7;
  420. ip = ip.substr(0, n);
  421. }
  422. if (ip.find_first_not_of("0123456789ABCDEFabcdef:") != std::string::npos) {
  423. return false;
  424. }
  425. if (ip.size() >= 40) {
  426. return false;
  427. }
  428. bool has_compressed = false;
  429. int groups = 0;
  430. if (ip.starts_with("::")) {
  431. has_compressed = true;
  432. ip.remove_prefix(2);
  433. }
  434. while (!ip.empty()) {
  435. int data;
  436. if (sscanf(ip.data(), "%4x", &data) != 1) {
  437. return false;
  438. }
  439. if (size_t n = ip.find(':'); std::min(n, ip.size()) > 4) {
  440. return false;
  441. } else if (n != std::string::npos) {
  442. ip.remove_prefix(n + 1);
  443. } else {
  444. ip = "";
  445. }
  446. ++groups;
  447. if (ip[0] == ':') {
  448. if (std::exchange(has_compressed, true)) {
  449. return false;
  450. }
  451. ip.remove_prefix(1);
  452. }
  453. }
  454. return groups == expected_spans || (has_compressed && groups < expected_spans);
  455. }
  456. // Let's be honest - no matter what RFC 5321 §4.1.2 or RFC 6531 say, the only
  457. // way to know if an email address is valid is to try and send a message to it.
  458. // Therefore, there's no point in trying to validate things according to a
  459. // complex grammar - as long as it has an '@' sign with at least one character
  460. // on each side, we ought to call it an email.
  461. template <typename CharT> inline bool email(std::basic_string_view<CharT> em) {
  462. using delim = detail::char_delimiters<CharT>;
  463. size_t n = em.find_last_of('@');
  464. if (n == 0 || n >= em.size() - 1) {
  465. return false;
  466. }
  467. auto const who = em.substr(0, n);
  468. if (who.starts_with('"') && who.ends_with('"')) {
  469. // No validation
  470. } else if (who.starts_with('.') || who.ends_with('.')) {
  471. return false;
  472. } else if (em.substr(0, n).find(delim::dotdot) != em.npos) {
  473. return false;
  474. }
  475. auto domain = em.substr(n + 1);
  476. if (not(domain.starts_with('[') && domain.ends_with(']'))) {
  477. return hostname(domain);
  478. }
  479. domain.remove_prefix(1);
  480. domain.remove_suffix(1);
  481. if (auto ip = detail::to_u8(domain); ip.starts_with("IPv6:")) {
  482. return ipv6(ip.substr(5));
  483. } else {
  484. return ipv4(ip);
  485. }
  486. }
  487. template <typename T> inline bool ctor_as_valid(std::string_view str) {
  488. try {
  489. [[maybe_unused]] auto _ = T(str);
  490. return true;
  491. } catch (std::exception const &) { return false; }
  492. }
  493. template <auto Predicate> bool utf32(std::string_view str) {
  494. return Predicate(detail::to_u32(str));
  495. }
  496. }
  497. namespace jvalidate {
  498. class FormatValidator {
  499. public:
  500. using Predicate = bool (*)(std::string_view);
  501. enum class Status { Unknown, Unimplemented, Valid, Invalid };
  502. private:
  503. std::unordered_map<std::string, Predicate> supported_formats_{
  504. {"date", &format::date},
  505. {"date-time", &format::date_time},
  506. {"duration", &format::duration},
  507. {"email", &format::email},
  508. {"hostname", &format::hostname},
  509. {"idn-email", UTF32(email)},
  510. {"idn-hostname", UTF32(hostname)},
  511. {"ipv4", &format::ipv4},
  512. {"ipv6", &format::ipv6},
  513. {"iri", UTF32(uri)},
  514. {"iri-reference", UTF32(uri_reference)},
  515. {"json-pointer", CONSTRUCTS(Pointer)},
  516. {"relative-json-pointer", CONSTRUCTS(RelativePointer)},
  517. {"regex", nullptr},
  518. {"time", &format::time},
  519. {"uri", &format::uri},
  520. {"uri-reference", &format::uri_reference},
  521. {"uri-template", &format::utf32<format::uri_template>},
  522. {"uuid", &format::uuid},
  523. };
  524. public:
  525. FormatValidator() = default;
  526. FormatValidator(Predicate is_regex) { supported_formats_.insert_or_assign("regex", is_regex); }
  527. Status operator()(std::string const & format, std::string_view text) const {
  528. if (auto it = supported_formats_.find(format); it != supported_formats_.end() && it->second) {
  529. if (not it->second) {
  530. return Status::Unimplemented;
  531. }
  532. return it->second(text) ? Status::Valid : Status::Invalid;
  533. }
  534. return Status::Unknown;
  535. }
  536. };
  537. }
  538. #undef CONSTRUCTS
  539. #undef UTF32