diff --git a/README.md b/README.md index 84631d6..591add3 100644 --- a/README.md +++ b/README.md @@ -332,6 +332,8 @@ json_validator validator(nullptr, // or loader-callback my_format_checker); // create validator ``` +## Default Checker + The library contains a default-checker, which does some checks. It needs to be provided manually to the constructor of the validator: @@ -340,6 +342,10 @@ json_validator validator(loader, // or nullptr for no loader nlohmann::json_schema::default_string_format_check); ``` +Supported formats: `date-time, date, time, email, hostname, ipv4, ipv6, uuid, regex` + +More formats can be added in `src/string-format-check.cpp`. Please contribute implementions for missing json scheme draft format. + # Contributing Before opening a pull request, please apply the coding style given in the diff --git a/src/string-format-check.cpp b/src/string-format-check.cpp index 28a818c..7261f2f 100644 --- a/src/string-format-check.cpp +++ b/src/string-format-check.cpp @@ -166,6 +166,8 @@ const std::string host{ "|" + regName + ")"}; +const std::string uuid{R"([0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12})"}; + // from http://stackoverflow.com/questions/106179/regular-expression-to-match-dns-hostname-or-ip-address const std::string hostname{R"(^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$)"}; @@ -295,6 +297,11 @@ void default_string_format_check(const std::string &format, const std::string &v if (!std::regex_match(value, ipv6Regex)) { throw std::invalid_argument(value + " is not an IPv6 string according to RFC 5954."); } + } else if (format == "uuid") { + static const std::regex uuidRegex{uuid}; + if (!std::regex_match(value, uuidRegex)) { + throw std::invalid_argument(value + " is not an uuid string according to RFC 4122."); + } } else if (format == "regex") { try { std::regex re(value, std::regex::ECMAScript);