Given the following example:
class Person {
Integer age
String lastName
String firstName
}
The property age should be constrained to specific validation rules:
- Higher than 0
Same for the lastName and firstName:
- These strings should not contain special characters (e.g. numbers, underscore, ...)
- Length should be > 0
In order to abstract this validation policy, should I create value objects such as age and name in order to encapsulate the validation:
class Age {
Integer value
}
class Name {
String value
}
class Person {
Name lastName
Name firstName
Age age
}
Indeed I can also keep the code DRY and re-use my value objects but it seems like an "over-abstraction"