SpineEventEngine / validation

Apache License 2.0
3 stars 0 forks source link

Just return `Optional.empty()` when there is nothing to validate #48

Open alexander-yevsyukov opened 2 years ago

alexander-yevsyukov commented 2 years ago

I have the following type (it's a part of test data in core-java project):

message MixinCreateProject {
    int32 project_id = 1;
    string name = 2;
}

It's a command (because it resides in the mixin_commands.proto file). As such it has its first field required, but since it's of int32 type it would always have some value. So, we have nothing to validate about this type.

The code of validation generated for this type is this:

public java.util.Optional<io.spine.validate.ValidationError> validate() {
  java.util.ArrayList<io.spine.validate.ConstraintViolation> violations = new java.util.ArrayList<>();
  // INSERT:'extra_validation:type.spine.io/spine.test.mixin.command.MixinCreateProject'
  if (!violations.isEmpty()) {
    return java.util.Optional.of(io.spine.validate.ValidationError.newBuilder().addAllConstraintViolation(violations).build());
      } else {
    return java.util.Optional.empty();
   }
}

It does nothing useful because the array created would be always empty, and there's no code added in the insertion point.

It would be good to have just this instead:

public java.util.Optional<io.spine.validate.ValidationError> validate() {
    return java.util.Optional.empty();
}