Would this Validation System work?

I have this validation system I wanted to implement for absolutely no reason what so ever other than waste my time but essentially it just validates objects that are going to be created. Say you are creating an class called Tenant and this tenant is using the builder pattern. before the Tenant object is created it would validate it which would look something like this:
// TODO Check this Method for Validity (Refactor if Necessary)
private void isValidTenantBuild() {
Validator<TenantBuilder> validator = Validator.of(this);

// FIXME Add More Rules for Tenant If Necessary
var tenantValid = validator
.addRule(t -> t.id != null, "Tenant ID is Required.")
.addRule(t -> t.name != null, "Tenant Name is Required.")
.addRule(t -> t.email != null, "Tenant Email is Required.")
.addRule(t -> t.phone != null, "Tenant Phone is Required.")
.addRule(t -> t.address != null, "Tenant Address is Required.")
.addRule(t -> t.lease != null, "Tenant Lease is Required.")
.getErrors();
}
// TODO Check this Method for Validity (Refactor if Necessary)
private void isValidTenantBuild() {
Validator<TenantBuilder> validator = Validator.of(this);

// FIXME Add More Rules for Tenant If Necessary
var tenantValid = validator
.addRule(t -> t.id != null, "Tenant ID is Required.")
.addRule(t -> t.name != null, "Tenant Name is Required.")
.addRule(t -> t.email != null, "Tenant Email is Required.")
.addRule(t -> t.phone != null, "Tenant Phone is Required.")
.addRule(t -> t.address != null, "Tenant Address is Required.")
.addRule(t -> t.lease != null, "Tenant Lease is Required.")
.getErrors();
}
for the cove above its just a simple example of what you can do but it should work with different datatypes (primitive or reference) with their own set of rules.
7 Replies
JavaBot
JavaBot7mo ago
This post has been reserved for your question.
Hey @verbose! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
verbose
verboseOP7mo ago
here is the validator class
verbose
verboseOP7mo ago
and the validate interface:
public interface Validate<T> {
Validator<T> addRule(Predicate<? super T> rule, String message);
<U> Validator<T> addRule(Function<? super T, ? extends U> mapper, Predicate<? super U> rule, String message);
Validator<T> addRule(Predicate<? super T> rule);
Validator<T> addRegexRule(String regex, String message);
<U> Validator<T> addRegexRule(Function<? super T, ? extends U> mapper, String regex, String message);
}
public interface Validate<T> {
Validator<T> addRule(Predicate<? super T> rule, String message);
<U> Validator<T> addRule(Function<? super T, ? extends U> mapper, Predicate<? super U> rule, String message);
Validator<T> addRule(Predicate<? super T> rule);
Validator<T> addRegexRule(String regex, String message);
<U> Validator<T> addRegexRule(Function<? super T, ? extends U> mapper, String regex, String message);
}
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
verbose
verboseOP7mo ago
yeah you're right it would be pretty whack now that I look at it, but I can see this being much better actually this code was references from another repository on github but it had the same construct you are talking about with Rule interface ill try this thanks!
JavaBot
JavaBot7mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts. 💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
JavaBot
JavaBot7mo ago
Post Closed
This post has been closed by <@400463761011507212>.
Want results from more Discord servers?
Add your server