1

I used @Length and @Size of Hibernate annotations along with String type in java DTO class but String type filed allow all other types like boolean and integer ..etc. So how to restrict only string type data along with @Length or @Size with message.

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CreateRoleRequestDto extends BaseDto {

    @Size(max = 255, message = "Role description field length should not exceed 255 
    characters" 
    private String description;
   
    @Length( max = 50,message = "Role name should not exceed 50 characters")
    private String displayName;
}

So here dispalyName and description allow boolean and integer etc . how to restrict to allow only string type along with @Length and @Size with message .

5
  • There is no boolean, interger etc. in the web, everything is a string. A true value will result in the "true" string value. Also what makes you think true cannot be a name of the role or 123? Commented Feb 18, 2022 at 7:44
  • Yes , true can not be name it just boolean , So how to restrict this Commented Feb 18, 2022 at 8:47
  • Why wouldn't it be allowed? Why wouldn't someone be able to name a role true. Looks like you want to restrict something that isn't really an issue. And where do you stop? Strings that look like dates? Strings that look like amounts? Strings that look like Locales, etc. etc. Feels like you are focussing on the wrong thing. Commented Feb 18, 2022 at 8:58
  • What i am trying to say is that String data type strictly allow String data only . don't allow boolean , integer i.e true ,false , 888,334.4 ...etc . It allow like "true" .but not true Commented Feb 18, 2022 at 9:02
  • Sigh.. I give up... You really haven't read my first comment that everything in HTTP is a string... So true and "true" as well as 888 and "888" are the same. If you want to disallow this you are fixing it at the wrong level to start with. I strongly suggest you first start to understand HTTP (and I assume JSON). Commented Feb 18, 2022 at 9:04

1 Answer 1

0

You need to use regxp for solving the issue. Suppose, If you are willing to take only alphabet for the string then you can use something like this :

@Size(max = 255, message = "Role description field length should not exceed 255 
    characters"

@Pattern(regexp="^[A-Za-z]*$",message = "Invalid Input")

private String description;

Like this is you need anything more than just alphabet you can write your custom regexp base on your need.

Sign up to request clarification or add additional context in comments.

1 Comment

With @Patter we can avoid interger types but it allow boolean types like true or false. How to avoid boolean types also ? { "description": false, // it should not accept , but now allowing }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.