Skip to content

Commit

Permalink
Add support for "duration" string format.
Browse files Browse the repository at this point in the history
Fix #116
  • Loading branch information
lcharlois-neotys committed Oct 6, 2023
1 parent d9a9190 commit a964b30
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class RegularExpressions {
public static final Pattern URI = Pattern.compile("^[a-zA-Z][a-zA-Z0-9+-.]*:[^\\s]*$");
public static final Pattern DATE = Pattern.compile("^\\d{4}-(?:0[0-9]|1[0-2])-[0-9]{2}$");
public static final Pattern DATETIME = Pattern.compile("^([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\\.[0-9]+)?(([Zz])|([\\+|\\-]([01][0-9]|2[0-3]):[0-5][0-9]))$");

public static final Pattern DURATION = Pattern.compile("^(-)?P(?:(?:(-?[0-9,.]*)Y)?(?:(-?[0-9,.]*)M)?(?:(-?[0-9,.]*)D)?(?:T(?:(-?[0-9,.]*)H)?(?:(-?[0-9,.]*)M)?(?:(-?[0-9,.]*)S)?)?|(-?[0-9,.]*)W)$");
public static final Pattern TIME = Pattern.compile("^(?:[0-2]\\d:[0-5]\\d:[0-5]\\d|23:59:60)(?:\\.\\d+)?(?:[zZ]|[+-]\\d\\d:\\d\\d)?$");
public static final Pattern BASE64 = Pattern.compile("^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$");
public static final Pattern IPV4 = Pattern.compile("^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}" + "" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public Map<String, Predicate<String>> initFormatsMap() {
predicates.put("byte", createPredicateFromPattern(RegularExpressions.BASE64));
predicates.put("date", createPredicateFromPattern(RegularExpressions.DATE));
predicates.put("date-time", createPredicateFromPattern(RegularExpressions.DATETIME));
predicates.put("duration", createPredicateFromPattern(RegularExpressions.DURATION));
predicates.put("ipv4", createPredicateFromPattern(RegularExpressions.IPV4));
predicates.put("ipv6", createPredicateFromPattern(RegularExpressions.IPV6));
predicates.put("hostname", createPredicateFromPattern(RegularExpressions.HOSTNAME));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public Map<String, Predicate<String>> initFormatsMap() {
predicates.put("byte", createPredicateFromPattern(RegularExpressions.BASE64));
predicates.put("date", createPredicateFromPattern(RegularExpressions.DATE));
predicates.put("date-time", createPredicateFromPattern(RegularExpressions.DATETIME));
predicates.put("duration", createPredicateFromPattern(RegularExpressions.DURATION));
predicates.put("ipv4", createPredicateFromPattern(RegularExpressions.IPV4));
predicates.put("ipv6", createPredicateFromPattern(RegularExpressions.IPV6));
predicates.put("hostname", createPredicateFromPattern(RegularExpressions.HOSTNAME));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.vertx.json.schema.common;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.*;

/**
* @author lcharlois
* @since 06/10/2023.
*/
class RegularExpressionsTest {

@ParameterizedTest
@ValueSource(strings = {"P3Y6M4DT12H30M5S","P3DT12H","P1M","PT1M","PT0S","P0.5Y","PT1M3.025S"}) // six numbers
void shouldMatchValidDuration(String duration) {
assertTrue(RegularExpressions.DURATION.matcher(duration).matches());
}


}

0 comments on commit a964b30

Please sign in to comment.