Bean Validation range constraint - java

I need to implement range constraints on Entity data fields:
#Entity
#Table(name = "T_PAYMENT")
public class Payment extends AbstractEntity {
//....
//Something like that
#Range(minValue = 80, maxValue = 85)
private Long paymentType;
}
I already created validating service, but have to implement many of these cases.
I need the app to throw exception if the inserted number is out of range.

You need Hibernate Validator (see documentation)
Hibernate Validator
The Bean Validation reference implementation.
Application layer agnostic validation Hibernate Validator allows to
express and validate application constraints. The default metadata
source are annotations, with the ability to override and extend
through the use of XML. It is not tied to a specific application tier
or programming model and is available for both server and client
application programming. But a simple example says more than 1000
words:
public class Car {
#NotNull
private String manufacturer;
#NotNull
#Size(min = 2, max = 14)
private String licensePlate;
#Min(2)
private int seatCount;
// ...
}

With hibernate-validator dependency you can define range check
#Min(value = 80)
#Max(value = 85)
private Long paymentType;
In pom.xml add below dependency
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>{hibernate.version}</version>
</dependency>

For integer and long you can use #Min(value = 80)
#Max(value = 85)
For BigDecimal #DecimalMin(value = "80.99999")
#DecimalMax(value = "86.9999")

I believe this is the specific annotation you are looking for:
https://docs.jboss.org/hibernate/validator/4.1/api/org/hibernate/validator/constraints/Range.html
Example:
#Range(min = 1, max =12)
private String expiryMonth;
This is also a more beneficial annotation because it will work on Strings or Number variants without using two annotations like is the case with #Max/#Min. #Size is not compatible with Integers.

Related

Bean validation specification prioritizing

I am using bean validation specification to validate my form on spring-boot thymeleaf project.
My entity property is as follow.
#NotEmpty(message = "{Password should not be empty}")
#Pattern(regexp = //Pattern for range 1-20, message = "{Wrong Input}")
private String password;
When I run and inputed to password field of my form with empty value, both of Validation Error Messages were shown.
My expectation is, while empty value is inputed, only #NotEmpty annotation should work and on the other hand, only #Pattern should be shown upon user input is wrong.
How can I do with Bean Validation Specification for that?
Regards.
1. Validation groups
#NotEmpty(groups = First.class), message = ...,
#Pattern(groups = Second.class, regexp = ...)
private String password;
Create the validation groups:
//Validation Groups - Just empty interface used as Group identifier
public interface First {
}
public interface Second {
}
and validate the model this way:
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<Model>> violations = validator.validate(model, First.class);
if(violations.isEmpty()){
violations = validator.validate(model, Second.class);
}
2. Groups Sequences
I've never used them, but it seems it does just what you want
Check this other answer (https://stackoverflow.com/a/7779681/641627). I've added below a quote from this answer (from #Gunnar), which ironically also uses First and Second group names:
#GroupSequence({First.class, Second.class})
public interface Sequence {}
#Size(min = 2, max = 10, message = "Name length improper", groups = { First.class })
#Pattern(regexp = "T.*", message = "Name doesn't start with T" , groups = { Second.class })
private String name;
When now validating a Bean instance using the defined sequence
(validator.validate(bean, Sequence.class)), at first the #Size
constraint will be validated and only if that succeeds the #Pattern
constraint.
With this solution, you wouldn't need to manually call validator.validate(...), the validations would be performed in the order defined in the Sequence with short-circuit if one fails.

Jpa 2.0 and Unique Constraints

I'm in a project which is based on Oracle DB and EclipseLink as EM implementation.
I got a table which has a standard id sequence generator, let's say:
#Id
#GeneratedValue(strategy = GenerationType.TABLE, generator = "INVOICE")
#TableGenerator(name = "INVOICE", allocationSize = 1, table = Constants.SEQUENCE_TABLE, schema = Constants.DATABASE_SCHEMA)
#Column(length = 40)
private String id;
I also got a unique numeric field. I've decided to create a special sequence for that field, and populate it through a trigger, which increments that numeric field on every insert, passing sequence "NextVal".
My question is: is it a good practice to take advantage of #Sequencegenerator and #TableGenerator annotation also for a field that is not an #Id for the same entity?
I'm not a trigger fan...so adding a thing like this from the hypothetical first snippet... i could manage whole entity with JPA standard Annotations:
#GeneratedValue(strategy = GenerationType.TABLE, generator = "UNIQUEFIELD")
#TableGenerator(name = "UNIQUEFIELD", allocationSize = 1)
#Column
private String uniqueValuesField;
Thanks!
JPA does not support sequences on non-id fields, so this isn't likely to work without relying or delving into your provider's native support.
You can always refresh the entity afterward if required to get the values set by triggers, but they are a common enough occurrence on legacy systems, so providers will have support of some form for triggers. EclipseLink has #ReturnInsert and #ReturnUpdate to allow getting the values back from triggers.
To mark a property as generated, use The Hibernate specific #Generated annotation.Properties marked as generated must additionally be non-insertable and non-updateable. Only #Version and #Basic types can be marked as generated.
http://docs.jboss.org/hibernate/orm/5.1/userguide/html_single/Hibernate_User_Guide.html#mapping-generated
never (the default)
the given property value is not generated within the database.
insert
the given property value is generated on insert, but is not regenerated on subsequent updates. Properties like creationTimestamp fall into this category.
always
the property value is generated both on insert and on update.
hope this can help you.
I think writing a trigger is a redundant action, and it will reduce the readability of the code.
There are three main types of ID generators provided in hibernate TABLE, SEQUENCE and IDENTITY.
here is a good read with examples by Vladmihalcea on the Sequence generator strategies provided.
Or else you can create your own sequence generator where I had to keep a unique id among two tables so I did this
#Id
#GenericGenerator(name = "sequence", strategy = "IdGenerator")
#GeneratedValue(generator = "sequence")
#Column(name = "ID", columnDefinition = "BIGINT")
and in the Id generator
Public class IdGenerator extends IncrementGenerator {
private static long nextVal = Long.MIN_VALUE;
private static Object idlock = new Object();
private static volatile String COLUMN_NAME = "id";
#Override
public Serializable generate(SessionImplementor session, Object obj) {
if (obj == null) {
throw new HibernateException("Null object passed");
}
synchronized (idlock) {
nextVal = //get the value according to your logic
}
return nextVal;
}
}

How to make javax validation only display one error message

Say I have validation on a field like this:
#NotEmpty
#Digits(integer = 3, fraction = 0)
private String code;
Using Spring MVC and Hibernate validation currently I get both messages if I leave the form field blank. Is there a way to only display the #NotEmpty message?
If you want to stick to the Bean Validation Specification you need to use a group sequence. Only a groups sequence guarantees an ordered constraint evaluation which stops on the first error. Something like:
#GroupSequence({ First.class, Second.class })
public class MyBean {
#NotEmpty(groups = First.class)
#Digits(integer = 3, fraction = 0, groups = Second.class)
private String code;
}

Group for Constraints in Bean Validation

#NotNull(message = "{email.error}")
#Valid(message = "{email.error}")
private String email;
#NotNull(message = "{password.error}")
#Size(min = 8, max = 16, message = "{password.error}")
private String password;
Is there anyway to group the constraints that belong to a single attribute?
So if one constraint is not passed, this will throw the error in question. If multiple constraints are not valid, this would still throw only this one error.
Let me illustrate what type I am after:
#List(constraints = {
#NotNull,
#Valid
}, message = "{email.error}")
private String email;
#List(constraints = {
#NotNull,
#Size(min = 8, max = 16)
}, message = "{password.error}")
private String password;
Is something like this provided or what I have to create my own Validator for this?
Thanks in advance.
You are probably looking for the group respectively group sequence feature. Check the documentation for that. There are heaps of examples.
Regarding your example, #Valid on a String does not make sense. This annotation is for cascaded validation. Again check the documentation if you want to know more. Also an annotation like #List does not exist and wold not even compile.

How to write tests for validation?

Let's say you have a request class defined like this:
public class CreateUserRequest {
#NotNull
#Pattern("^[a-zA-Z0-9]+$")
#Size(min = 6, max = 16)
public String userName;
#NotNull
#Pattern("^[a-zA-Z0-9]+$")
#Size(min = 8, max = 32)
public String password;
#NotNull
#Pattern("^[a-zA-Z]+ [a-zA-Z]+$")
#Size(min = 3, max = 100)
public String fullName;
}
How do you write tests for constraints?
Do you write these tests at all?
Minor update
It's not about constraints I'm using in this example. It's about testing/not testing whatever constraints you have.
Hibernate validator allows validating individual fields, so for this case one of the possible solutions is to write tests for userName, password and fullName. This approach is pretty silly though, you may check this for better approach: Using same constraints in multiple classes.

Categories

Resources