Java 8 Lambda 表达式验证

我在此处阅读有关使用谓词进行验证的文章。我正在尝试在 Spring Boot 框架中实现它,但我遇到了一些问题。


在代码中:


public class LamdaPersonValidator implements PersonValidator {


   public void validate(Person person) {

     notNull.and(between(2, 12)).test(person.getFirstName()).throwIfInvalid("firstname");

     notNull.and(between(4, 30)).test(person.getLastName()).throwIfInvalid("secondname");

     notNull.and(between(3, 50)).and(contains("@")).test(person.getEmail()).throwIfInvalid("email");

     intBetween(0, 110).test(person.getAge()).throwIfInvalid("age");

   }


 }

没有提到检查验证方法中的人对象本身是否为空的标准方法。是否可以只进行空检查,if(persone != null) { // notNull.and..}或者可能有一些更好的方法来进行空检查。


另一件事是假设,我想做一些自定义检查,例如数据库中是否存在人员。在这种情况下,我需要连接到数据库进行检查。在这种情况下,我需要自动装配无法使用静态变量和方法的接口。


那么,从数据库进行验证时,使用它的最佳方法是什么?


三国纷争
浏览 175回答 2
2回答

鸿蒙传说

我们不是神圣裁判所的代码法官,所以我们没有责任告诉你,“只做一个空检查就可以”。当然,可以将 is 写成普通if语句,就像我们过去 25 年所做的那样,就像可以发明一个详细的框架来封装null检查并以某种方式将术语“lambda”带入其中。剩下的唯一问题是你是否真的打算写作if(person != null) { /* do the checks */ },换句话说,让一个null人通过考试。如果您想拒绝null人员(这会更合理),已经有可能在没有显式测试的情况下编写它Objects.requireNonNull,因为 Java 7,这表明您不需要“一切都变得更好”框架以实现该目标。一般来说,你可以用常规代码合理地编写验证代码,与文章示例相反,使用&&运算符等简单工具,将常用代码放入方法中:public void validate(Person person) {&nbsp; &nbsp; Objects.requireNonNull(person, "person is null");&nbsp; &nbsp; checkString(person.getFirstName(), "first name", 2, 12);&nbsp; &nbsp; checkString(person.getLastName(), "last name", 4, 30);&nbsp; &nbsp; checkString(person.getEmail(), "email", 3, 50);&nbsp; &nbsp; if(!person.getEmail().contains("@"))&nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException("invalid email format");&nbsp; &nbsp; checkBounds(person.getAge(), "age", 0, 110);}private void checkString(String nameValue, String nameType, int min, int max) {&nbsp; &nbsp; Objects.requireNonNull(nameValue, () -> nameType+" is null");&nbsp; &nbsp; checkBounds(nameValue.length(), nameType, min, max);}private void checkBounds(int value, String valueType, int min, int max) {&nbsp; &nbsp; if(value < min || value > max)&nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException(valueType+" is not within ["+min+" "+max+']');}这与您的代码相同,没有任何名称中带有“Lambda”的框架,仍然具有可读的验证代码并允许重用检查代码。也就是说LamdaPersonValidator,您应该使用反映类职责的类名,而不是反映您如何实现它的类名。显然,负责验证对象某些属性的验证器不应与检查数据库中实体存在的验证器混淆。后者本身就是一个完全不同的话题,也应该是一个单独的问题。上面的代码只是一个例子,如何实现与原始代码相同。它永远不应该以这种形式出现在生产代码中,因为它展示了一种广泛存在的反模式,将任意不合理的约束应用于属性,很可能是程序员在编写代码时发明的。为什么它假设一个人必须有名字和姓氏,为什么它假设名字必须至少有两个到十二个字符,而姓氏必须在四到三十个字符之间?它实际上甚至不是字符,因为char单位和实际字符之间的关联不是 1:1。对于每个考虑实现名称验证的程序员来说,必须阅读Falsehoods Programmers Being About Names (With Examples)。同样,维基百科经验证的最年长者名单列出了 100 名 110 岁以上的人。并且没有理由假设电子邮件地址不能超过五十个字符。一个正确的电子邮件模式的真正的验证可能会变成被什么东西故意省略......

慕的地10843

您也可以像这样编写 GenericValidator:编写AbstractValidator普通工作类:public abstract class AbstractValidator {&nbsp; &nbsp; private Map<Predicate, String> validatorMap = new LinkedHashMap<>();&nbsp; &nbsp; protected List<String> messages;&nbsp; &nbsp; public AbstractValidator() {&nbsp; &nbsp; &nbsp; &nbsp; this.messages = new ArrayList<>();&nbsp; &nbsp; }&nbsp; &nbsp; protected <E> AbstractValidator add(Predicate<E> predicate, String reason) {&nbsp; &nbsp; &nbsp; &nbsp; validatorMap.put(predicate, reason);&nbsp; &nbsp; &nbsp; &nbsp; return this;&nbsp; &nbsp; }&nbsp; &nbsp; protected AbstractValidator apply(String fieldName, Object val) {&nbsp; &nbsp; &nbsp; &nbsp; AtomicBoolean flag= new AtomicBoolean(true);&nbsp; &nbsp; &nbsp; &nbsp; this.validatorMap.forEach((modifier, reason) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (flag.get() && !modifier.test(val)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String message = MessageFormat.format("{0} {1}", fieldName, reason);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; messages.add(message);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag.set(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; this.validatorMap.clear();&nbsp; &nbsp; &nbsp; &nbsp; return this;&nbsp; &nbsp; }&nbsp; &nbsp; public void end(String exceptionStatus) {&nbsp; &nbsp; &nbsp; &nbsp; Optional.ofNullable(messages).filter(CollectionUtils::isEmpty)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .orElseThrow(() -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RuntimeException ex = new RuntimeException(exceptionStatus, messages);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; messages.clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ex;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}编写GenericValidator将扩展AbstractValidator验证实现的类:public class GenericValidator extends AbstractValidator {&nbsp; &nbsp; private GenericValidator() {&nbsp; &nbsp; &nbsp; &nbsp; super();&nbsp; &nbsp; }&nbsp; &nbsp; public static GenericValidator of() {&nbsp; &nbsp; &nbsp; &nbsp; return new GenericValidator();&nbsp; &nbsp; }&nbsp; &nbsp; public GenericValidator nonNull() {&nbsp; &nbsp; &nbsp; &nbsp; add(Objects::nonNull, "Field value is null");&nbsp; &nbsp; &nbsp; &nbsp; return this;&nbsp; &nbsp; }&nbsp; &nbsp; public GenericValidator notEmpty() {&nbsp; &nbsp; &nbsp; &nbsp; add(StringUtils::isNotEmpty, "Field is empty");&nbsp; &nbsp; &nbsp; &nbsp; return this;&nbsp; &nbsp; }&nbsp; &nbsp; public GenericValidator min(int min) {&nbsp; &nbsp; &nbsp; &nbsp; add(s -> ((String) s).length() >= min, "Field min size is " + min);&nbsp; &nbsp; &nbsp; &nbsp; return this;&nbsp; &nbsp; }&nbsp; &nbsp; public GenericValidator max(int max) {&nbsp; &nbsp; &nbsp; &nbsp; add(s -> ((String) s).length() <= max, "Field max size is " + max);&nbsp; &nbsp; &nbsp; &nbsp; return this;&nbsp; &nbsp; }&nbsp; &nbsp; public GenericValidator notEmptyList() {&nbsp; &nbsp; &nbsp; &nbsp; add(CollectionUtils::isNotEmpty, "Field List is null/Empty");&nbsp; &nbsp; &nbsp; &nbsp; return this;&nbsp; &nbsp; }&nbsp; &nbsp; public GenericValidator apply(String fieldName, Object val) {&nbsp; &nbsp; &nbsp; &nbsp; return (GenericValidator) super.apply(fieldName, val);&nbsp; &nbsp; }}请相应地进行测试。测试用例示例:class GenericValidatorTest {&nbsp; &nbsp; @Test&nbsp; &nbsp; void genericValidationSuccessCase() {&nbsp; &nbsp; &nbsp; &nbsp; Abc abc = new Abc();&nbsp; &nbsp; &nbsp; &nbsp; abc.setName("a");&nbsp; &nbsp; &nbsp; &nbsp; abc.setVal(1);&nbsp; &nbsp; &nbsp; &nbsp; abc.setAbslist(Collections.singletonList(new ChildAbc()));&nbsp; &nbsp; &nbsp; &nbsp; GenericValidator of = GenericValidator.of();&nbsp; &nbsp; &nbsp; &nbsp; of.nonNull().apply("abc", abc).end(GENERIC_JSON_SERIALIZATION);&nbsp; &nbsp; &nbsp; &nbsp; of.notEmpty().min(1).max(1).apply("name", abc.getName())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .nonNull().apply("value", abc.getVal())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .notEmptyList().apply("childAbc", abc.getAbslist())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .end(GENERIC_JSON_SERIALIZATION);&nbsp; &nbsp; }&nbsp; &nbsp; @Test&nbsp; &nbsp; void genericValidationWhenObjectNull() {&nbsp; &nbsp; &nbsp; &nbsp; GenericValidator of = GenericValidator.of();&nbsp; &nbsp; &nbsp; &nbsp; Assertions.assertThrows(BusinessException.class, () -> of.nonNull()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .apply("abc", null).end(GENERIC_JSON_SERIALIZATION));&nbsp; &nbsp; }&nbsp; &nbsp; @Test&nbsp; &nbsp; void genericValidationWithExceptionInput() {&nbsp; &nbsp; &nbsp; &nbsp; Abc abc = new Abc();&nbsp; &nbsp; &nbsp; &nbsp; abc.setName("a");&nbsp; &nbsp; &nbsp; &nbsp; abc.setVal(1);&nbsp; &nbsp; &nbsp; &nbsp; GenericValidator of = GenericValidator.of();&nbsp; &nbsp; &nbsp; &nbsp; of.nonNull().apply("abc", abc).end(GENERIC_JSON_SERIALIZATION);&nbsp; &nbsp; &nbsp; &nbsp; GenericValidator apply = of.notEmpty().min(1).max(1).apply("name", abc.getName())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .nonNull().apply("value", abc.getVal())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .notEmptyList().apply("childAbc", abc.getAbslist());&nbsp; &nbsp; &nbsp; &nbsp; Assertions.assertThrows(BusinessException.class, () -> apply.end(GENERIC_JSON_SERIALIZATION));&nbsp; &nbsp; }}class Abc {&nbsp; &nbsp; String name;&nbsp; &nbsp; Integer val;&nbsp; &nbsp; List<ChildAbc> abslist;&nbsp; &nbsp; public String getName() {&nbsp; &nbsp; &nbsp; &nbsp; return name;&nbsp; &nbsp; }&nbsp; &nbsp; public void setName(String name) {&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; }&nbsp; &nbsp; public Integer getVal() {&nbsp; &nbsp; &nbsp; &nbsp; return val;&nbsp; &nbsp; }&nbsp; &nbsp; public void setVal(Integer val) {&nbsp; &nbsp; &nbsp; &nbsp; this.val = val;&nbsp; &nbsp; }&nbsp; &nbsp; public List<ChildAbc> getAbslist() {&nbsp; &nbsp; &nbsp; &nbsp; return abslist;&nbsp; &nbsp; }&nbsp; &nbsp; public void setAbslist(List<ChildAbc> abslist) {&nbsp; &nbsp; &nbsp; &nbsp; this.abslist = abslist;&nbsp; &nbsp; }}class ChildAbc {&nbsp; &nbsp; String name;&nbsp; &nbsp; public String getName() {&nbsp; &nbsp; &nbsp; &nbsp; return name;&nbsp; &nbsp; }&nbsp; &nbsp; public void setName(String name) {&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java