Skip to content

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

@alexander-yevsyukov

Description

@alexander-yevsyukov

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();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions