Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a regex validator for FreeTextFieldType #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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