Hibernate Validator路径不起作用

时间:2018-08-16 23:17:41

标签: java hibernate

我有以下内容:

@ValidAccount
class Account {
   List<User> users;
}

class User {
   String name;
}

我的验证者有

public boolean isValid (final Account account, final ConstraintValidatorContext context) {
        // For the sake of simplicy, I am not looping through the array and just fetching the first entry
        User user = account.getUsers().get(0);
        if (user.getName().equals("SOME_DISALLOWED_NAME")) {
            context.disableDefaultConstraintViolation();
            context.buildConstraintViolationWithTemplate("name is invalid")
                    .addPropertyNode("users")
                    .addPropertyNode("name").inIterable().atIndex(0)
                    .addConstraintViolation();
            return false;
        }

        return true;
}

现在我希望能够做到

// Logging path shows users[0].name
Try.of(() -> (NodeImpl) Iterables.getLast(violation.getPropertyPath()))
    .andThen(node -> {
        final User user = (User) node.getParent().getValue();
    });

但是此转换失败。我在做什么错了?

修改

最终,我的自定义验证器旨在进行唯一检查,以确保没有两个用户具有相同的name

0 个答案:

没有答案