Skip to content

Commit

Permalink
refactor(validation): cleanup DatasetFieldValueValidator URL validation
Browse files Browse the repository at this point in the history
  • Loading branch information
poikilotherm committed Mar 24, 2022
1 parent 85dcbbf commit 1c53f7f
Showing 1 changed file with 4 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import javax.validation.ConstraintValidatorContext;

import edu.harvard.iq.dataverse.validation.EMailValidator;
import edu.harvard.iq.dataverse.validation.URLValidator;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.validator.routines.UrlValidator;

Expand Down Expand Up @@ -167,20 +168,11 @@ public boolean isValid(DatasetFieldValue value, ConstraintValidatorContext conte
// Note, length validation for FieldType.TEXT was removed to accommodate migrated data that is greater than 255 chars.

if (fieldType.equals(FieldType.URL) && !lengthOnly) {

String[] schemes = {"http","https", "ftp"};
UrlValidator urlValidator = new UrlValidator(schemes);

try {
if (urlValidator.isValid(value.getValue())) {
} else {
context.buildConstraintViolationWithTemplate(dsfType.getDisplayName() + " " + value.getValue() + " is not a valid URL.").addConstraintViolation();
return false;
}
} catch (NullPointerException npe) {
boolean isValidUrl = URLValidator.isURLValid(value.getValue());
if (!isValidUrl) {
context.buildConstraintViolationWithTemplate(dsfType.getDisplayName() + " " + value.getValue() + " {url.invalid}").addConstraintViolation();
return false;
}

}

if (fieldType.equals(FieldType.EMAIL) && !lengthOnly) {
Expand Down

0 comments on commit 1c53f7f

Please sign in to comment.