Skip to content

Jakarta.validation.constraints #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading