I'm doing an application in Typescript using DDD and hexagonal architecture and I'd like to know in which cases I should return false in validations and in which other ones I should throw an exception.
As far as I have understood, an exception is thrown when you don't have that error handled, you don't expect that thing to happen, but then I see better programmers code using DDD as well and they are using Not Found exceptions, which obviously you should have that handled.
In my example, I want to do a validation in a valueObject that looks like this:
public static nameValidation(name: string): boolean {
if (Name.containsSpecialChars(name)) {
return false;
}
if (name.length < 2) {
return false;
}
return true;
}
I have put this as a boolean that return false in case that the validation is not okay, but could easily change this into an exception.
I don't really know how to distinguish which has to go where.
Any thoughts?