{"id":22613044,"url":"https://github.com/seahrh/bean-validation-example","last_synced_at":"2026-03-18T02:02:24.034Z","repository":{"id":75891833,"uuid":"159083906","full_name":"seahrh/bean-validation-example","owner":"seahrh","description":"Learn how to use the Bean Validation API 2.0 by testing it!","archived":false,"fork":false,"pushed_at":"2018-12-29T03:19:05.000Z","size":102,"stargazers_count":5,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-11T10:16:59.835Z","etag":null,"topics":["bean-validation","hibernate","java","object-oriented","validation"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/seahrh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-11-25T23:19:19.000Z","updated_at":"2024-10-29T09:07:29.000Z","dependencies_parsed_at":"2023-07-12T00:01:08.052Z","dependency_job_id":null,"html_url":"https://github.com/seahrh/bean-validation-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/seahrh/bean-validation-example","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seahrh%2Fbean-validation-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seahrh%2Fbean-validation-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seahrh%2Fbean-validation-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seahrh%2Fbean-validation-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/seahrh","download_url":"https://codeload.github.com/seahrh/bean-validation-example/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seahrh%2Fbean-validation-example/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30641711,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-18T01:41:58.583Z","status":"online","status_checked_at":"2026-03-18T02:00:07.824Z","response_time":104,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bean-validation","hibernate","java","object-oriented","validation"],"created_at":"2024-12-08T17:14:53.035Z","updated_at":"2026-03-18T02:02:24.029Z","avatar_url":"https://github.com/seahrh.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# bean-validation-example\n\n[![Build Status](https://travis-ci.org/seahrh/bean-validation-example.svg?branch=master)](https://travis-ci.org/seahrh/bean-validation-example)\n\nThe Bean Validation API allows code reuse of validators through annotations. Validators pass constraint violations to the application so that it can decide error handling. Despite its name, the Bean Validation API does **not** require the use of JavaBeans or Java EE.\n\n## Goals\nLearn how to use the Bean Validation API 2.0\n- What are the features and limitations?\n- Add tests to verify behaviour not covered in the original examples from Hibernate Validator's reference guide\u003csup\u003e[1](#1)\u003c/sup\u003e\n- Find out the minimal dependency set required\n\n## Prerequisites\n- JDK 8\n- Apache Maven\n\n### Maven Dependencies\nTwo dependencies required.\n\n1. Hibernate Validator - implementation of the Bean Validation API\n\n    `pom.xml`\n    ```xml\n    \u003cdependency\u003e\n        \u003cgroupId\u003eorg.hibernate.validator\u003c/groupId\u003e\n        \u003cartifactId\u003ehibernate-validator\u003c/artifactId\u003e\n        \u003cversion\u003e6.0.13.Final\u003c/version\u003e\n    \u003c/dependency\u003e\n    ```\n1. Unified EL - implementation of the Unified Expression Language (JSR 341) for evaluating dynamic expressions in constraint violation messages. Not required if the application is running in a Java EE container.\n\n    `pom.xml`\n    ```xml\n    \u003cdependency\u003e\n        \u003cgroupId\u003eorg.glassfish\u003c/groupId\u003e\n        \u003cartifactId\u003ejavax.el\u003c/artifactId\u003e\n        \u003cversion\u003e3.0.1-b09\u003c/version\u003e\n    \u003c/dependency\u003e\n    ```\n## Evaluation\nMy personal opinion of Hibernate Validator: TLDR; yes I will definitely use it in my next Java project!\n### Features\n1. Out of the box, constraints can be declared on the following via Java annotations\n    1. [Fields](https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#_field_level_constraints)\n    1. [Properties](https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#_property_level_constraints) i.e. getter methods, following the JavaBean standard\n    1. Values in [standard Java containers](https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#container-element-constraints) like `java.util.Iterable`, `java.util.Map` and `java.util.Optional`\n        1. Also works on nested containers like `Map\u003c@NotNull Part, List\u003c@NotNull Manufacturer\u003e\u003e`\n        1. Custom containers require additional work by writing a `ValueExtractor`\n    1. Validating the [entire class](https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#validator-usingvalidator-classlevel), useful for interactions among fields e.g. the seating capacity of a car and number of passengers\n    1. [Inheriting contraints](https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-constraint-inheritance) from the superclass\n    1. Cascading validation through the [object graph](https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-object-graph-validation) (when an object holds a reference to another object)\n        1. Also works for containers\n        1. `null` is ignored during cascaded validation\n        1.  The validation engine ensures that no infinite loops occur during cascaded validation, for example if two objects hold references to each other.\n1. Validators can check the entire object or [parts of it](https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#_validator_methods) e.g. a single field\n1. List of [built-in constraints](https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-builtin-constraints)\n    1. Common business objects like `@Email`, `@CreditCardNumber`, `@Currency`\n    1. Web: `@URL`, `@SafeHtml`\n    1. Time: `@PastOrPresent`, `@Future`\n1. Write your own [custom validator](https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#validator-customconstraints) or [compose](https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-constraint-composition) a new one from existing constraints\n    1. By default, constraint composition is logical `AND`. It is also possible to configure logical [`OR` and `ALL FALSE`](https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-boolean-constraint-composition)\n1. [Group constraints](https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#chapter-groups) to check in stages\n    1. This mimics business processes/object lifecycles that require different validation logic in each stage\n    1. e.g. driving off a new car at the showroom\n        1. car must have passed vehicle inspection\n        1. driver must have a license\n1. Set [dynamic error messages](https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#chapter-message-interpolation)\n1. Dynamic payload\n    1. [Pass a payload to the constraint](https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#validator-customconstraints-constraintannotation), so as to set the control flow of the validation\n        1. e.g. a custom constraint `@CheckCase` can have two different payloads `@CheckCase(UPPER)` or `@CheckCase(LOWER)`\n    1. [As part of `ConstraintViolation`](https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-dynamic-payload)\n        1. Customise error messages e.g. suggest valid values\n        1. Provide hints to the application on how to handle the error e.g. error severity\n1. For reproducible testing, [define what is the time now](https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-clock-provider) for time-related validation (such as `@Past` or `@Future` constraints)\n1. By default, validation reports all the errors found instead of stopping at the first error. Optionally the validator can be configured to [fail fast](https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-fail-fast) e.g. expensive validation of large object graphs\n\n### Limitations\n1. Do not recommend use of [method constraints](https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-declaring-method-constraints)\n    1. Requires an additional library that has a method interception facility, like Spring AOP\n    1. Can only used for instance methods (static methods are not supported)\n\n## References\n\u003ca name=\"1\"\u003e[1]:\u003c/a\u003e Hibernate Validator [reference guide](https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/) and [source code](https://github.com/hibernate/hibernate-validator/tree/master/documentation/src/test/java/org/hibernate/validator/referenceguide)\n\n\u003ca name=\"2\"\u003e[2]:\u003c/a\u003e [Baeldung examples of Bean Validation API](https://github.com/eugenp/tutorials/tree/master/javaxval/src/main/java/org/baeldung/javaxval/methodvalidation)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseahrh%2Fbean-validation-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fseahrh%2Fbean-validation-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseahrh%2Fbean-validation-example/lists"}