I am in a confusion where I cannot decide whether a rule is validation rule or Business rule. I have simple POJO with JSR 303 bean validation rules.
public class VehicleDto {
@NotNull(message = "Vehicle ID is not given.")
@Size(min = 3, max = 20, message = "Vehicle ID length is out of range of 3 to 20.")
@Pattern(regexp = "^([0-9]+[a-zA-Z]+|[a-zA-Z]+[0-9]+)[0-9a-zA-Z]*$", message = "Vehicle ID is not alphanumeric.")
private String id;
@NotNull(message = "Vehicle name is not given.")
@Size(min = 3, max = 45, message = "Vehicle name length is out of range of 3 to 45.")
private String name;
@Min(value = 1, message = "Organization ID is not given.")
private long organizationId;
private Long deviceId;
}
I believe validation rule as data validation. For example: In above pojo, I have a vehicle id which is always supposed to be alphanumeric of length at least 3 to 20 if vehicle id exists. But it should exist to register a vehicle is our business policy right which happens to be a business rule. Also while registering a vehicle, the need of foreign key (organizationId and deviceId) checks for their entity existences for registration also happens to be business rule.
These are the thoughts that are confusing me. Am I right on those thoughts guys?
While “vehicleId must exist” may be also a (quite non-specific) business rule, in this case it’s mainly a validation rule. Unless there are cases where you would create and assign a new id for a vehicle, the business rule is implied (you need to have an id for the entity if you intend to work with it).
Would you consider “user must exist if you want to change its password” a business rule? I think not.
If other parts of your system were able to create and assign vehicleIds, the business rule would be “vehicles must have vehicleId, unless they’re being created”, which would result in two validation rules in separate parts of the system.
3