Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9288981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:02:52+00:00 2026-06-18T20:02:52+00:00

I am using Hibernate Validator (HV) as my JSR 303 validator. However, both in

  • 0

I am using Hibernate Validator (HV) as my JSR 303 validator. However, both in JSR 303 and in HV’s extensions, there does not seem to be any annotation to specify constraints like a key must exist in a Map or only validate the Map value corresponding to a key.

My use case is that some map keys are required to have valid values only if some other property of the bean is set to true. Currently, I used a fake getter and a @AssertTrue annotation, but I’d really like to make this more annotation based.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T20:02:53+00:00Added an answer on June 18, 2026 at 8:02 pm

    There does not seem to be any annotation to specify constraints like a key must exist in a Map.

    This can be solved by implementing a custom constraint:

    @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
    @Retention(RUNTIME)
    @Documented
    @Constraint(validatedBy = { ContainsKeyValidator.class })
    public @interface ContainsKeys {
        String message() default "{com.acme.ContainsKey.message}";
        Class<?>[] groups() default { };
        Class<? extends Payload>[] payload() default { };
        String[] value() default {};
    }
    

    And a validator like this:

    public class ContainsKeysValidator implements ConstraintValidator<ContainsKeys, Map<?, ?>> {
    
        String[] requiredKeys;
    
        @Override
        public void initialize(ContainsKeys constraintAnnotation) {
            requiredKeys = constraintAnnotation.values();
        }
    
        @Override
        public boolean isValid(Map<?, ?> value, ConstraintValidatorContext context) {
            if( value == null ) {
                return true;
            }
    
            for( String requiredKey : requiredKeys ) {
                if( value.containsKey( requiredKey ) ) {
                    return false;
                }
            }
    
            return true;
        }
    }
    

    My use case is that some map keys are required to have valid values only if some other property of the bean is set to true.

    For this requirement you could implement a class-level constraint, which allows to access the complete bean with all its attributes in the validator implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using hibernate validator to validate a form. However the validator does not
I'm looking into using JSR-303 with hibernate validator. We would like to be able
I am using @NotEmpty annotation from Hibernate validator. However I am required to provide
I'm using Hibernate validator implementation of JSR 303 to validate some entities in my
I was using JSR 303 validation with hibernate validator to have fields validated automatically
I'm using Hibernate Validator. I have a Class level annotation. It compares three properties
I am using org.hibernate.validator.Pattern annotation in my jsf managed bean to validate an <h:inputText>
We all know, that Spring MVC integrate well with Hibernate Validator and JSR-303 in
I am validation my beans using hibernate validations (JSR 303).All my validations are working
I'm using Hibernate Validator 4.0.2, Spring 3.0 and Hibernate 3.3.2 (which, as I understand

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.