Hot to write a custom validator with apache bval - java

I am using bean-validation with apache bval as its implementation. (I do not want to use hibernate-validation or oval, because they have too much dependencies.)
First question: Is bval still maintained or is it orphaned?
Second question: Is it possible to implement custom validators using bval? E.g. a need a validator to File field that validates, that the file exists, isreadable and a file.

Apache BVal is afaik compliant with Bean Validation 1.0 which means should be able to write portable custom constraints. However, BVal is not yet Bean Validation 1.1 compatible, meaning you won't have executable constraints.
I don't know how active development is atm. Last release was in September 2012 which is quite a while back.
What is your concern with the Hibernate Validator or OVal dependencies?

Somebody did a performance comparison. However I am not sure how objective this comparison is: http://soularis999.blogspot.com/2011/07/bean-validation-performance-evaluation.html

Theres a how to now on baeldung.com: https://www.baeldung.com/apache-bval, it looks pretty standard
define an new annotation describing your constraint
define an constraint validator which implements ConstraintValidator<CONSTRAINT, TARGET>
annotatate your annotation with #Constraint(validatedBy={Validator.class})

Related

Questions around validation

I think this might possibly be answered elsewhere, but was getting lost in the context of other posts. So, decided to restate here. If anyone knows of this being already answered elsewhere, please point me in that direction. So, my questions.
I see JSR-303, JSR-349 and Hibernate Validator. What is the difference?
It seems to me that Hibernate Validator is a reference implementation of the JSR-303 spec, is that true?
Then what is 349?
If Hibernate Validator is a reference impl of the spec, are there other impl's of the spec?
Now some specifics of what I am looking for. I have done some reading about JSR-303 and I follow generally how the annotations work. My question is can I dynamically change the constraint values? Take the following for example:
public class Foo {
#Min(value = 18, message = "Age must be greater than or equal to 18")
#Max(value = 150, message = "Age must be less than or equal to 150")
int fooAge;
}
So, is there a way using the spec to feed the age constraints at runtime. So, lets say for some reason in one case I need the min to be 20 and max to be 100. In another case I need 10 and 50, etc. etc.
Thanks in advance for helping to clear this up for me.
303 is the JSR for Bean Validation 1.0. 349 is the JSR for Bean Validation 1.1.
The home page of the project says: "Hibernate Validator 5.x is the reference implementation Bean Validation 1.1!"
The JSR defines the specification. Hibernate validator is the reference implementation of this specification.
It seems not: http://beanvalidation.org/1.1/certified/
Yes, you can do that by assigning the constraints to validation groups, and activate one group or another depending on the use-case. See http://beanvalidation.org/1.0/spec/#validationapi-validatorapi-groups
As far as question is concerned:
If Hibernate Validator is a reference impl of the spec, are there other impl's of the spec?
There is also apache implementation of JSR-303 -> http://bval.apache.org/.
Unfortunately the latest release version Apache BVal (0.5) doesn't support Bean Validation 1.1. It should be introduced in version: Apache BVal (1.1.0). Right now there is an alpa-snapshot release:
<dependency>
<groupId>org.apache.bval</groupId>
<artifactId>bval-jsr</artifactId>
<version>1.1.0-alpha-SNAPSHOT</version>
</dependency>
More information on: http://cxf.apache.org/docs/validationfeature.html

Getting a java bean property value by annotation

How do I get the returned value from a java bean property by annotation?
For example, trying to get the returned value of the property id from a Hibernate entity using its annotation (#Id).
Thanks.
Edit:
I've eliminated the requirement of using javassist for this question.
I don't know much details about javassit internal except that hibernate uses it internally, but surely can tell you that spring reflection util have a powerful support for handling things like processing all the classes which have an annotation on properties[e.g.] [user defined or not].In case you don't find success with javassist, give a try to spring reflection api.

CXF/JAXB Code-first service: modify XMLSchemaType of inputs

I am working on a CXF/JAXB code-first web service. I would like to be able to modify the WSDL definitions of some of the inputs.
For example, given the following interface snippet:
#WebMethod
public void Something(#WebParam(name="date") Date date);
The WSDL will generate the XMLSchema for the input "date" to be a "datetime" xml element. I would like it to be simply a "date" element instead.
It would also be nice to be able to specify the some of the other input attributes, such as minOccurs, etc.
For a custom object, the sub-elements can define all of these things through annotations such as XmlElement and XmlSchemaType. However, these annotations are not legal on an input parameter.
I know earlier versions of CXF did not handle this, but I'm not sure about the later versions. I'm currently running CXF 2.3.5.
Here is the answer I got from the cxf-users list:
1) Endorse the JAXB 2.2 API jar via one of the JDK endorsement mechanisms.
Then the XmlElement and XmlSchemaType annotations can be used on the parameter.
(This is not quite correct--the 2.2 jaxb that I endorsed did allow the #XmlElement annotation on a parameter, but it did not allow the #XmlSchemaType annotation)
2) Create the wrapper type beans (you can use java2ws -warpperBeans as a starting point) and add the #RequestWrapper/#ResponseWrapper annotations to
the method to point at them. You can finely control the appearance of the
wrapper schema via annotations in the beans.
3) You CAN configure extra subclasses of AbstractServiceConfiguration into the factories where you override some of the methods to set various
minOccurs/maxOccurs/etc.. type things. VERY fine level of control, but very
complex to do.

Python validation API

I'm looking for a Python (<3) validation API something like Java's Bean Validation or Spring validation. I'm not looking for a library that is limited to form validation, since I want to validate domain objects.
Do you know a Python API for validating of domain objects?
maybe you should try formencode? it's not form-only validation library
Spring.py
The only libraries that are a bit like Java's Bean validation that I know (and have used) are:
Zope 3's zope.schema , but this is tied to using zope interfaces I think
SQLAlchamy's Elixir, but this is tied to using SQLAlchemy entities
I'm not sure if (or how) these could be used without depending of specific super classes
+1 for formencode and also there is promising pycerberus .

Commonly reusable annotations or commons annotations?

Are there any commonly usable annotations available? Similar to commons-lang?
If not, have you seen any effective use of annontations (not built-in annotations) as part of any open source application development.
I remember Mifos was using it for Transaction.
Mohan
i think Hibernate Validator has really good and reusable annotations for any kind of validation. it is based on a the reference implementation for JSR 303: Bean Validation.
Only non-standard annotations I've used more than once outside my testing project have been WicketStuff Annotations which are very useful in their own context.
Another interesting annotation set which is also the basis for JSR-305 is FindBugs' annotations which also may prove useful in the future - we'll see how that goes.
Check out my Bean annotations
http://code.google.com/p/javadude/wiki/Annotations
Things like
#Bean(
cloneable=true,
defineSimpleEqualsAndHashCode=true,
properties={
#Property(name="name", bound=true),
#Property(name="age", type=int.class, bound=true),
#Property(name="friend", type=Person.class, kind=PropertyKind.LIST)
},
observers={
#Observer(type=FeverListener.class)
}
)
public class Person extends PersonGen { }
The annotation processor generates the PersonGen superclass.
Note that I'm currently working on a major change to them and the API is changing (I'll still leave the current version available, but the 3.x.x version stream will be breaking)
I'm trying to get the new version done in the next couple of weeks.
JAXB defines annotations (javax.xml.bind.annotation) that are reused to some degree -- although they are named to indicate they only related to XML serialization, most of metadata has to do with annotating properties to serialize, so they can be used for serializing to other data formats (such as JSON) too. Jackson JSON processor supports them, along its own 'native' annotations, since there are no really standardizes non-data-format specific annotations (AFAIK).
I like and Oval http://oval.sourceforge.net/ and JAXB
there really needs to be a set of common annotationsin the core jre which are used in similar ways in multiple frameworks.
for example #Transactional #Nullable

Categories