diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java index 89b88b1..73a32cf 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java @@ -237,7 +237,7 @@ public abstract class BasePropertyValidator TPropertyValidator equalTo( Object that, Function 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 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 自定义异常类型 + * @param that 给定值 + * @param e 自定义异常 + * @return 属性校验器 + */ + public TPropertyValidator notEqual( + Object that, Supplier e) { + return notEqual(that, convertToExceptionFunction(e)); + } + + /** + * 添加一条校验属性的规则,校验属性是否等于给定值 + * + * @param 自定义异常类型 + * @param that 给定值 + * @param e 自定义异常 + * @return 属性校验器 + */ + public TPropertyValidator notEqual( + Object that, Function e) { + withRule(value -> value == null || !value.equals(that), e); + return thisObject(); + } + + // ================================ + // #endregion - notEqual + // ================================ + // ================================ // #region - must // ================================ diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ObjectPropertyValidatorTests.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ObjectPropertyValidatorTests.java index 56d792d..b6f0f70 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ObjectPropertyValidatorTests.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ObjectPropertyValidatorTests.java @@ -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 validator = new BaseValidator() { + { + 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 defaultRule = new BaseValidator() { { - 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 ruleWithMessage = new BaseValidator() { { - 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 ruleWithExceptionSupplier = new BaseValidator() { { - 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 ruleWithExceptionFunction = new BaseValidator() { { - 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 // ================================ // ================================