-->
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.  [ 2 posts ] 
Author Message
 Post subject: Question: Validating indivddual values in a map based on key
PostPosted: Thu Oct 01, 2015 12:16 pm 
Newbie

Joined: Thu Oct 01, 2015 12:07 pm
Posts: 2
I have map with key being of String type and value of Object type (Map<String, Object>). I want to be able to validate the value of the object given based on what key is provided. For example:

If I have a map:

{ "foo" : Object}

I need the value of a key foo in this map to be of Integer type. Can I do this using hibernate validator? if so how would I be able to do this?


Top
 Profile  
 
 Post subject: Re: Question: Validating indivddual values in a map based on key
PostPosted: Fri Oct 02, 2015 3:16 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Hi, you could implement a custom constraint for that (see http://docs.jboss.org/hibernate/validator/5.2/reference/en-US/html_single/#validator-customconstraints). E.g. something like that:

Code:
@MapValueTypes({
    @ValueType(key="foo", type=Integer.class),
    @ValueType(key="bar", type=Long.class)
})
private Map<String, Object> myMap;


But if your code relies on specific map values and their type, chances are you are better off with a dedicated class modeling that structure rather than using a map.

--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.  [ 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.