Skip to content

Instantly share code, notes, and snippets.

@thetekst
Created September 21, 2018 15:05
Show Gist options
  • Save thetekst/8b7c93cb985307e310656fb7451a2bc5 to your computer and use it in GitHub Desktop.
Save thetekst/8b7c93cb985307e310656fb7451a2bc5 to your computer and use it in GitHub Desktop.
Validate emails and each email length apart
@Data
@ToString(exclude = {"password"})
public class MyRequest {
@NotEmpty
@Size(min = 1, max = 64)
private String password;
private Set<String> emails;
private String phone;
@AssertTrue(message="passVerify field should be equal than pass field")
private boolean isValid() {
return !CollectionUtils.isEmpty(emails) && emails.stream()
.filter(f -> f.length() > 10)
.findFirst()
.orElse("")
.equals("");
}
}
@Slf4j
@RequiredArgsConstructor
@Service
public class MyService {
public void process(final MyRequest request) {
log.info("request: {}", request);
if(CollectionUtils.isEmpty(request.getEmails()) || StringUtils.isNotBlank(request.getEmails().stream()
.filter(f -> f.length() > EMAIL_MAX_LENGTH)
.findFirst()
.orElse(""))) {
throw new ServiceException(Errors.INTERNAL_ERROR);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment