Project with validations utils for Java based in javax and Gson.
- Maven
- Java 11
- Lombok
- Gson 2.9.0
- Static Code Analysis: SonarCloud
- Unit Tests and Analyze SonarCloud
- Build and run Unit Tests with Maven (branch master)
- Publish on GitHub Packages (tag/release)
❗ Can use only in String fields.
Annotation params:
- message: Error message. Default:
Value is a invalid date
.- pattern: Pattern to valid/parse String Date. Default:
dd/MM/yyyy
.- locale: Locale of Date input. Default:
pt_BR
.- parse: Indicates whether the field will be converted to LocalDate. Default:
False
.
The annotated element will not be serialized to gson.toJson(dto).
❗ You need to get the Gson() by the
br.com.bvilela.lib.utils.GsonUtils.getGson()
.
To check PMD rules in your machine, run follow command in app
dir:
mvn pmd:check
To include this dependency in you project, you have to do three things.
- Add as dependency in your
pom.xml
:
<dependency>
<groupId>com.bvilela.lib</groupId>
<artifactId>java-util-validation</artifactId>
<version>0.0.1</version>
</dependency>
- Add the GitHub repository in your
pom.xml
:
<repositories>
<repository>
<id>github</id>
<name>GitHub</name>
<url>https://maven.pkg.github.com/bvilela/java-util-validation-lib</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
- Add the authentication to the Package Registry in your global
settings.xml
:USER_HOME\.m2\settings.xml
<servers>
<server>
<id>github</id>
<username>YOUR_USERNAME</username>
<password>YOUR_AUTH_TOKEN</password>
</server>
</servers>
Replace the YOUR_USERNAME
with your GitHub login name.
Replace the YOUR_AUTH_TOKEN
with a generated GitHub Personal Access Token (PAT):
GitHub > Settings > Developer Settings > Personal access tokens > Generate new token.
The token needs at least the
read:packages
scope.❗ Otherwise you will get a Not authorized exception.
Validate if a variable of type String
is a Valid date.
For this, use the @ValidParseDate
annotation, with the parse
parameter as false
or omit this param (default is false
).
import javax.annotation.br.com.bvilela.lib.utils.ValidParseDate;
public class MyExampleDTO {
@ValidParseDate(message = "DateInit is a invalid date!", pattern = "dd-MM-yyyy")
private String dateInit;
}
import javax.annotation.br.com.bvilela.lib.utils.ValidParseDate;
public class MyExampleDTO {
@ValidParseDate(parse = false, pattern = "dd MMMM yyyy", locale = "en")
private String date; // example: 01 January 2022 (month name in English)
}
import javax.annotation.br.com.bvilela.lib.utils.ValidParseDate;
public class MyExampleDTO {
@ValidParseDate(pattern = "yyyy dd MMMM", locale = "de_DE")
private String date; // example: 2022 15 Oktober (month name in German)
}
import javax.annotation.br.com.bvilela.lib.utils.ValidParseDate;
public class MyExampleDTO {
@ValidParseDate(pattern = "dd MMMM yyyy")
private String date; // example: 01 janeiro 2022 (name month in Portuguese)
}
Validate if a variable of type String
is a Valid date and Convert this value to a variable of type LocalDate
.
For this, use the @ValidParseDate
annotation, with the parse
parameter as true
.
In this case, you need to create a LocalDate
variable with the same name of String variable, concatenating Converted
in name.
import javax.annotation.br.com.bvilela.lib.utils.ValidParseDate;
public class MyExampleDTO {
@ValidParseDate(message = "DateInit is a invalid date!", pattern = "dd-MM-yyyy", parse = true)
private String dateInit;
private LocalDate dateInitConverted;
}
Use @NotSerialized annotation.
import gson.annotation.br.com.bvilela.lib.utils.NotSerialized;
public class MyExampleDTO {
private String name;
@NotSerialized
private String nickName;
@NotSerialized
private int age;
}
import br.com.bvilela.lib.utils.GsonUtils;
var json = GsonUtils.getGson().toJson(dto);
// json = {"name":"nameValue"}