diff --git a/_config/site.yml b/_config/site.yml index a863a24ec..ffff52c10 100644 --- a/_config/site.yml +++ b/_config/site.yml @@ -469,9 +469,11 @@ projects: html: https://docs.hibernate.org/validator/{series.version}/reference/en-US/html_single/#validator-gettingstarted pdf: https://docs.hibernate.org/validator/{series.version}/reference/en-US/pdf/hibernate_validator_reference.pdf#validator-gettingstarted migration_guide: - html: /validator/documentation/migration-guide/#{series.version.dashes}-x + html: https://docs.hibernate.org/validator/{series.version}/migration/en-US/html_single + pdf: https://docs.hibernate.org/validator/{series.version}/migration/en-US/pdf/hibernate_validator_migration_guide.pdf whats_new: - html: "/validator/releases/{series.version}/#whats-new" + html: https://docs.hibernate.org/validator/{series.version}/whats-new/en-US/html_single + pdf: https://docs.hibernate.org/validator/{series.version}/whats-new/en-US/pdf/hibernate_validator_whats_new.pdf dist: sourceforge: zip: https://sourceforge.net/projects/hibernate/files/hibernate-validator/{release.version}/hibernate-validator-{release.version}-dist.zip/download diff --git a/_data/projects/validator/releases/4.3/series.yml b/_data/projects/validator/releases/4.3/series.yml index 487f52575..2c095436d 100644 --- a/_data/projects/validator/releases/4.3/series.yml +++ b/_data/projects/validator/releases/4.3/series.yml @@ -1,4 +1,9 @@ summary: Last Bean Validation 1.0 compatible version +links: + whats_new: + html: "/validator/releases/{series.version}/#whats-new" + migration_guide: + html: /validator/documentation/migration-guide/#{series.version.dashes}-x maven: coord: group_id: org.hibernate diff --git a/_data/projects/validator/releases/5.0/series.yml b/_data/projects/validator/releases/5.0/series.yml index ac7861d73..08e1de7bb 100644 --- a/_data/projects/validator/releases/5.0/series.yml +++ b/_data/projects/validator/releases/5.0/series.yml @@ -1,4 +1,9 @@ summary: Reference implementation for Bean Validation 1.1 +links: + whats_new: + html: "/validator/releases/{series.version}/#whats-new" + migration_guide: + html: /validator/documentation/migration-guide/#{series.version.dashes}-x maven: coord: group_id: org.hibernate diff --git a/_data/projects/validator/releases/5.1/series.yml b/_data/projects/validator/releases/5.1/series.yml index 5d3c3717a..ad6d550e3 100644 --- a/_data/projects/validator/releases/5.1/series.yml +++ b/_data/projects/validator/releases/5.1/series.yml @@ -1,4 +1,9 @@ summary: Performances and memory footprint improvements +links: + whats_new: + html: "/validator/releases/{series.version}/#whats-new" + migration_guide: + html: /validator/documentation/migration-guide/#{series.version.dashes}-x maven: coord: group_id: org.hibernate diff --git a/_data/projects/validator/releases/5.2/series.yml b/_data/projects/validator/releases/5.2/series.yml index fe5c7b1a7..742195f66 100644 --- a/_data/projects/validator/releases/5.2/series.yml +++ b/_data/projects/validator/releases/5.2/series.yml @@ -1,4 +1,9 @@ summary: Java 8 support and more +links: + whats_new: + html: "/validator/releases/{series.version}/#whats-new" + migration_guide: + html: /validator/documentation/migration-guide/#{series.version.dashes}-x maven: coord: group_id: org.hibernate diff --git a/_data/projects/validator/releases/5.3/series.yml b/_data/projects/validator/releases/5.3/series.yml index 8109e3667..7a3ee87fc 100644 --- a/_data/projects/validator/releases/5.3/series.yml +++ b/_data/projects/validator/releases/5.3/series.yml @@ -1,4 +1,9 @@ summary: Dynamic constraint payload, parameter relaxation, new api for programmatic constraint definition, new translations. +links: + whats_new: + html: "/validator/releases/{series.version}/#whats-new" + migration_guide: + html: /validator/documentation/migration-guide/#{series.version.dashes}-x maven: coord: group_id: org.hibernate diff --git a/_data/projects/validator/releases/5.4/series.yml b/_data/projects/validator/releases/5.4/series.yml index 022349958..270e7d911 100644 --- a/_data/projects/validator/releases/5.4/series.yml +++ b/_data/projects/validator/releases/5.4/series.yml @@ -1,4 +1,9 @@ summary: JavaMoney support, annotation processor improvements. +links: + whats_new: + html: "/validator/releases/{series.version}/#whats-new" + migration_guide: + html: /validator/documentation/migration-guide/#{series.version.dashes}-x maven: coord: group_id: org.hibernate diff --git a/_data/projects/validator/releases/6.0/series.yml b/_data/projects/validator/releases/6.0/series.yml index 01981c960..d27b2826b 100644 --- a/_data/projects/validator/releases/6.0/series.yml +++ b/_data/projects/validator/releases/6.0/series.yml @@ -1,4 +1,9 @@ summary: Bean Validation 2.0 support, performance improvements. +links: + whats_new: + html: "/validator/releases/{series.version}/#whats-new" + migration_guide: + html: /validator/documentation/migration-guide/#{series.version.dashes}-x maven: artifacts: - artifact_id: hibernate-validator diff --git a/_data/projects/validator/releases/6.1/series.yml b/_data/projects/validator/releases/6.1/series.yml index bd89e1118..30aa3bb58 100644 --- a/_data/projects/validator/releases/6.1/series.yml +++ b/_data/projects/validator/releases/6.1/series.yml @@ -1,4 +1,9 @@ summary: Jakarta Bean Validation, new Quarkus-tailored bootstrap +links: + whats_new: + html: "/validator/releases/{series.version}/#whats-new" + migration_guide: + html: /validator/documentation/migration-guide/#{series.version.dashes}-x maven: repo: snapshots: jboss diff --git a/_data/projects/validator/releases/6.2/series.yml b/_data/projects/validator/releases/6.2/series.yml index b8a87e2b7..765edaae7 100644 --- a/_data/projects/validator/releases/6.2/series.yml +++ b/_data/projects/validator/releases/6.2/series.yml @@ -1,5 +1,10 @@ summary: Keep javax.* packages, Expression Language overhaul, @SafeHtml removal status: limited-support +links: + whats_new: + html: "/validator/releases/{series.version}/#whats-new" + migration_guide: + html: /validator/documentation/migration-guide/#{series.version.dashes}-x maven: artifacts: - artifact_id: hibernate-validator diff --git a/_data/projects/validator/releases/7.0/series.yml b/_data/projects/validator/releases/7.0/series.yml index ffaf46871..9c675d5ae 100644 --- a/_data/projects/validator/releases/7.0/series.yml +++ b/_data/projects/validator/releases/7.0/series.yml @@ -1,4 +1,9 @@ summary: Jakarta Bean Validation 3.0, jakarta.* packages +links: + whats_new: + html: "/validator/releases/{series.version}/#whats-new" + migration_guide: + html: /validator/documentation/migration-guide/#{series.version.dashes}-x maven: artifacts: - artifact_id: hibernate-validator diff --git a/_data/projects/validator/releases/8.0/series.yml b/_data/projects/validator/releases/8.0/series.yml index 42d45c3cd..31a9b73ad 100644 --- a/_data/projects/validator/releases/8.0/series.yml +++ b/_data/projects/validator/releases/8.0/series.yml @@ -1,4 +1,9 @@ summary: Jakarta EE 10, @UUID constraint +links: + whats_new: + html: "/validator/releases/{series.version}/#whats-new" + migration_guide: + html: /validator/documentation/migration-guide/#{series.version.dashes}-x maven: artifacts: - artifact_id: hibernate-validator diff --git a/_data/projects/validator/releases/9.0/series.yml b/_data/projects/validator/releases/9.0/series.yml index 8123b7064..7bc3bdee1 100644 --- a/_data/projects/validator/releases/9.0/series.yml +++ b/_data/projects/validator/releases/9.0/series.yml @@ -6,6 +6,11 @@ summary: >- BOM for simpler dependency management, dependency updates, improvements and bug fixes. +links: + whats_new: + html: "/validator/releases/{series.version}/#whats-new" + migration_guide: + html: /validator/documentation/migration-guide/#{series.version.dashes}-x maven: artifacts: - artifact_id: hibernate-validator-bom diff --git a/_data/projects/validator/releases/9.1/9.1.0.Final.yml b/_data/projects/validator/releases/9.1/9.1.0.Final.yml new file mode 100644 index 000000000..2b0254a8a --- /dev/null +++ b/_data/projects/validator/releases/9.1/9.1.0.Final.yml @@ -0,0 +1,4 @@ +date: 2025-11-07 +summary: >- + Misc build adjustments. +announcement_url: https://in.relation.to/2025/11/07/hibernate-validator-9-1-0-Final/ diff --git a/validator/documentation/index.adoc b/validator/documentation/index.adoc index 826eb8cc0..f0bc7a19b 100644 --- a/validator/documentation/index.adoc +++ b/validator/documentation/index.adoc @@ -13,8 +13,7 @@ Frequently asked questions. link:https://developer.jboss.org/en/hibernate/validator[JBoss-Developer wiki]:: Old wiki hosted at JBoss.org. Mostly out-of-date content. -== Maintainers +== Jakarta Validation -* link:releasing-hibernate-validator[Releasing Hibernate Validator] -* link:releasing-bean-validation-tck[Releasing Bean Validation TCK] -* link:releasing-bean-validation-api-spec[Releasing Bean Validation API and spec] +* https://jakarta.ee/specifications/bean-validation/[Jakarta Validation Specifications] +* https://beanvalidation.org/[beanvalidation.org] diff --git a/validator/documentation/migrate/index.adoc b/validator/documentation/migrate/index.adoc new file mode 100644 index 000000000..58927447e --- /dev/null +++ b/validator/documentation/migrate/index.adoc @@ -0,0 +1,4 @@ += Migration guides +:awestruct-layout: project-migrate +:awestruct-project: validator +:page-interpolate: true diff --git a/validator/documentation/releasing-bean-validation-api-spec.adoc b/validator/documentation/releasing-bean-validation-api-spec.adoc index 823b42a52..85d103c9e 100644 --- a/validator/documentation/releasing-bean-validation-api-spec.adoc +++ b/validator/documentation/releasing-bean-validation-api-spec.adoc @@ -4,54 +4,4 @@ Follow these steps for an official release. -WARNING: Bean Validation has been moved to Jakarta EE thus this release process needs an update. - -== Prerequisites - -* You need to have access to http://ci.hibernate.org/: the release process is now driven by CI -* Follow the order of operations - the API has to be released before the specification - -== Prepare - -* Make sure there are no outstanding issues in https://hibernate.atlassian.net/projects/BVAL/issues[Jira] -* Release the https://hibernate.atlassian.net/projects/BVAL?selectedItem=com.atlassian.jira.jira-projects-plugin%3Arelease-page&status=released-unreleased[Jira version] - -== Release beanvalidation-api - -=== Prepare - -* Check that everything has been pushed to the upstream repository -* Check that CI is green for the branch you want to release -* Before you release try the latest snapshot of the API with the RI (if the RI is up to date with the API) - -=== Release - -* Go to CI, and select http://ci.hibernate.org/view/Validator/job/beanvalidation-api-release/[the beanvalidation-api-release job]. Click on _Build with parameters_. -** *Be careful when filling the form with the build parameters* -** *Fill in the DELIVERY_QUALIFIER if the release is an official JCP release step (use - if not)* -* Release the artifacts on the JBoss Nexus repository manager. This is a two-steps process. -** Log into https://repository.jboss.org/nexus/index.html[Nexus] and examine https://repository.jboss.org/nexus/index.html#stagingRepositories[your staging repository]. Click on the _Updated_ column 2 times to have the repositories sorted correctly. -** You can either drop it in order to start over again or you can close and promote it making the artifacts available in the live repository. (Click _Refresh_ after each action) -** You can find more information in the description of https://developer.jboss.org/docs/DOC-14608[the JBoss project release process]. A Maven settings.xml for testing the artifacts in the staging repository can be found https://developer.jboss.org/docs/DOC-15664[here]. -* Transfer the released issues in JIRA to the _Closed_ state. You can use this search https://hibernate.atlassian.net/browse/BVAL-560?jql=project%20%3D%20BVAL%20AND%20fixVersion%20%3D%202.0.0.Alpha2%20ORDER%20BY%20priority%20DESC%2C%20updated%20DESC[Issue Navigator - Hibernate JIRA] and select another `fixVersion`. Once you have selected the right version, click on the button with three dots at the top right of the screen and click on _Bulk update all XX issues_. Use the _Transition_ action to transition your issues from _Resolved_ to _Closed_. - -== Release beanvalidation-spec - -=== Prepare - -* Check that everything has been pushed to the upstream repository -* Check that you are able to build the TCK audit file via the command ant create-tck-audit-file (a section id might be missing) -* Check that CI is green for the branch you want to release -* *The changelog of the spec is now updated automatically: do not update it!* - -=== Release - -* Go to CI, and select http://ci.hibernate.org/view/Validator/job/beanvalidation-spec-release/[the beanvalidation-spec-release job]. Click on _Build with parameters_. -** *Be careful when filling the form with the build parameters.* -** *Fill in the RELEASE_VERSION_QUALIFIER if the release is an official JCP release step* -** *Fill in the DELIVERY_QUALIFIER if the release is an official JCP release step (use - if not)* -* The release job pushes the specification to the website repository but there are still some manual steps: -** Update the home page: http://beanvalidation.org/[Bean Validation - Home] -** Update the specification page: http://beanvalidation.org/specification/[Bean Validation - Specification] -** Once the spec is online (ie the website job triggered by the release job is finished), you can generate the HTML diffs using: https://www.changedetection.com/comparepages.html[ChangeDetection - Compare Web Pages] - +WARNING: Bean Validation has been moved to Jakarta EE thus, see https://github.com/jakartaee/validation/blob/main/MAINTAINERS.md[MAINTAINERS.md] for release process. diff --git a/validator/documentation/releasing-bean-validation-tck.adoc b/validator/documentation/releasing-bean-validation-tck.adoc index 46f541b2c..d826c250a 100644 --- a/validator/documentation/releasing-bean-validation-tck.adoc +++ b/validator/documentation/releasing-bean-validation-tck.adoc @@ -4,35 +4,4 @@ Follow these steps for an official release. -WARNING: Bean Validation has been moved to Jakarta EE thus this release process needs an update. - -== Prerequisites - -* You need to have access to http://ci.hibernate.org/: the release process is now driven by CI - -== Prepare - -* Update the TCK audit file (`tests/src/main/resources/tck-audit.xml`) as per the latest spec revision and check that it was identical to the preexisting file. If not, you will probably need to update the TCK assertion ids accordingly. -* Check that everything has been pushed to the upstream repository -* Check that CI is green -* Check that the BV and HV versions referenced in the parent pom.xml are alright (non SNAPSHOT and pointing to the right release) -* Before you release, deploy the TCK and run the Validator tests with this version of the TCK to check everything is OK (run the TCK in container mode) -* Make sure there are no outstanding issues in https://hibernate.atlassian.net/browse/BVTCK[Jira] - -== Release - -* Release the https://hibernate.atlassian.net/plugins/servlet/project-config/BVTCK/versions[Jira version] -* Do NOT update the `changelog.txt` and `README.md`, the release job does it for you. -* Go to CI, and select the beanvalidation-tck-release job. Click on Build with parameters. -** *Be careful when filling the form with the build parameters.* -* Release the artifacts on the JBoss Nexus repository manager. This is a two-steps process. -** Log into https://repository.jboss.org/nexus/index.html[Nexus] and examine https://repository.jboss.org/nexus/index.html#stagingRepositories[your staging repository]. Click on the _Updated_ column 2 times to have the repositories sorted correctly. -** You can either drop it in order to start over again or you can close and promote it making the artifacts available in the live repository. (Click _Refresh_ after each action) -** You can find more information in the description of https://developer.jboss.org/docs/DOC-14608[the JBoss project release process]. A Maven settings.xml for testing the artifacts in the staging repository can be found https://developer.jboss.org/docs/DOC-15664[here]. -* Transfer the released issues in JIRA to the _Closed_ state. You can use this search https://hibernate.atlassian.net/issues/?jql=project%20%3D%20BVTCK%20AND%20status%20%3D%20Resolved%20AND%20fixVersion%20%3D%202.0.0.Alpha1[Issue Navigator - Hibernate JIRA] and select another `fixVersion`. Once you have selected the right version, click on the button with three dots at the top right of the screen and click on _Bulk update all XX issues_. Use the _Transition_ action to transition your issues from _Resolved_ to _Closed_. - -== Publish - -NOTE: In the HTML version of the reference guide, some special characters and the section titles will show up not correctly when opening the file locally. This is a known issue, the documentation looks correct when retrieved via Apache. - -The CI job automatically pushes the distribution to SourceForge and publishes the documentation to docs.jboss.org. +WARNING: Bean Validation has been moved to Jakarta EE thus, see https://github.com/jakartaee/validation/blob/main/MAINTAINERS.md[MAINTAINERS.md] for release process. diff --git a/validator/documentation/releasing-hibernate-validator.adoc b/validator/documentation/releasing-hibernate-validator.adoc index 369a706a9..58a33858d 100644 --- a/validator/documentation/releasing-hibernate-validator.adoc +++ b/validator/documentation/releasing-hibernate-validator.adoc @@ -2,54 +2,4 @@ :awestruct-layout: project-standard :awestruct-project: validator -== Release Process - -Follow these steps for an official release. - -=== Prerequisites - -You need to have access to https://ci.hibernate.org/: the release process is now driven by CI. - -=== Prepare - -* Update the version of validation-api and tck in `pom.xml` if need be -* Update the version of tck in `pom.xml` if need be -* Check that everything has been pushed to the upstream repository -* Check that CI is green for the branch you want to release -* Before you release try the latest snapshot of Validator in Core to make sure the snapshot works in Hibernate ORM. -** Use the HV snapshot version in https://github.com/hibernate/hibernate-orm/blob/master/gradle/libraries.gradle[libraries.gradle] -** Run either the full ORM build or hibernate-core and hibernate-entitymanager -** A `./gradlew test` in the ORM directory should be sufficient -* Double check that `distribution/pom.xml` and `distribution/src/main/assembly/dist.xml` list and include all the right dependencies (this is especially important when dependencies changed). -* Make sure there are no outstanding issues in https://hibernate.atlassian.net/browse/HV[Jira] -* On https://hibernate.atlassian.net/issues/?jql=project%20%3D%20HV%20AND%20fixVersion%20%3D%206.0.0.Alpha1%20AND%20component%20%3D%20EMPTY%20ORDER%20BY%20priority%20DESC%2C%20updated%20DESC[Jira (adjust the fixVersion)] check that all the issues referenced in this version have a Component set - -=== Release - -* Release https://hibernate.atlassian.net/projects/HV?selectedItem=com.atlassian.jira.jira-projects-plugin%3Arelease-page&status=released-unreleased[the Jira version] -* Do *NOT* update the changelog.txt and README.md, the release job does it for you. -* Go to CI, and select https://ci.hibernate.org/view/Validator/job/hibernate-validator-release/[the hibernate-validator-release job]. Click on Build with parameters. -** *Be careful when filling the form with the build parameters.* -* Release the artifacts on the OSSRH repository manager. This is a two-steps process. -** Log into https://oss.sonatype.org/[OSSRH] and examine your https://oss.sonatype.org/#stagingRepositories[staging repository]. -** You can either `Drop` it in order to start over again or you can `Close` and `Release` it making the artifacts available in the live repository. (Click `Refresh` after each action) -* Transfer the released issues in JIRA to the Closed state. You can use this search https://hibernate.atlassian.net/issues/?jql=project%20%3D%20HV%20AND%20status%20%3D%20Resolved%20AND%20fixVersion%20%3D%205.3.0.CR1[Issue Navigator - Hibernate JIRA] and select another fixVersion. Once you have selected the right version, click on the button with three dots at the top right of the screen and click on Bulk update all XX issues. Use the Transition action to transition your issues from Resolved to Closed. - -=== Publish - -NOTE: In the HTML version of the reference guide, some special characters and the section titles will show up not correctly when opening the file locally. This is a known issue, the documentation looks correct when retrieved via Apache. - -The CI job automatically pushes the distribution to SourceForge and publishes the documentation to docs.jboss.org. - -=== Announce - -* Update the migration guide: https://github.com/hibernate/hibernate.org/blob/production/validator/documentation/migration-guide.adoc -* Blog about the release on in.relation.to. Add the tag 'Hibernate Validator' to the post so it will also appear on the Hibernate Validator project page. -* Update https://github.com/hibernate/hibernate.org[hibernate.org]: -** If it is a new major release, add a `_data/projects/validator/releases/series.yml` file and a `validator/releases//index.adoc` file -** Add a new release file to `_data/projects/validator/releases` -* Depending on which series you want to have displayed, make sure to adjust the displayed flag of the `series.yml` file of the old series -* Deploy to production -* Send an email to mailto:hibernate-dev@lists.jboss.org[hibernate-dev] and mailto:hibernate-announce@lists.jboss.org[hibernate-announce] -* Twitter via https://twitter.com/Hibernate[Hibernate] user - +WARNING: The maintainer's guide is moved to the project sources: https://github.com/hibernate/hibernate-validator/blob/main/MAINTAINERS.md[MAINTAINERS.md] diff --git a/validator/releases/9.1/index.adoc b/validator/releases/9.1/index.adoc index 4a5a088b7..9fb74f086 100644 --- a/validator/releases/9.1/index.adoc +++ b/validator/releases/9.1/index.adoc @@ -1,173 +1,3 @@ :awestruct-layout: project-releases-series :awestruct-project: validator :awestruct-series_version: "9.1" - -**** -[discrete] -=== Jakarta EE 11 - -Hibernate Validator 9.1 continues to target Jakarta Validation 3.1 and requires a minimum Java version of 17. - -[discrete] -=== Performance improvements - -In this version, we have changed the `jakarta.validation.Path` implementation, -approach to processed bean tracking and slightly modified the message interpolation to improve both overall performance -and performance of cascading validation when beans require a lot of cascading operations. -We plan to publish a more detailed report with benchmark results, so stay tuned if you are interested in the details and numbers. - -[discrete] -=== Deprecating the use of `@Valid` at the container level - -Since Bean Validation 2.0, which introduced type argument constraints, -the approach of placing the `@Valid` annotation at the container level to apply cascading validation to container elements -has been considered a "legacy" approach. -Users have been encouraged to move their constraints to the type argument level. - -.Legacy approach to cascading validation for container elements -==== -[source, Java, indent=0, subs="+attributes"] ----- -class MyBean { - @Valid <1> - List list; -} ----- -<1> Cascading validation is requested at the container level, -whereas the expectation is that container elements will be considered for cascading, not the container itself. -==== - -.Current approach to cascading validation for container elements -==== -[source, Java, indent=0, subs="+attributes"] ----- -class MyBean { - List<@Valid MyContainerElement> list; <1> -} ----- -<1> Cascading validation is requested at the container element level. -This clearly communicates the intention that elements are expected to be cascaded into and not the container itself. -==== - -Starting with 9.1, Hibernate Validator will produce warnings during the metadata building step -if it detects the legacy approach to cascading validation of container elements. -In the future version, this support of Bean Validation 1.0/1.1 legacy behaviour will be dropped. -Placing the `@Valid` at the container level would result in cascading into the container itself rather than into its elements. - -[NOTE] -==== -There is still some work ahead of us before we can turn off this legacy behaviour. -For example, we must address the case where the container does not have type arguments -but still requires cascading into its elements. -This is currently tracked at the Jakarta Validation specification level through the following link:https://github.com/jakartaee/validation/issues/266[issue]. - -We encourage users to review and update their validation mapping where necessary. -Additionally, if you encounter a particular use case that you believe is not covered, please don't hesitate to contact us. -==== - -[discrete] -[[hibernate-path]] -=== Extended validation path - -This version introduces another extension of the `jakarta.validation.Path`: `org.hibernate.validator.path.RandomAccessPath`. -There are scenarios where the first couple of nodes have to be inspected to determine how to process the constraint violation. -For cases when the path is represented by an array or some other collection that allows easy random access to the nodes, -it would be simple enough to expose the access to the nodes by index: - -.Random access to the path nodes -==== -[source, Java, indent=0, subs="+attributes"] ----- -Path path = constraintViolation.getPropertyPath(); -if ( path instanceof org.hibernate.validator.path.RandomAccessPath hvPath ) { - Node rootNode = hvPath.getRootNode(); - // ... - int index = ... - Node someNode = hvPath.getNode(index); - // ... - for(int i; i < hvPath.length(); i++) { - hvPath.getNode(i); - } -} ----- -==== - -[discrete] -[[initialization-data]] -=== Constraint initialization shared data - -Constraint initialization shared data opens up a way for constraint validators to access a shared instance within the `initialize(..)`. -This can be used to cache and reuse elements required to construct a constraint validator. For example, -internally, this mechanism is used by the pattern constraint validator to reuse the `java.util.regex.Pattern` instances - -.Accessing the constraint validator's lazy initialization shared data in a constraint validator -==== -[source, JAVA, indent=0] ----- -public class ParsableDateTimeFormatValidator - implements HibernateConstraintValidator { // <1> - - private DateTimeFormatter formatter; - - @Override - public void initialize(ConstraintDescriptor constraintDescriptor, - HibernateConstraintValidatorInitializationContext initializationContext) { - formatter = initializationContext.getSharedData( DateTimeFormatterCache.class, DateTimeFormatterCache::new ) // <2> - .get( constraintDescriptor.getAnnotation().dateFormat() ); // <3> - } - - @Override - public boolean isValid(String dateTime, ConstraintValidatorContext constraintContext) { - if ( dateTime == null ) { - return true; - } - - try { - formatter.parse( dateTime ); - } - catch (DateTimeParseException e) { - return false; - } - return true; - } - - private static class DateTimeFormatterCache { // <4> - private final Map cache = new ConcurrentHashMap<>(); - - DateTimeFormatter get(String format) { - return cache.computeIfAbsent( format, DateTimeFormatter::ofPattern ); - } - } -} ----- -<1> Implement the Hibernate Validator `HibernateConstraintValidator` extension to have access to the initialization context. -<2> Retrieve the shared data from the initialization context, providing the supplier that will be executed -if the `DateTimeFormatterCache` is not yet available in the current initialization context. -<3> Perform some actions with the shared data instance. -<4> A simple wrapper around the map to cache the formatters. -Compared to the use of a static field cache, using the shared data has the benefit that it is tied to the initialization context -and will be garbage collected along with it. -==== - -[discrete] -[[ip-address-constraint]] -=== `IpAddress` constraint - -The new `@IpAddress` constraint validates that the corresponding string is a well-formed IP address. -This constraint provides a `IpAddress.Type` enum with the IP address types it can validate: `IPv4`, `IPv6` or `ANY`. -By default, `IpAddress.ANY` is used, which allows validating all the other address types listed in the `IpAddress.Type` enum. - -==== -[source, Java, indent=0, subs="+attributes"] ----- -@IpAddress <1> -String address; -// ... -@IpAddress(type = Type.IPv6) <2> -private String address; ----- -<1> Using a default configuration of the `@IpAddress` constraint, -where both `IPv4` and `IPv6` address types are considered valid. -<2> Applying the `@IpAddress` constraint, where only the `IPv6` addresses are considered valid. -==== -****