Add support for v4 UUIDs, and allow UUIDs to be case insensitive (#1688)

This commit is contained in:
Jamie Tanna
2021-08-06 17:54:23 +01:00
committed by GitHub
parent 930eea8d3c
commit 13054ddb29
2 changed files with 31 additions and 3 deletions

View File

@@ -66,7 +66,10 @@ public final class RegexPatterns {
protected static final Pattern HTTPS_URL = UrlHelper.HTTPS_URL;
protected static final Pattern UUID = Pattern
.compile("[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}");
.compile("[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}", Pattern.CASE_INSENSITIVE);
protected static final Pattern UUID4 = Pattern
.compile("[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}", Pattern.CASE_INSENSITIVE);
protected static final Pattern ANY_DATE = Pattern
.compile("(\\d\\d\\d\\d)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])");
@@ -164,6 +167,10 @@ public final class RegexPatterns {
return new RegexProperty(UUID).asString();
}
public static RegexProperty uuid4() {
return new RegexProperty(UUID4).asString();
}
public static RegexProperty isoDate() {
return new RegexProperty(ANY_DATE).asString();
}

View File

@@ -237,13 +237,34 @@ class RegexPatternsSpec extends Specification {
def "should generate a regex for a uuid [#textToMatch] that is a match [#shouldMatch]"() {
expect:
shouldMatch == RegexPatterns.uuid().matcher(textToMatch).matches()
shouldMatch == RegexPatterns.uuid().matcher(textToMatch).matches()
where:
textToMatch || shouldMatch
UUID.randomUUID().toString() || true
UUID.randomUUID().toString() || true
UUID.randomUUID().toString().toUpperCase() || true
UUID.randomUUID().toString() + "!" || false
'23e4567-z89b-12z3-j456-426655440000' || false
'dog' || false
'5' || false
}
def "should generate a regex for a uuidv4 [#textToMatch] that is a match [#shouldMatch]"() {
expect:
shouldMatch == RegexPatterns.uuid4().matcher(textToMatch).matches()
where:
textToMatch || shouldMatch
UUID.randomUUID().toString() || true
UUID.randomUUID().toString() || true
UUID.randomUUID().toString().toUpperCase() || true
'123e4567-e89b-42d3-a456-556642440000' || true
'00000000-0000-4000-8000-000000000000' || true
'00000000-0000-4000-9000-000000000000' || true
'00000000-0000-4000-a000-000000000000' || true
'00000000-0000-4000-b000-000000000000' || true
'00000000-0000-4000-1000-000000000000' || false
UUID.randomUUID().toString() + "!" || false
'23e4567-z89b-12z3-j456-426655440000' || false
'00000000-0000-0000-0000-000000000000' || false
'dog' || false
'5' || false
}