Skip to content

bvilela/java-util-validation-lib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Java Util Validation Lib

🎯 Quality Status

build publish Quality Gate Status Coverage

📊 Repository Statistics

Lines of Code GitHub repo size GitHub language count GitHub open issues GitHub open pull requests

🔎 Summary

Project with validations utils for Java based in javax and Gson.

💻 Technologies

🚀 GitHub Actions

  • Unit Tests and Analyze SonarCloud
  • Build and run Unit Tests with Maven (branch master)
  • Publish on GitHub Packages (tag/release)

🛠️ Lib Features

Annotation @ValidParseDate.

❗ 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.

Annotation @NotSerialized.

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().

✔️ Check PMD rules locally

To check PMD rules in your machine, run follow command in app dir:

mvn pmd:check

⚙️ Add dependency in your project

To include this dependency in you project, you have to do three things.

  1. Add as dependency in your pom.xml:
<dependency>
	<groupId>com.bvilela.lib</groupId>
	<artifactId>java-util-validation</artifactId>
	<version>0.0.1</version>
</dependency>
  1. 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>
  1. 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.

❓ How to Use

Case 1

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)
}

Case 2

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;
}

Case 3

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"}

⬆ Voltar ao topo