Skip to content

Commit

Permalink
added a regex validator for FreeTextFieldType
Browse files Browse the repository at this point in the history
  • Loading branch information
andre.cruz committed May 24, 2022
1 parent 621b2dc commit 41f2dcb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -36,18 +37,46 @@ public class FreeTextFieldType implements ModelParameterType {
*/
private final String defaultValue;

/**
* A regular expression defining a valid input.
*/
private final Pattern 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 != null ? Pattern.compile(validRegex) : null;
}

@Override
public Optional<ParamValidationError> validate(final String parameterName, final String parameterValue) {
return Optional.empty();
if (this.validRegex == null) {
return Optional.empty();
}

if (this.validRegex.matcher(parameterValue).matches()) {
return Optional.empty();
} else {
return Optional.of(new ParamValidationError(
parameterName,
parameterValue,
"Should match the following regex: " + this.validRegex
));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
public class FreeTextFieldTypeTest extends AbstractConfigFieldTypeTest<FreeTextFieldType> {

/**
* 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() {
Expand All @@ -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.
*/
Expand Down

0 comments on commit 41f2dcb

Please sign in to comment.