diff --git a/plusone-validator/pom.xml b/plusone-validator/pom.xml index a1689fa..06500a7 100644 --- a/plusone-validator/pom.xml +++ b/plusone-validator/pom.xml @@ -8,7 +8,7 @@ xyz.zhouxy.plusone plusone-validator-parent - 1.0.0-RC1 + 1.0.0-RC2 plusone-validator diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java index 0638d20..8ee57b2 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java @@ -19,6 +19,7 @@ package xyz.zhouxy.plusone.validator; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.AbstractMap.SimpleImmutableEntry; import java.util.Map.Entry; import java.util.function.Consumer; import java.util.function.Function; @@ -287,6 +288,24 @@ public abstract class BaseValidator implements IValidator { return validator; } + /** + * 添加一个针对二元组的校验器 + * + * @param 第一个元素的类型 + * @param 第二个元素的类型 + * @param v1Getter 用于从目标对象获取第一个元素的函数式接口。示例:{@code Person::getName1}。 + * @param v2Getter 用于从目标对象获取第二个元素的函数式接口。示例:{@code Person::getName2}。 + * @return {@code PairPropertyValidator}。用于添加针对该二元组的校验规则。 + */ + protected final PairPropertyValidator ruleForPair( + Function v1Getter, + Function v2Getter) { + PairPropertyValidator validator = new PairPropertyValidator<>( + t -> new SimpleImmutableEntry<>(v1Getter.apply(t), v2Getter.apply(t))); + this.rules.add(validator::validate); + return validator; + } + /** {@inheritDoc} */ @Override public void validate(T obj) { diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/PairPropertyValidatorTests.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/PairPropertyValidatorTests.java index 29c61d1..8396c1a 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/PairPropertyValidatorTests.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/PairPropertyValidatorTests.java @@ -41,7 +41,9 @@ public class PairPropertyValidatorTests { @Test void must_validInput() { - IValidator validator = new BaseValidator() { + ExampleCommand command = exampleCommandWithIntAndStringListProperty(100, "100"); + + IValidator validator1 = new BaseValidator() { { ruleForPair((ExampleCommand command) -> new SimpleImmutableEntry(command.getStringProperty(), command.getIntProperty())) .must((str, intValue) -> Objects.equals(str, intValue.toString())) @@ -51,69 +53,113 @@ public class PairPropertyValidatorTests { (str, intValue) -> ExampleException.withMessage("Validation failed: ('%s', %d).", str, intValue)); } }; + assertDoesNotThrow(() -> validator1.validate(command)); - ExampleCommand command = exampleCommandWithIntAndStringListProperty(100, "100"); - assertDoesNotThrow(() -> validator.validate(command)); + IValidator validator2 = new BaseValidator() { + { + ruleForPair(ExampleCommand::getStringProperty, ExampleCommand::getIntProperty) + .must((str, intValue) -> Objects.equals(str, intValue.toString())) + .must((str, intValue) -> Objects.equals(str, intValue.toString()), MESSAGE) + .must((str, intValue) -> Objects.equals(str, intValue.toString()), () -> ExampleException.withMessage(MESSAGE)) + .must((str, intValue) -> Objects.equals(str, intValue.toString()), + (str, intValue) -> ExampleException.withMessage("Validation failed: ('%s', %d).", str, intValue)); + } + }; + assertDoesNotThrow(() -> validator2.validate(command)); } @Test void must_default_invalidInput() { - IValidator validator = new BaseValidator() { + ExampleCommand command = exampleCommandWithIntAndStringListProperty(100, ""); + + IValidator validator1 = new BaseValidator() { { ruleForPair((ExampleCommand command) -> new SimpleImmutableEntry(command.getStringProperty(), command.getIntProperty())) .must((str, intValue) -> Objects.equals(str, intValue.toString())); } }; + ValidationException e1 = assertThrows(ValidationException.class, () -> validator1.validate(command)); + assertEquals("The specified condition was not met for the input.", e1.getMessage()); - ExampleCommand command = exampleCommandWithIntAndStringListProperty(100, ""); - - ValidationException e = assertThrows(ValidationException.class, () -> validator.validate(command)); - assertEquals("The specified condition was not met for the input.", e.getMessage()); + IValidator validator2 = new BaseValidator() { + { + ruleForPair(ExampleCommand::getStringProperty, ExampleCommand::getIntProperty) + .must((str, intValue) -> Objects.equals(str, intValue.toString())); + } + }; + ValidationException e2 = assertThrows(ValidationException.class, () -> validator2.validate(command)); + assertEquals("The specified condition was not met for the input.", e2.getMessage()); } @Test void must_message_invalidInput() { - IValidator validator = new BaseValidator() { + ExampleCommand command = exampleCommandWithIntAndStringListProperty(100, ""); + + IValidator validator1 = new BaseValidator() { { ruleForPair((ExampleCommand command) -> new SimpleImmutableEntry(command.getStringProperty(), command.getIntProperty())) .must((str, intValue) -> Objects.equals(str, intValue.toString()), MESSAGE); } }; + ValidationException e1 = assertThrows(ValidationException.class, () -> validator1.validate(command)); + assertEquals(MESSAGE, e1.getMessage()); - ExampleCommand command = exampleCommandWithIntAndStringListProperty(100, ""); - ValidationException e = assertThrows(ValidationException.class, () -> validator.validate(command)); - assertEquals(MESSAGE, e.getMessage()); + IValidator validator2 = new BaseValidator() { + { + ruleForPair(ExampleCommand::getStringProperty, ExampleCommand::getIntProperty) + .must((str, intValue) -> Objects.equals(str, intValue.toString()), MESSAGE); + } + }; + ValidationException e2 = assertThrows(ValidationException.class, () -> validator2.validate(command)); + assertEquals(MESSAGE, e2.getMessage()); } @Test void must_exceptionSupplier_invalidInput() { - IValidator validator = new BaseValidator() { + ExampleCommand command = exampleCommandWithIntAndStringListProperty(100, ""); + + IValidator validator1 = new BaseValidator() { { ruleForPair((ExampleCommand command) -> new SimpleImmutableEntry(command.getStringProperty(), command.getIntProperty())) .must((str, intValue) -> Objects.equals(str, intValue.toString()), () -> ExampleException.withMessage(MESSAGE)); } }; + ExampleException e1 = assertThrows(ExampleException.class, () -> validator1.validate(command)); + assertEquals(MESSAGE, e1.getMessage()); - ExampleCommand command = exampleCommandWithIntAndStringListProperty(100, ""); - - ExampleException e = assertThrows(ExampleException.class, () -> validator.validate(command)); - assertEquals(MESSAGE, e.getMessage()); + IValidator validator2 = new BaseValidator() { + { + ruleForPair(ExampleCommand::getStringProperty, ExampleCommand::getIntProperty) + .must((str, intValue) -> Objects.equals(str, intValue.toString()), () -> ExampleException.withMessage(MESSAGE)); + } + }; + ExampleException e2 = assertThrows(ExampleException.class, () -> validator2.validate(command)); + assertEquals(MESSAGE, e2.getMessage()); } @Test void must_exceptionFunction_invalidInput() { - IValidator validator = new BaseValidator() { + ExampleCommand command = exampleCommandWithIntAndStringListProperty(100, ""); + + IValidator validator1 = new BaseValidator() { { ruleForPair((ExampleCommand command) -> new SimpleImmutableEntry(command.getStringProperty(), command.getIntProperty())) .must((str, intValue) -> Objects.equals(str, intValue.toString()), (str, intValue) -> ExampleException.withMessage("Validation failed: ('%s', %d).", str, intValue)); } }; + ExampleException e1 = assertThrows(ExampleException.class, () -> validator1.validate(command)); + assertEquals("Validation failed: ('', 100).", e1.getMessage()); - ExampleCommand command = exampleCommandWithIntAndStringListProperty(100, ""); - - ExampleException e = assertThrows(ExampleException.class, () -> validator.validate(command)); - assertEquals("Validation failed: ('', 100).", e.getMessage()); + IValidator validator2 = new BaseValidator() { + { + ruleForPair(ExampleCommand::getStringProperty, ExampleCommand::getIntProperty) + .must((str, intValue) -> Objects.equals(str, intValue.toString()), + (str, intValue) -> ExampleException.withMessage("Validation failed: ('%s', %d).", str, intValue)); + } + }; + ExampleException e2 = assertThrows(ExampleException.class, () -> validator2.validate(command)); + assertEquals("Validation failed: ('', 100).", e2.getMessage()); } // ================================ diff --git a/pom.xml b/pom.xml index 955ccaa..0b48140 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ xyz.zhouxy.plusone plusone-validator-parent - 1.0.0-RC1 + 1.0.0-RC2 plusone-validator-parent http://zhouxy.xyz