Skip to content
Merged
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 @@ -65,6 +65,11 @@ public ValidationError getMissingScaleDataTypeMsg() {
return new ValidationError("Scale Class", "Missing scale class", HttpStatus.UNPROCESSABLE_ENTITY);
}

@Override
public ValidationError getPeriodObsVarNameMsg() {
return new ValidationError("Name", "Period is invalid", HttpStatus.UNPROCESSABLE_ENTITY);
}

@Override
public ValidationError getMissingObsVarNameMsg() {
return new ValidationError("Name", "Missing name", HttpStatus.UNPROCESSABLE_ENTITY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public ValidationError getMissingScaleDataTypeMsg() {
}

@Override
public ValidationError getPeriodObsVarNameMsg() {
return new ValidationError("observationVariableName", "Period in name is invalid", HttpStatus.BAD_REQUEST);
}
Comment on lines +70 to +71
Copy link
Contributor

@dmeidlin dmeidlin Oct 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to keep the error messages consistent between TraitValidatortError and TraitFileValidatorError, so for example, this message could be changed to "Period is invalid".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a banner error message. Table messages have a field the tells the user which field contains the error, banner messages do not. So, it was decided that this message should contain the name of the filed.


public ValidationError getMissingObsVarNameMsg() {
return new ValidationError("observationVariableName", "Missing Name", HttpStatus.BAD_REQUEST);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public interface TraitValidatorErrorInterface {
ValidationError getMissingScaleUnitMsg();
ValidationError getMissingScaleDataTypeMsg();
ValidationError getMissingObsVarNameMsg();
ValidationError getPeriodObsVarNameMsg();
ValidationError getMissingTraitEntityMsg();
ValidationError getMissingTraitAttributeMsg();
ValidationError getMissingTraitDescriptionMsg();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.breedinginsight.services.validators;

import io.micronaut.http.HttpStatus;
import org.breedinginsight.api.model.v1.response.ValidationError;
import org.breedinginsight.api.model.v1.response.ValidationErrors;
import org.breedinginsight.dao.db.enums.DataType;
Expand All @@ -26,6 +27,8 @@

import javax.inject.Inject;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static org.apache.commons.lang3.StringUtils.isBlank;
Expand Down Expand Up @@ -207,6 +210,27 @@ public ValidationErrors checkTraitFieldsLength(List<Trait> traits, TraitValidato
return errors;
}

public ValidationErrors checkTraitFieldsFormat(List<Trait> traits, TraitValidatorErrorInterface traitValidatorErrors) {

ValidationErrors errors = new ValidationErrors();

for (int i = 0; i < traits.size(); i++) {

Trait trait = traits.get(i);
String name = trait.getObservationVariableName();

Pattern pattern = Pattern.compile("\\.");
Matcher matcher = pattern.matcher(name);
boolean containsInvalidCharacter = matcher.find();

if (name != null && containsInvalidCharacter){
ValidationError error = traitValidatorErrors.getPeriodObsVarNameMsg();
errors.addError(traitValidatorErrors.getRowNumber(i), error);
}
}
return errors;
}

public List<Trait> checkDuplicateTraitsExistingByName(UUID programId, List<Trait> traits){

List<Trait> duplicates = new ArrayList<>();
Expand Down Expand Up @@ -273,7 +297,8 @@ public Optional<ValidationErrors> checkAllTraitValidations(List<Trait> traits, T
ValidationErrors dataConsistencyErrors = checkTraitDataConsistency(traits, traitValidatorError);
ValidationErrors duplicateTraitsInFile = checkDuplicateTraitsInFile(traits, traitValidatorError);
ValidationErrors fieldLengthError = checkTraitFieldsLength(traits, traitValidatorError);
validationErrors.mergeAll(requiredFieldErrors, dataConsistencyErrors, duplicateTraitsInFile, fieldLengthError);
ValidationErrors fieldFormatErrors = checkTraitFieldsFormat(traits, traitValidatorError);
validationErrors.mergeAll(requiredFieldErrors, dataConsistencyErrors, duplicateTraitsInFile, fieldLengthError, fieldFormatErrors);

if (validationErrors.hasErrors()){
return Optional.of(validationErrors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,5 +348,28 @@ public void charLimitExceeded() {
}
}

@Test
@SneakyThrows
public void periodInName() {

Trait trait = new Trait();
trait.setObservationVariableName("Period.1");

ValidationErrors validationErrors = traitValidatorService.checkTraitFieldsFormat(List.of(trait), new TraitValidatorError());

assertEquals(1, validationErrors.getRowErrors().size(), "Wrong number of row errors returned");
RowValidationErrors rowValidationErrors = validationErrors.getRowErrors().get(0);
assertEquals(1, rowValidationErrors.getErrors().size(), "Wrong number of errors for row");
assertEquals(400, rowValidationErrors.getErrors().get(0).getHttpStatusCode(), "Wrong error code");
assertEquals("observationVariableName", rowValidationErrors.getErrors().get(0).getField(), "Wrong error column");

//There should be no errors
Trait noPeriodTrait = new Trait();
noPeriodTrait.setObservationVariableName("NoPeriod");
validationErrors = traitValidatorService.checkTraitFieldsFormat(List.of(noPeriodTrait), new TraitValidatorError());
assertEquals(0, validationErrors.getRowErrors().size(), "Wrong number of row errors returned");
}



}