-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: @Size or @Pattern for Custom User Types
PostPosted: Tue Jun 21, 2011 4:25 am 
Newbie

Joined: Mon Mar 07, 2011 9:46 am
Posts: 10
Hi,

I am using Hibernate Validator for validating the user inputs. So far so good. I created now some custom datatypes, which mainly depend on a String (e.g. an Type for an Email-Address). Is it possible to use annotations like @Size or @Pattern or other default annotation on properties of a custom data type? Something like:
Code:
@Size(min=10)
private Email email;


Or do I have to create my own Annotation for this?

Thanks for your help and kind regards,
Andreas


Top
 Profile  
 
 Post subject: Re: @Size or @Pattern for Custom User Types
PostPosted: Tue Jun 21, 2011 5:25 am 
Contributor
Contributor

Joined: Mon Feb 28, 2011 7:27 pm
Posts: 20
Location: France
Hi,

It's possible to alter ConstraintValidator list associated to a given constraint (in your case @Size and @Pattern).
The only way to achieve this (or the only I have in mind :) is to use an XML mapping file as it described here http://people.redhat.com/~ebernard/vali ... definition

Note: This mapping file has to be listed in the validation configuration descriptor (validation.xml) as described here http://people.redhat.com/~ebernard/vali ... xml-config

Hope this help

--Kevin


Top
 Profile  
 
 Post subject: Re: @Size or @Pattern for Custom User Types
PostPosted: Tue Jun 21, 2011 5:29 am 
Newbie

Joined: Mon Mar 07, 2011 9:46 am
Posts: 10
Hi Kevin,

thanks for your reply. I will have a look on the links. It is planed, that this configuration can be done in annotation?

Thanks and kind regards,
Andreas


Top
 Profile  
 
 Post subject: Re: @Size or @Pattern for Custom User Types
PostPosted: Wed Jun 22, 2011 2:45 am 
Newbie

Joined: Mon Mar 07, 2011 9:46 am
Posts: 10
Hi,

I just read the article and tested it. I put a validation.xml with the following content in the META-INF folder:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<constraint-mappings
        xmlns="http://jboss.org/xml/ns/javax/validation/mapping"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration validation-configuration-1.0.xsd">
           
    <constraint-definition annotation="javax.validation.constraint.Size">
        <validated-by include-existing-validators="true">
            <value>de.kba.zfer.core.util.beanvalidation.validator.SizeValidatorForStringDelegate</value>
        </validated-by>
    </constraint-definition>
   
</constraint-mappings>


I read in the logging, that hibernate validator founds and parses the file:
Quote:
2011-06-22 08:14:01,254 | INFO | ValidationXmlParser | - META-INF/validation.xml found. Parsing XML based configuration.


But I get an error during executing the validation, that there is no validator found for my custom type:
Quote:
javax.validation.UnexpectedTypeException: No validator could be found for type: de.kba.zfer.core.model.common.datatypes.FahrerlaubnisNummer


This is my validator:
Code:
package de.kba.zfer.core.util.beanvalidation.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.constraints.Size;

import de.kba.zfer.core.model.common.datatypes.FahrerlaubnisNummer;

public class SizeValidatorForStringDelegate implements ConstraintValidator<Size, FahrerlaubnisNummer> {

    private int min;

    private int max;

    @Override
    public void initialize(Size constraintAnnotation) {
        this.min = constraintAnnotation.min();
        this.max = constraintAnnotation.max();
        validateParameters();
    }

    @Override
    public boolean isValid(FahrerlaubnisNummer value, ConstraintValidatorContext context) {
        // sollte das gegebene StringDelegate-Object null sein, gib true zurueck
        if (value == null) {
            return true;
        }

        // ansonsten pruefe, ob die Laenge zwischen min und max liegt
        return value.length() >= min && value.length() <= max;
    }

    private void validateParameters() {
        if (min < 0) {
            throw new IllegalArgumentException("The min parameter cannot be negative.");
        }
        if (max < 0) {
            throw new IllegalArgumentException("The max parameter cannot be negative.");
        }
        if (max < min) {
            throw new IllegalArgumentException("The length cannot be negative.");
        }
    }
}


Can somebody tell me my mistake?

Thanks and kind regards,
Andreas


Top
 Profile  
 
 Post subject: Re: @Size or @Pattern for Custom User Types
PostPosted: Wed Jun 22, 2011 4:02 am 
Contributor
Contributor

Joined: Mon Feb 28, 2011 7:27 pm
Posts: 20
Location: France
Hi,

Look at http://docs.jboss.org/hibernate/validat ... figuration
Your mistake is that you haven't created a mapping file (I hope :). You have to create a validation.xml file with the following content which references your mapping file (with the content described in your previous post).

Code:
<?xml version="1.0" encoding="UTF-8"?>
<validation-config xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
                         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                         xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration">
    ...
    <constraint-mapping>yourMappingFile.xml</constraint-mapping>
    ...
</validation-config>

Quote:
It is planed, that this configuration can be done in annotation?

I don't think something like this is planned. My thinking is that we can consider to offer this possibility via a specific Hibernate Validator configuration or with the HV programmatic mapping API. Maybe this could be adressed by BV (I'm not sure that using an annotation would be the right way).

Hope this help

--Kevin


Top
 Profile  
 
 Post subject: Re: @Size or @Pattern for Custom User Types
PostPosted: Wed Jun 22, 2011 2:08 pm 
Newbie

Joined: Mon Mar 07, 2011 9:46 am
Posts: 10
Hi,

I have now created a validation.xml with the content of your last posting. And I placed a second XML-File with my configuration beside it. When I deploy my web-application, I got the error, that Hibernate cannot open an InputStream to my configuration file (I tried /filename.xml and filename.xml). I do not know, why the input stream cannot be opened, but it is so. How do I have to enter the file name within the validation.xml?

Thanks for your gread help and kind regards,
Andreas


Top
 Profile  
 
 Post subject: Re: @Size or @Pattern for Custom User Types
PostPosted: Wed Jun 22, 2011 3:20 pm 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
The name must identify a resource on your class path. If the mapping file is e.g. stored in META-INF next to validation.xml the name would be META-INF/filename.xml.

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: @Size or @Pattern for Custom User Types
PostPosted: Thu Jun 23, 2011 6:43 am 
Newbie

Joined: Mon Mar 07, 2011 9:46 am
Posts: 10
Hi Gunnar,

thanks for your help. Now it works fine. But I have still one question / one idea for improvement:
E.g. the SizeValidatorForString is defined as implements ConstraintValidator<Size, String>. If the String is not a String but a CharacterSequence, I had no need to implement my own Validators and xml-configuration, but just implement CharacterSequence on by datatype and everthing will work fine. Is it possible to think about this?

Thanks and kind regards,
Andreas


Top
 Profile  
 
 Post subject: Re: @Size or @Pattern for Custom User Types
PostPosted: Thu Jun 23, 2011 1:35 pm 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Of course it's possible to think about this :)

IMO this shouldn't be a big deal. Actually HV's latest character related constraint, @SafeHtml, already supports CharacterSequence.

Could you create a ticket for this at the HV issue tracker? It might also be an idea to consider this for Bean Validation 1.1, so could you create a ticket for this as well?

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.