休眠验证器 4.2.0 最终@CreditCardNumber

发布于 2024-11-16 19:03:13 字数 1088 浏览 8 评论 0原文

@Autowired
private Validator validator;

    @Test
        public void testValidateMethodOfPaymentBadCreditCard() {
            final MethodOfPayment mop = new MethodOfPayment();
            command.setDescription("1234567890");
            final Set<ConstraintViolation<MethodOfPayment>> constraintViolations = this.validator.validateProperty(mop, "cardNumber", Default.class);
            Assert.assertFalse(constraintViolations.isEmpty());
            for (final ConstraintViolation<MethodOfPayment> cv : constraintViolations) {
                Assert.assertEquals(cv.getMessage(), "{error.invalid.cardNumber}");
            }
        }

单元类在我的 xml 中有定义实例。

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>

我的支付方法注释为

@CreditCardNumber(message="{error.invalid.cardNumber}")
    public String getCardNumber() {
        return this.cardNumber;
    }

“此测试在线失败”,表示约束违规为空。我想它不应该是空的吧?

怎么了? 什么样的数据无法通过信用卡验证?

@Autowired
private Validator validator;

    @Test
        public void testValidateMethodOfPaymentBadCreditCard() {
            final MethodOfPayment mop = new MethodOfPayment();
            command.setDescription("1234567890");
            final Set<ConstraintViolation<MethodOfPayment>> constraintViolations = this.validator.validateProperty(mop, "cardNumber", Default.class);
            Assert.assertFalse(constraintViolations.isEmpty());
            for (final ConstraintViolation<MethodOfPayment> cv : constraintViolations) {
                Assert.assertEquals(cv.getMessage(), "{error.invalid.cardNumber}");
            }
        }

The unit class has instance of define in my xml.

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>

my methodofpayment is annotated with

@CreditCardNumber(message="{error.invalid.cardNumber}")
    public String getCardNumber() {
        return this.cardNumber;
    }

This test fails on line saying the constraint violation is empty. I thought it should not be empty right?

What is wrong?
What kind of data fails for credit card validation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东京女 2024-11-23 19:03:13

null 被认为是 Bean Validation 和 Hibernate Validator 中定义的大多数约束的有效值。

因此,如果您不为 cardNumber 属性分配值或使用 @NotNull 注释它,则不会违反约束(顺便说一句。您的列表看起来有点奇怪,command 从哪里来?)

@CreditCardNumber 的验证器使用 Luhn 算法 用于检查信用卡号码。

null is considered as valid value for most constraints defined in Bean Validation and Hibernate Validator.

Hence you won't get a constraint violation if you don't assign a value to the cardNumber property or annotate it with @NotNull (btw. your listing seems a bit strange, where does command come from?)

The validator for @CreditCardNumber uses the Luhn algorithm for checking credit card numbers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文