feat: 重载 BaseValidator#ruleForPair
- 重载 BaseValidator#ruleForPair,允许传入两个 Function,用于分别从目标对象中获取两个属性 - 升级项目版本至 1.0.0-RC2
This commit is contained in:
@@ -8,7 +8,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>xyz.zhouxy.plusone</groupId>
|
<groupId>xyz.zhouxy.plusone</groupId>
|
||||||
<artifactId>plusone-validator-parent</artifactId>
|
<artifactId>plusone-validator-parent</artifactId>
|
||||||
<version>1.0.0-RC1</version>
|
<version>1.0.0-RC2</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>plusone-validator</artifactId>
|
<artifactId>plusone-validator</artifactId>
|
||||||
|
@@ -19,6 +19,7 @@ package xyz.zhouxy.plusone.validator;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.AbstractMap.SimpleImmutableEntry;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
@@ -287,6 +288,24 @@ public abstract class BaseValidator<T> implements IValidator<T> {
|
|||||||
return validator;
|
return validator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加一个针对二元组的校验器
|
||||||
|
*
|
||||||
|
* @param <V1> 第一个元素的类型
|
||||||
|
* @param <V2> 第二个元素的类型
|
||||||
|
* @param v1Getter 用于从目标对象获取第一个元素的函数式接口。示例:{@code Person::getName1}。
|
||||||
|
* @param v2Getter 用于从目标对象获取第二个元素的函数式接口。示例:{@code Person::getName2}。
|
||||||
|
* @return {@code PairPropertyValidator}。用于添加针对该二元组的校验规则。
|
||||||
|
*/
|
||||||
|
protected final <V1, V2> PairPropertyValidator<T, V1, V2> ruleForPair(
|
||||||
|
Function<T, V1> v1Getter,
|
||||||
|
Function<T, V2> v2Getter) {
|
||||||
|
PairPropertyValidator<T, V1, V2> validator = new PairPropertyValidator<>(
|
||||||
|
t -> new SimpleImmutableEntry<>(v1Getter.apply(t), v2Getter.apply(t)));
|
||||||
|
this.rules.add(validator::validate);
|
||||||
|
return validator;
|
||||||
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public void validate(T obj) {
|
public void validate(T obj) {
|
||||||
|
@@ -41,7 +41,9 @@ public class PairPropertyValidatorTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void must_validInput() {
|
void must_validInput() {
|
||||||
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
ExampleCommand command = exampleCommandWithIntAndStringListProperty(100, "100");
|
||||||
|
|
||||||
|
IValidator<ExampleCommand> validator1 = new BaseValidator<ExampleCommand>() {
|
||||||
{
|
{
|
||||||
ruleForPair((ExampleCommand command) -> new SimpleImmutableEntry<String,Integer>(command.getStringProperty(), command.getIntProperty()))
|
ruleForPair((ExampleCommand command) -> new SimpleImmutableEntry<String,Integer>(command.getStringProperty(), command.getIntProperty()))
|
||||||
.must((str, intValue) -> Objects.equals(str, intValue.toString()))
|
.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));
|
(str, intValue) -> ExampleException.withMessage("Validation failed: ('%s', %d).", str, intValue));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
assertDoesNotThrow(() -> validator1.validate(command));
|
||||||
|
|
||||||
ExampleCommand command = exampleCommandWithIntAndStringListProperty(100, "100");
|
IValidator<ExampleCommand> validator2 = new BaseValidator<ExampleCommand>() {
|
||||||
assertDoesNotThrow(() -> validator.validate(command));
|
{
|
||||||
|
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
|
@Test
|
||||||
void must_default_invalidInput() {
|
void must_default_invalidInput() {
|
||||||
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
ExampleCommand command = exampleCommandWithIntAndStringListProperty(100, "");
|
||||||
|
|
||||||
|
IValidator<ExampleCommand> validator1 = new BaseValidator<ExampleCommand>() {
|
||||||
{
|
{
|
||||||
ruleForPair((ExampleCommand command) -> new SimpleImmutableEntry<String,Integer>(command.getStringProperty(), command.getIntProperty()))
|
ruleForPair((ExampleCommand command) -> new SimpleImmutableEntry<String,Integer>(command.getStringProperty(), command.getIntProperty()))
|
||||||
.must((str, intValue) -> Objects.equals(str, intValue.toString()));
|
.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, "");
|
IValidator<ExampleCommand> validator2 = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
ValidationException e = assertThrows(ValidationException.class, () -> validator.validate(command));
|
ruleForPair(ExampleCommand::getStringProperty, ExampleCommand::getIntProperty)
|
||||||
assertEquals("The specified condition was not met for the input.", e.getMessage());
|
.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
|
@Test
|
||||||
void must_message_invalidInput() {
|
void must_message_invalidInput() {
|
||||||
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
ExampleCommand command = exampleCommandWithIntAndStringListProperty(100, "");
|
||||||
|
|
||||||
|
IValidator<ExampleCommand> validator1 = new BaseValidator<ExampleCommand>() {
|
||||||
{
|
{
|
||||||
ruleForPair((ExampleCommand command) -> new SimpleImmutableEntry<String,Integer>(command.getStringProperty(), command.getIntProperty()))
|
ruleForPair((ExampleCommand command) -> new SimpleImmutableEntry<String,Integer>(command.getStringProperty(), command.getIntProperty()))
|
||||||
.must((str, intValue) -> Objects.equals(str, intValue.toString()), MESSAGE);
|
.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, "");
|
IValidator<ExampleCommand> validator2 = new BaseValidator<ExampleCommand>() {
|
||||||
ValidationException e = assertThrows(ValidationException.class, () -> validator.validate(command));
|
{
|
||||||
assertEquals(MESSAGE, e.getMessage());
|
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
|
@Test
|
||||||
void must_exceptionSupplier_invalidInput() {
|
void must_exceptionSupplier_invalidInput() {
|
||||||
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
ExampleCommand command = exampleCommandWithIntAndStringListProperty(100, "");
|
||||||
|
|
||||||
|
IValidator<ExampleCommand> validator1 = new BaseValidator<ExampleCommand>() {
|
||||||
{
|
{
|
||||||
ruleForPair((ExampleCommand command) -> new SimpleImmutableEntry<String,Integer>(command.getStringProperty(), command.getIntProperty()))
|
ruleForPair((ExampleCommand command) -> new SimpleImmutableEntry<String,Integer>(command.getStringProperty(), command.getIntProperty()))
|
||||||
.must((str, intValue) -> Objects.equals(str, intValue.toString()), () -> ExampleException.withMessage(MESSAGE));
|
.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, "");
|
IValidator<ExampleCommand> validator2 = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
ExampleException e = assertThrows(ExampleException.class, () -> validator.validate(command));
|
ruleForPair(ExampleCommand::getStringProperty, ExampleCommand::getIntProperty)
|
||||||
assertEquals(MESSAGE, e.getMessage());
|
.must((str, intValue) -> Objects.equals(str, intValue.toString()), () -> ExampleException.withMessage(MESSAGE));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
ExampleException e2 = assertThrows(ExampleException.class, () -> validator2.validate(command));
|
||||||
|
assertEquals(MESSAGE, e2.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void must_exceptionFunction_invalidInput() {
|
void must_exceptionFunction_invalidInput() {
|
||||||
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
ExampleCommand command = exampleCommandWithIntAndStringListProperty(100, "");
|
||||||
|
|
||||||
|
IValidator<ExampleCommand> validator1 = new BaseValidator<ExampleCommand>() {
|
||||||
{
|
{
|
||||||
ruleForPair((ExampleCommand command) -> new SimpleImmutableEntry<String,Integer>(command.getStringProperty(), command.getIntProperty()))
|
ruleForPair((ExampleCommand command) -> new SimpleImmutableEntry<String,Integer>(command.getStringProperty(), command.getIntProperty()))
|
||||||
.must((str, intValue) -> Objects.equals(str, intValue.toString()),
|
.must((str, intValue) -> Objects.equals(str, intValue.toString()),
|
||||||
(str, intValue) -> ExampleException.withMessage("Validation failed: ('%s', %d).", str, intValue));
|
(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, "");
|
IValidator<ExampleCommand> validator2 = new BaseValidator<ExampleCommand>() {
|
||||||
|
{
|
||||||
ExampleException e = assertThrows(ExampleException.class, () -> validator.validate(command));
|
ruleForPair(ExampleCommand::getStringProperty, ExampleCommand::getIntProperty)
|
||||||
assertEquals("Validation failed: ('', 100).", e.getMessage());
|
.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());
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================================
|
// ================================
|
||||||
|
2
pom.xml
2
pom.xml
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
<groupId>xyz.zhouxy.plusone</groupId>
|
<groupId>xyz.zhouxy.plusone</groupId>
|
||||||
<artifactId>plusone-validator-parent</artifactId>
|
<artifactId>plusone-validator-parent</artifactId>
|
||||||
<version>1.0.0-RC1</version>
|
<version>1.0.0-RC2</version>
|
||||||
|
|
||||||
<name>plusone-validator-parent</name>
|
<name>plusone-validator-parent</name>
|
||||||
<url>http://zhouxy.xyz</url>
|
<url>http://zhouxy.xyz</url>
|
||||||
|
Reference in New Issue
Block a user