From 7bcfc9b3d0b57db0e759255925002514e6e4062d Mon Sep 17 00:00:00 2001 From: "andre.cruz" Date: Tue, 24 May 2022 14:58:38 +0100 Subject: [PATCH] added a regex validator for FreeTextFieldType --- .../fieldtype/FreeTextFieldType.java | 29 ++++++++++++++++++- .../fieldtype/FreeTextFieldTypeTest.java | 24 ++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/openml-api/src/main/java/com/feedzai/openml/provider/descriptor/fieldtype/FreeTextFieldType.java b/openml-api/src/main/java/com/feedzai/openml/provider/descriptor/fieldtype/FreeTextFieldType.java index ecc3fc1..df55430 100644 --- a/openml-api/src/main/java/com/feedzai/openml/provider/descriptor/fieldtype/FreeTextFieldType.java +++ b/openml-api/src/main/java/com/feedzai/openml/provider/descriptor/fieldtype/FreeTextFieldType.java @@ -22,6 +22,7 @@ import java.util.Objects; import java.util.Optional; +import java.util.regex.Pattern; /** * A concrete implementation of a {@link ModelParameterType} for parameters whose values are textual. @@ -36,18 +37,44 @@ public class FreeTextFieldType implements ModelParameterType { */ private final String defaultValue; + /** + * A regular expression defining a valid input. + */ + private final String validRegex; + /** * Creates a new instance of this class. * * @param defaultValue The default value. */ public FreeTextFieldType(final String defaultValue) { + this(defaultValue, null); + } + + /** + * Creates a new instance of this class. + * + * @param defaultValue The default value. + * @param validRegex A regex that matches valid inputs. + */ + public FreeTextFieldType(final String defaultValue, final String validRegex) { this.defaultValue = Preconditions.checkNotNull(defaultValue, "defaultValue can't be null."); + this.validRegex = validRegex; } @Override public Optional validate(final String parameterName, final String parameterValue) { - return Optional.empty(); + if (this.validRegex == null) + return Optional.empty(); + + boolean inputIsValid = Pattern.matches(this.validRegex, parameterValue); + if (inputIsValid) { + return Optional.empty(); + } else { + String reason = "Should match the following regex: " + this.validRegex; + + return Optional.of(new ParamValidationError(parameterName, parameterValue, reason)); + } } /** diff --git a/openml-api/src/test/java/com/feedzai/openml/provider/fieldtype/FreeTextFieldTypeTest.java b/openml-api/src/test/java/com/feedzai/openml/provider/fieldtype/FreeTextFieldTypeTest.java index 1ecaa20..fda3025 100644 --- a/openml-api/src/test/java/com/feedzai/openml/provider/fieldtype/FreeTextFieldTypeTest.java +++ b/openml-api/src/test/java/com/feedzai/openml/provider/fieldtype/FreeTextFieldTypeTest.java @@ -31,7 +31,7 @@ public class FreeTextFieldTypeTest extends AbstractConfigFieldTypeTest { /** - * Tests the {@link FreeTextFieldType#validate(String, String)} method. + * Tests the {@link FreeTextFieldType#validate(String, String)} method with no regex validator string. */ @Test public void validate() { @@ -43,6 +43,28 @@ public void validate() { assertValidationResult(fieldType, "param2", "some string", false); } + /** + * Tests the {@link FreeTextFieldType#validate(String, String)} method using the regex validator constructor. + */ + @Test + public void validateRegex() { + final FreeTextFieldType fieldType = new FreeTextFieldType("1", "^((\\d+(\\.\\d*)?,)*(\\d+(\\.\\d*)?))$"); + + // Test valid inputs + assertValidationResult(fieldType, "param0", "1", false); + assertValidationResult(fieldType, "param0", "1.", false); + assertValidationResult(fieldType, "param1", "1.2", false); + assertValidationResult(fieldType, "param0", "1.,2", false); + assertValidationResult(fieldType, "param2", "1.2,3,4.5", false); + assertValidationResult(fieldType, "param3", "1,2,3.4", false); + + // Test invalid inputs + assertValidationResult(fieldType, "param4", "", true); + assertValidationResult(fieldType, "param5", "1,", true); + assertValidationResult(fieldType, "param6", ",1", true); + assertValidationResult(fieldType, "param6", "1.2,3.,", true); + } + /** * Checks the default value method. */