feat(validator): 添加 notEqual 校验方法并优化 equalTo

- 新增 `notEqual` 校验方法,用于判断属性值不等于给定值。
- 优化 `equalTo` 方法的空值判断逻辑,当属性值为空时不进行校验。
This commit is contained in:
zhouxy108 2025-06-01 20:35:41 +08:00
parent 88b3226b17
commit 7f4f5748f8
2 changed files with 106 additions and 15 deletions

View File

@ -237,7 +237,7 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
*/
public <E extends RuntimeException> TPropertyValidator equalTo(
Object that, Function<TProperty, E> e) {
withRule(value -> Objects.equals(value, that), e);
withRule(value -> value == null || value.equals(that), e);
return thisObject();
}
@ -245,6 +245,63 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
// #endregion - equalTo
// ================================
// ================================
// #region - notEqual
// ================================
/**
* 添加一条校验属性的规则校验属性是否等于给定值
*
* @param that 给定值
* @return 属性校验器
*/
public TPropertyValidator notEqual(Object that) {
return notEqual(that, value -> ValidationException
.withMessage("The input must not equal '%s'.", that));
}
/**
* 添加一条校验属性的规则校验属性是否等于给定值
*
* @param that 给定值
* @param errMsg 错误信息
* @return 属性校验器
*/
public TPropertyValidator notEqual(Object that, String errMsg) {
return notEqual(that, convertToExceptionFunction(errMsg));
}
/**
* 添加一条校验属性的规则校验属性是否等于给定值
*
* @param <E> 自定义异常类型
* @param that 给定值
* @param e 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> TPropertyValidator notEqual(
Object that, Supplier<E> e) {
return notEqual(that, convertToExceptionFunction(e));
}
/**
* 添加一条校验属性的规则校验属性是否等于给定值
*
* @param <E> 自定义异常类型
* @param that 给定值
* @param e 自定义异常
* @return 属性校验器
*/
public <E extends RuntimeException> TPropertyValidator notEqual(
Object that, Function<TProperty, E> e) {
withRule(value -> value == null || !value.equals(that), e);
return thisObject();
}
// ================================
// #endregion - notEqual
// ================================
// ================================
// #region - must
// ================================

View File

@ -231,8 +231,11 @@ public class ObjectPropertyValidatorTests {
}
};
ExampleCommand command = new ExampleCommand();
command.setStringProperty("Foo");
command.setStringProperty(null);
assertDoesNotThrow(() -> validator.validate(command));
command.setStringProperty("Foo");
assertDoesNotThrow(() -> validator.validate(command));
}
@ -281,52 +284,83 @@ public class ObjectPropertyValidatorTests {
assertEquals("The stringProperty should be equal to 'Foo', but is was 'Bar'.", specifiedException2.getMessage());
}
// ================================
// #endregion - equalTo
// ================================
// ================================
// #region - notEqual
// ================================
@Test
void equalTo_nullInput() {
void notEqual_validInput() {
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
{
ruleForString(ExampleCommand::getStringProperty)
.notEqual("Foo")
.notEqual("Foo", "The stringProperty should not equal 'Foo'.")
.notEqual("Foo", () ->
ExampleException.withMessage("The stringProperty should not equal 'Foo'."))
.notEqual("Foo", str ->
ExampleException.withMessage("The stringProperty should not equal 'Foo', but is was '%s'.", str));
}
};
ExampleCommand command = new ExampleCommand();
command.setStringProperty(null);
assertDoesNotThrow(() -> validator.validate(command));
command.setStringProperty("Bar");
assertDoesNotThrow(() -> validator.validate(command));
}
@Test
void notEqual_invalidInput() {
ExampleCommand command = new ExampleCommand();
command.setStringProperty("Foo");
IValidator<ExampleCommand> defaultRule = new BaseValidator<ExampleCommand>() {
{
ruleForString(ExampleCommand::getStringProperty).equalTo("Foo");
ruleForString(ExampleCommand::getStringProperty).notEqual("Foo");
}
};
ValidationException eWithDefaultMessage = assertThrows(
ValidationException.class, () -> defaultRule.validate(command));
assertEquals("The input must be equal to 'Foo'.", eWithDefaultMessage.getMessage());
assertEquals("The input must not equal 'Foo'.", eWithDefaultMessage.getMessage());
IValidator<ExampleCommand> ruleWithMessage = new BaseValidator<ExampleCommand>() {
{
ruleForString(ExampleCommand::getStringProperty).equalTo("Foo",
"The stringProperty should be equal to 'Foo'.");
ruleForString(ExampleCommand::getStringProperty).notEqual("Foo",
"The stringProperty should not equal 'Foo'.");
}
};
ValidationException eWithSpecifiedMessage = assertThrows(
ValidationException.class, () -> ruleWithMessage.validate(command));
assertEquals("The stringProperty should be equal to 'Foo'.", eWithSpecifiedMessage.getMessage());
assertEquals("The stringProperty should not equal 'Foo'.", eWithSpecifiedMessage.getMessage());
IValidator<ExampleCommand> ruleWithExceptionSupplier = new BaseValidator<ExampleCommand>() {
{
ruleForString(ExampleCommand::getStringProperty).equalTo("Foo",
() -> ExampleException.withMessage("The stringProperty should be equal to 'Foo'."));
ruleForString(ExampleCommand::getStringProperty).notEqual("Foo",
() -> ExampleException.withMessage("The stringProperty should not equal 'Foo'."));
}
};
ExampleException specifiedException = assertThrows(
ExampleException.class, () -> ruleWithExceptionSupplier.validate(command));
assertEquals("The stringProperty should be equal to 'Foo'.", specifiedException.getMessage());
assertEquals("The stringProperty should not equal 'Foo'.", specifiedException.getMessage());
IValidator<ExampleCommand> ruleWithExceptionFunction = new BaseValidator<ExampleCommand>() {
{
ruleForString(ExampleCommand::getStringProperty).equalTo("Foo",
str -> ExampleException.withMessage("The stringProperty should be equal to 'Foo', but is was '%s'.", str));
ruleForString(ExampleCommand::getStringProperty).notEqual("Foo",
str -> ExampleException.withMessage("The stringProperty should not equal 'Foo', but is was '%s'.", str));
}
};
ExampleException specifiedException2 = assertThrows(
ExampleException.class, () -> ruleWithExceptionFunction.validate(command));
assertEquals("The stringProperty should be equal to 'Foo', but is was 'null'.", specifiedException2.getMessage());
assertEquals("The stringProperty should not equal 'Foo', but is was 'Foo'.", specifiedException2.getMessage());
}
// ================================
// #endregion - equalTo
// #endregion - notEqual
// ================================
// ================================