Feature: Add uuid format-string support
This commit is contained in:
parent
efb0a3127b
commit
4ebc6c2334
@ -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 schema draft formats.
|
||||
|
||||
# Contributing
|
||||
|
||||
Before opening a pull request, please apply the coding style given in the
|
||||
|
||||
@ -175,6 +175,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]))*$)"};
|
||||
|
||||
@ -304,6 +306,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);
|
||||
|
||||
@ -0,0 +1,85 @@
|
||||
[
|
||||
{
|
||||
"description": "uuid format",
|
||||
"schema": {
|
||||
"format": "uuid"
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "all upper-case",
|
||||
"data": "2EB8AA08-AA98-11EA-B4AA-73B441D16380",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "all lower-case",
|
||||
"data": "2eb8aa08-aa98-11ea-b4aa-73b441d16380",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "mixed case",
|
||||
"data": "2eb8aa08-AA98-11ea-B4Aa-73B441D16380",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "all zeroes is valid",
|
||||
"data": "00000000-0000-0000-0000-000000000000",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "wrong length",
|
||||
"data": "2eb8aa08-aa98-11ea-b4aa-73b441d1638",
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "missing section",
|
||||
"data": "2eb8aa08-aa98-11ea-73b441d16380",
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "bad characters (not hex)",
|
||||
"data": "2eb8aa08-aa98-11ea-b4ga-73b441d16380",
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "no dashes",
|
||||
"data": "2eb8aa08aa9811eab4aa73b441d16380",
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "too few dashes",
|
||||
"data": "2eb8aa08aa98-11ea-b4aa73b441d16380",
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "too many dashes",
|
||||
"data": "2eb8-aa08-aa98-11ea-b4aa73b44-1d16380",
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "dashes in the wrong spot",
|
||||
"data": "2eb8aa08aa9811eab4aa73b441d16380----",
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"description": "valid version 4",
|
||||
"data": "98d80576-482e-427f-8434-7f86890ab222",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "valid version 5",
|
||||
"data": "99c17cbb-656f-564a-940f-1a4568f03487",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "hypothetical version 6",
|
||||
"data": "99c17cbb-656f-664a-940f-1a4568f03487",
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"description": "hypothetical version 15",
|
||||
"data": "99c17cbb-656f-f64a-940f-1a4568f03487",
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
Loading…
Reference in New Issue
Block a user