[JAVA] [Spring Boot] List of validation rules that can be used in the property file for error messages

Hi, I'm Mike from Team CPR. This time, I have summarized the validation rules that can be used in the property file for error messages in SpringBoot.

In SpringBoot, you can easily implement validation error for input from screen by setting in property file. Since there are many validation checks that can be used, I have compiled a list that also serves as a memorandum.

Rule summary

-The file name and installation location are as follows.

Filename ValidationMessages.properties
File location src> main> resources

-The method of filling in the property file is as follows. Specification of validation to use = Error message </ b>

Example 1: org.hibernate.validator.constraints.NotBlank.message = This item is required. Example 2: javax.validation.constraints.Min.message = Please enter a value larger than {value} for this item.

・ Supplement Writing {value} is a convenient way to write variable values. Variables, so to speak. The value passed as an annotation argument is displayed on the screen as a character string.

List of validation types
  • org.hibernate.validator.constraints.CreditCardNumber.message
  • org.hibernate.validator.constraints.EAN.message
  • org.hibernate.validator.constraints.Email.message
  • org.hibernate.validator.constraints.Length.message
  • org.hibernate.validator.constraints.LuhnCheck.message
  • org.hibernate.validator.constraints.Mod10Check.message
  • org.hibernate.validator.constraints.Mod11Check.message
  • org.hibernate.validator.constraints.ModCheck.message
  • org.hibernate.validator.constraints.NotBlank.message
  • org.hibernate.validator.constraints.NotEmpty.message
  • org.hibernate.validator.constraints.ParametersScriptAssert.message
  • org.hibernate.validator.constraints.Range.message
  • org.hibernate.validator.constraints.SafeHtml.message
  • org.hibernate.validator.constraints.ScriptAssert.message
  • org.hibernate.validator.constraints.URL.message
  • org.hibernate.validator.constraints.br.CNPJ.message
  • org.hibernate.validator.constraints.br.CPF.message
  • org.hibernate.validator.constraints.br.TituloEleitoral.message
  • javax.validation.constraints.AssertFalse.message
  • javax.validation.constraints.AssertTrue.message
  • javax.validation.constraints.DecimalMax.message
  • javax.validation.constraints.DecimalMin.message
  • javax.validation.constraints.Digits.message
  • javax.validation.constraints.Future.message
  • javax.validation.constraints.Max.message
  • javax.validation.constraints.Min.message
  • javax.validation.constraints.NotNull.message
  • javax.validation.constraints.Null.message
  • javax.validation.constraints.Past.message
  • javax.validation.constraints.Pattern.message
  • javax.validation.constraints.Size.message
* Describe the word before message as an annotation.

Summary Since the number of validation checks tends to be large, using the property file for error messages will lead to a significant reduction in man-hours, and above all, it will be easier to manage and maintain. Please try it if you like.

Citation / reference book Written by Yano Palmada, published in 2018, "Introduction to Spring Boot 2 Programming", Shuwa System

Recommended Posts