Skip to content

Commit

Permalink
[#82] Refactor default messages of generic entity validation errors.
Browse files Browse the repository at this point in the history
- Generate entity type specific bad request response messages - response message is a concatenation of entity type name and validation erro default message.
  • Loading branch information
kostobog committed Sep 12, 2024
1 parent ea45c29 commit d954733
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public ErrorInfo handleValidationException(HttpServletRequest request, Validatio

String errorMessage = e.getErrors()
.stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage)
.map(err -> "%s %s".formatted(err.getObjectName(), err.getDefaultMessage()))
.collect(Collectors.joining(",", "[", "]"));

return new ErrorInfo(errorMessage, request.getRequestURI());
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/cz/cvut/kbss/analysis/dao/BaseDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,9 @@ public void removeAll(URI subjectURI, URI propertyURI, URI context){
.setParameter("context", context)
.executeUpdate();
}

@Override
public Class<T> getType() {
return type;
}
}
5 changes: 5 additions & 0 deletions src/main/java/cz/cvut/kbss/analysis/dao/GenericDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,9 @@ public interface GenericDao<T extends HasIdentifier> {
* @param context
*/
void deleteContext(URI context);

/**
* @return the type of the entity managed by this class.
*/
Class<T> getType();
}
2 changes: 1 addition & 1 deletion src/main/java/cz/cvut/kbss/analysis/model/NamedEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@Setter
public class NamedEntity extends AbstractEntity{

@NotEmpty(message = "Name must not be empty")
@NotEmpty(message = "name is empty.")
@ParticipationConstraints(nonEmpty = true)
@OWLDataProperty(iri = Vocabulary.s_p_name)
private String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ public boolean existsInPersistenceContext(URI id) {
* @throws ValidationException In case the instance is not valid
*/
public void validate(T instance, Object ... groups) {
DataBinder binder = new DataBinder(instance);
DataBinder binder = new DataBinder(instance, getPrimaryDao().getType().getSimpleName());

binder.setValidator(validator);
BindingResult bindingResult = binder.getBindingResult();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ protected void customValidation(Object target, Errors errors, Object... validati
NamedEntity entity = (NamedEntity) target;

if(existsWithName(entity))
errors.rejectValue("name", "name.duplicate", "Duplicate entity name");
errors.rejectValue("name", "name.duplicate", "uri is not null nor unique.");

if(groups.isCreateGroup() && exists(entity)){
errors.rejectValue("uri", "uri.exists", "The uri should be null or unique");
errors.rejectValue("uri", "uri.exists", "name already exists.");
}
if(groups.isUpdateGroup() && !exists(entity)){
errors.rejectValue("uri", "uri.not-exists", "Uri does not refer to an existing entity");
errors.rejectValue("uri", "uri.not-exists", "uri does not refer to an existing entity.");
}
}

Expand Down

0 comments on commit d954733

Please sign in to comment.