bean-validation

What is point of constraint validation @Null?

旧街凉风 提交于 2019-12-10 01:30:19
问题 I was checking list of available constraints in javax.validation package and I noticed that there is an annotation @Null which force the field to be null. I do not understand what is point of adding it to my field if I already know it should be null. For example look at this class: public class MyClass{ @NotNull private String myString1; @Null private String myString2; // getter setters... } @NotNull completely makes sense. I do not expect myString1 to be null. but @Null makes myString2

f:convertNumber on Double: ClassCastException

与世无争的帅哥 提交于 2019-12-10 00:37:56
问题 In JSF 2.3, I have an h:inputText to edit a Double value, which has in addition Bean-Validation constraints. The h:inputText has a f:convertNumber . When submitting the form, this leads to a ClassCastException (see below). So, it seems, that f:convertNumber produces a Long which then, could not be converted to Double to validate the @DecimalMin constraint, right? In JSF 2.2 this worked as expected, the problem occured after upgrading to JSF 2.3. Does anybody has any ideas what could be the

Using @Autowired component in custom JSR-303 validator

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-09 23:11:01
问题 I'm trying to implement a custom validator for my model classes that autowires a custom bean of mine (declared via @Component ). In this, I followed the Spring documentation on that topic. My AuthenticationFacade object is implemented according to this tutorial. When running my tests, however, the autowired attribute in the Validator object is always null . Why is that? Here are the relevant parts of my code: My custom bean , AuthenticationFacadeImpl.java @Component public class

Validation framework in C#?

ⅰ亾dé卋堺 提交于 2019-12-09 18:11:19
问题 In the java world there is the bean validation framework JSR-303 which is a nicely well thought out strategy for performing data validation in both the presentation and persistence layers of an application. It covers a lot of things, including validation of whole graph models, validation domain grouping, i18n, etc. I have failed to find any data model validation frameworks in C#. Are there anything similar to JSR-303 in C#? 回答1: There is the Enterprise Library Validation block. http://msdn

How do I construct a ConstraintViolationException?

风格不统一 提交于 2019-12-09 08:55:55
问题 I am puzzled by the javax.validation API. I am writing a simple test to understand it : Sample sample = new Sample(); Set<ConstraintViolation<Sample>> violations = validator.validate(sample); if (!violations.isEmpty()) { // Eclipse refuses to let me use my violations variable throw new ConstraintViolationException(violations); } How should I declare the set of violations so I can use it in my exception constructor? 回答1: You can work around this like so: throw new ConstraintViolationException(

Get field name when javax.validation.ConstraintViolationException is thrown

谁说胖子不能爱 提交于 2019-12-09 08:49:21
问题 When the PathVariable 'name' doesn't pass validation a javax.validation.ConstraintViolationException is thrown. Is there a way to retrieve the parameter name in the thrown javax.validation.ConstraintViolationException? @RestController @Validated public class HelloController { @RequestMapping("/hi/{name}") public String sayHi(@Size(max = 10, min = 3, message = "name should have between 3 and 10 characters") @PathVariable("name") String name) { return "Hi " + name; } 回答1: The following

JPA - No validator could be found for type: enum

半腔热情 提交于 2019-12-09 07:48:41
问题 I have an entity class which uses an enum type for one of the properties, and I am getting the following exception when I try to persist the entity: javax.validation.UnexpectedTypeException: No validator could be found for type: model.schema.BaseYesNo Any idea why this might be happening? My thinking is that since it is an enum, it should already be validated by the compiler, so no need for some kind of validator. (code below) The entity property: @Enumerated( EnumType.STRING ) @Column( name

Dependency Injection in JSR-303 ConstraintValidator

我只是一个虾纸丫 提交于 2019-12-08 13:27:00
问题 I have the same problem that the guy from this thread has. More precisely I get the following error, when trying to inject a bean in my custom validator that implements CustomValidator interface (it's a NPE when accessing the bean i wanted to inject): javax.validation.ValidationException: HV000028: Unexpected exception during isValid call. at org.hibernate.validator.internal.engine.ConstraintTree.validateSingleConstraint(ConstraintTree.java:294) at org.hibernate.validator.internal.engine

Validating a List of beans using Bean Validation 2.0 (JSR-308) and Spring 5

半腔热情 提交于 2019-12-08 11:24:08
问题 Spring 5 is supposed to support Bean Validation 2.0 which introduced validation on List types but I can't seem to get it to work for me. I have the following endpoint in my @RestController : @PostMapping("/foos") public List<Foo> analysePoints(@RequestBody @Valid List<@Valid Foo> request) { ... } Where the class Foo has some JSR validation annotations on its properties. Unfortunately, this doesn't seem to validate it at all. Prior to Spring 5 I got around this by wrapping the List<Foo> in a

How to match cross-field bean validation errors to a single h:message?

回眸只為那壹抹淺笑 提交于 2019-12-08 08:59:48
问题 We're currently building a web application based on JSF and PrimeFaces and want to use Bean Validation to validate the domain model. The problem we currently face is that we want most error messages displayed next to a single input field. For example, suppose there's a constraint "enddate > startdate", implemented as a class-level bean validator, we still want constraint violations for that rule to show up in a h:message/p:message specific to the enddate field rather than in the globalOnly