Skip to content

Commit

Permalink
Merge pull request #214 from xdev-software/jakarta.validation.constra…
Browse files Browse the repository at this point in the history
…ints

Jakarta.validation.constraints
  • Loading branch information
JohannesRabauer authored Dec 20, 2024
2 parents 4f1cc86 + 78a6794 commit 072879a
Show file tree
Hide file tree
Showing 8 changed files with 471 additions and 19 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

* Auto-Fix problems with adding ids to entities with existing data store.

~~~~
# 2.3.0

* Add support for shutting down the storage during application shutdown
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
** xref:features/transactions.adoc[Transactions]
** xref:features/versions.adoc[Versions]
** xref:features/rest-api.adoc[REST Interface]
** xref:features/validation-constraints.adoc[Validation Constraints]
* xref:migration.adoc[Migration from JPA]
* xref:known-issues.adoc[Known issues]
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/features/features.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
* xref:features/transactions.adoc[Transactions]
* xref:features/versions.adoc[Versions]
* xref:features/rest-api.adoc[REST Interface]
* xref:features/validation-constraints.adoc[Validation Constraints]
19 changes: 19 additions & 0 deletions docs/modules/ROOT/pages/features/validation-constraints.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
= Validation Constraints

By using the https://jakarta.ee/learn/docs/jakartaee-tutorial/current/beanvalidation/bean-validation/bean-validation.html[Jakarta Bean Validation Constraints] developers with {product-name} can easily limit the allowed input of entities.
Here is a full list of supported validations: https://jakarta.ee/learn/docs/jakartaee-tutorial/current/beanvalidation/bean-validation/bean-validation.html#_using_jakarta_bean_validation_constraints[https://jakarta.ee/learn]
[source,java,title="https://github.com/xdev-software/spring-data-eclipse-store/blob/develop/spring-data-eclipse-store-demo/src/main/java/software/xdev/spring/data/eclipse/store/demo/complex/model/Person.java[Example from complex demo]"]
----
package software.xdev.spring.data.eclipse.store.demo.complex.model;
import jakarta.validation.constraints.NotBlank;
public class Person extends BaseEntity
{
@NotBlank
private String firstName;
//...
----
The ``jakarta.validation.Validator`` is provided by the https://github.com/xdev-software/spring-data-eclipse-store/tree/develop/spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/config/EclipseStoreClientConfiguration.java[``EclipseStoreClientConfiguration``] and can be changed in the project-specific configuration.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
*/
package software.xdev.spring.data.eclipse.store.demo.complex.model;

import jakarta.validation.constraints.NotBlank;


public class Person extends BaseEntity
{
@NotBlank
private String firstName;

private String lastName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;

import org.eclipse.serializer.reflect.ClassLoaderProvider;
import org.eclipse.store.integrations.spring.boot.types.configuration.EclipseStoreProperties;
Expand Down Expand Up @@ -206,6 +207,9 @@ public void shutdownStorageOnContextClosed(final ContextClosedEvent event)
@Bean
public Validator getValidator()
{
return Validation.buildDefaultValidatorFactory().getValidator();
try(ValidatorFactory factory = Validation.buildDefaultValidatorFactory())
{
return factory.getValidator();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class ConstraintDaoObject
@DecimalMin("5.00")
BigDecimal discountMin5;

@DecimalMax("30.00")
@DecimalMax("20.00")
BigDecimal discountMax20;

@Email
Expand Down Expand Up @@ -116,8 +116,8 @@ public class ConstraintDaoObject
@PositiveOrZero
int positiveOrZeroField;

@Size(min = 2, max = 240)
String messageMin2AndMax240;
@Size(min = 2, max = 10)
String messageMin2AndMax10;

public ConstraintDaoObject()
{
Expand All @@ -141,7 +141,7 @@ public ConstraintDaoObject()
this.phoneNumber = "(123)456-7890";
this.area = BigDecimal.valueOf(1);
this.positiveOrZeroField = 1;
this.messageMin2AndMax240 = "..";
this.messageMin2AndMax10 = "..";
}

@AssertFalse
Expand Down Expand Up @@ -186,12 +186,12 @@ public void setDiscountMin5(final @DecimalMin("5.00") BigDecimal discountMin5)
this.discountMin5 = discountMin5;
}

public @DecimalMax("30.00") BigDecimal getDiscountMax20()
public @DecimalMax("20.00") BigDecimal getDiscountMax20()
{
return this.discountMax20;
}

public void setDiscountMax20(final @DecimalMax("30.00") BigDecimal discountMax20)
public void setDiscountMax20(final @DecimalMax("20.00") BigDecimal discountMax20)
{
this.discountMax20 = discountMax20;
}
Expand Down Expand Up @@ -361,13 +361,13 @@ public void setPositiveOrZeroField(@PositiveOrZero final int positiveOrZeroField
this.positiveOrZeroField = positiveOrZeroField;
}

public @Size(min = 2, max = 240) String getMessageMin2AndMax240()
public @Size(min = 2, max = 10) String getMessageMin2AndMax10()
{
return this.messageMin2AndMax240;
return this.messageMin2AndMax10;
}

public void setMessageMin2AndMax240(final @Size(min = 2, max = 240) String messageMin2AndMax240)
public void setMessageMin2AndMax10(final @Size(min = 2, max = 10) String messageMin2AndMax10)
{
this.messageMin2AndMax240 = messageMin2AndMax240;
this.messageMin2AndMax10 = messageMin2AndMax10;
}
}
Loading

0 comments on commit 072879a

Please sign in to comment.