
com.github.vickumar1981.svalidate.util.example.model.Person Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of svalidate_2.11 Show documentation
Show all versions of svalidate_2.11 Show documentation
Simple and easy validations for Scala and Java
The newest version!
package com.github.vickumar1981.svalidate.util.example.model;
import com.github.vickumar1981.svalidate.util.Validation;
import java.util.Optional;
import static com.github.vickumar1981.svalidate.util.ValidationSyntax.orElse;
import static com.github.vickumar1981.svalidate.util.ValidationSyntax.errorIfEmpty;
import static com.github.vickumar1981.svalidate.util.ValidationSyntax.errorIfDefined;
import static com.github.vickumar1981.svalidate.util.ValidationSyntax.maybeValidate;
public class Person implements Validatable {
private String firstName;
private String lastName;
private Boolean hasContactInfo;
private Optional address;
private Optional phone;
public Person(String firstName,
String lastName,
Boolean hasContactInfo,
Optional address,
Optional phone) {
this.firstName = firstName;
this.lastName = lastName;
this.hasContactInfo = hasContactInfo;
this.address = address;
this.phone = phone;
}
private Validation validateContactInfo() {
return Validation.of(
errorIfEmpty("Address is required").apply(address),
errorIfEmpty("Phone # is required").apply(phone),
maybeValidate(address),
maybeValidate(phone.map(p -> p.matches("\\d{10}")),
orElse("Phone # must be 10 digits"))
);
}
@Override
public Validation validate() {
return Validation.of(
orElse("First name is required").apply(
firstName != null && !firstName.isEmpty()) ,
orElse("Last name is required").apply(
lastName != null && !lastName.isEmpty())
)
.andThen(hasContactInfo, this::validateContactInfo)
.orElse(hasContactInfo, () ->
errorIfDefined("Address must be empty").apply(address)
.append(errorIfDefined("Phone # must be empty").apply(phone))
);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy