Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot validate that field is of type string [closed]

I have a class that represents form submit:

Class Form {
   // @String(message = "Invalid type")
   private String name;
}

I was looking for a javax.validation annotation to put on top of the name field, that will validate that the field is indeed of type string, and if not, display an appropriate error message, but I couldn't find one. What's the way to do it?

Clarification:

What bothers me is a case where someone sends one of the following payloads, and I still haven't seen a solution for that:

{
  name: true
}

or

{
  name: 1234
}
like image 821
Mister_L Avatar asked Oct 17 '25 10:10

Mister_L


1 Answers

If you want to validate the input to only have alphabets then use @javax.validation.constraints.Pattern like follows.

class Form {
   @Pattern(regexp="^[A-Za-z]*$",message = "Invalid Input")
   private String name;
}

This will validate that name will only have alphabet.

like image 106
Karthikeyan Vaithilingam Avatar answered Oct 20 '25 00:10

Karthikeyan Vaithilingam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!