use of io.spine.validate.ConstraintViolation in project core-java by SpineEventEngine.
the class EventBus method validateMessage.
@Override
protected boolean validateMessage(Message event, StreamObserver<Response> responseObserver) {
final EventClass eventClass = EventClass.of(event);
if (isUnsupportedEvent(eventClass)) {
final UnsupportedEventException unsupportedEvent = new UnsupportedEventException(event);
responseObserver.onError(invalidArgumentWithCause(unsupportedEvent, unsupportedEvent.getError()));
return false;
}
final List<ConstraintViolation> violations = eventValidator.validate(event);
if (!violations.isEmpty()) {
final InvalidEventException invalidEvent = InvalidEventException.onConstraintViolations(event, violations);
responseObserver.onError(invalidArgumentWithCause(invalidEvent, invalidEvent.getError()));
return false;
}
return true;
}
use of io.spine.validate.ConstraintViolation in project core-java by SpineEventEngine.
the class MessageValidatorShould method provide_one_valid_violation_if_number_is_greater_than_max.
@Test
public void provide_one_valid_violation_if_number_is_greater_than_max() {
final MaxNumberFieldValue msg = MaxNumberFieldValue.newBuilder().setValue(GREATER_THAN_MAX).build();
validate(msg);
assertEquals(1, violations.size());
final ConstraintViolation violation = firstViolation();
assertEquals(GREATER_MAX_MSG, format(violation.getMsgFormat(), violation.getParam(0)));
assertFieldPathIs(violation, VALUE);
assertTrue(violation.getViolationList().isEmpty());
}
use of io.spine.validate.ConstraintViolation in project core-java by SpineEventEngine.
the class MessageValidatorShould method provide_valid_violations_if_enclosed_message_field_is_not_valid.
@Test
public void provide_valid_violations_if_enclosed_message_field_is_not_valid() {
final RequiredStringFieldValue enclosedMsg = RequiredStringFieldValue.getDefaultInstance();
final EnclosedMessageFieldValue msg = EnclosedMessageFieldValue.newBuilder().setOuterMsgField(enclosedMsg).build();
validate(msg);
assertEquals(1, violations.size());
final ConstraintViolation violation = firstViolation();
assertEquals("Message must have valid properties.", violation.getMsgFormat());
assertFieldPathIs(violation, OUTER_MSG_FIELD);
final List<ConstraintViolation> innerViolations = violation.getViolationList();
assertEquals(1, innerViolations.size());
final ConstraintViolation innerViolation = innerViolations.get(0);
assertEquals(NO_VALUE_MSG, innerViolation.getMsgFormat());
assertFieldPathIs(innerViolation, OUTER_MSG_FIELD, VALUE);
assertTrue(innerViolation.getViolationList().isEmpty());
}
use of io.spine.validate.ConstraintViolation in project core-java by SpineEventEngine.
the class MessageValidatorShould method assertIsValid.
private void assertIsValid(boolean isValid) {
if (isValid) {
assertTrue(violations.isEmpty());
} else {
assertFalse(violations.isEmpty());
for (ConstraintViolation violation : violations) {
final String format = violation.getMsgFormat();
assertTrue(!format.isEmpty());
final boolean noParams = violation.getParamList().isEmpty();
if (format.contains("%s")) {
assertFalse(noParams);
} else {
assertTrue(noParams);
}
assertFalse(violation.getFieldPath().getFieldNameList().isEmpty());
}
}
}
use of io.spine.validate.ConstraintViolation in project core-java by SpineEventEngine.
the class MessageValidatorShould method provide_one_valid_violation_if_string_does_not_match_to_regex_pattern.
@Test
public void provide_one_valid_violation_if_string_does_not_match_to_regex_pattern() {
final PatternStringFieldValue msg = PatternStringFieldValue.newBuilder().setEmail("invalid.email").build();
validate(msg);
assertEquals(1, violations.size());
final ConstraintViolation violation = firstViolation();
assertEquals("String must match the regular expression '%s'.", violation.getMsgFormat());
assertEquals("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$", firstViolation().getParam(0));
assertFieldPathIs(violation, "email");
assertTrue(violation.getViolationList().isEmpty());
}
Aggregations