Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Validate a string type against a list of dynamic values
PostPosted: Tue Aug 15, 2017 6:33 pm 
Regular
Regular

Joined: Mon Aug 07, 2006 6:22 pm
Posts: 66
I've had Hibernate Validator working in my API framework for a while now, and it works well enough for comparing an instance value against "static" constraints.

Someone on my team has an instance variable whose legal values are defined by a list of values obtained from a properties file. This doesn't fit with what's available here. For now, I've told him to just do the manual validation at the point where we do the "simple" validation, and just cause it to manually construct a "ConstraintViolation" object (and subclass, being that it's an interface) and add it to any list that the simple validation produces.

I'd like to know what other people have done in this situation, as I can't imagine this is a rare thing to do.


Top
 Profile  
 
 Post subject: Re: Validate a string type against a list of dynamic values
PostPosted: Wed Aug 16, 2017 9:23 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 385
Hi, you could have a string constraint whose validator implementation reads a whitelist with allowed values from a text file. E.g. like so:

Code:
@Target({ FIELD, METHOD, PARAMETER, ANNOTATION_TYPE, TYPE_USE })
@Retention(RUNTIME)
@Constraint(validatedBy = InWhitelistValidator.class)
@Documented
@Repeatable(List.class)
public @interface InWhitelist {

    String message() default "{com.example.InWhitelist.message}";
    Class<?>[] groups() default { };
    Class<? extends Payload>[] payload() default { };

    String whitelistFile();

    @Target({ FIELD, METHOD, PARAMETER, ANNOTATION_TYPE })
    @Retention(RUNTIME)
    @Documented
    @interface List {
        InWhitelist[] value();
    }
}


Code:
public class InWhitelistValidator implements ConstraintValidator<InWhitelist, String> {

    private Set<String> whitelist;

    @Override
    public void initialize(InWhitelist constraintAnnotation) {
        this.whitelist = readFile( constraintAnnotation.whitelistFile() ).toSet(); // read contents of given whitelist file
    }

    @Override
    public boolean isValid(String object, ConstraintValidatorContext constraintContext) {
        if ( object == null ) {
            return true;
        }

        return whitelist.contains( object );
    }
}


You then can create constraints for specific "enums" via constraint composition (http://docs.jboss.org/hibernate/stable/ ... omposition):

Code:
@Target({ FIELD, METHOD, PARAMETER, ANNOTATION_TYPE, TYPE_USE })
@Retention(RUNTIME)
@Constraint(validatedBy = {})
@Documented
@InWhitelist(whitelistFile="currency-codes.txt")
@Repeatable(List.class)
public @interface ValidCurrencyCode {

    String message() default "{com.example.ValidCurrencyCode.message}";
    Class<?>[] groups() default { };
    Class<? extends Payload>[] payload() default { };

    @Target({ FIELD, METHOD, PARAMETER, ANNOTATION_TYPE })
    @Retention(RUNTIME)
    @Documented
    @interface List {
        ValidCurrencyCode[] value();
    }
}


Hth,

--Gunnar

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 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.