REST parameter validation - java

Is there a way to specifically mention the length for the request parameter? My Parameter could be of length 4 or 6 ..
But specifying like below :
#Size(min=4, max=6)
#RequestParam String param1
Would allow length 5 too which is invalid in my case ? Is there a way to accomplish this without a customer validator?
Thanks

You could try to use #Pattern annotation which verifies that string follows specific regexp.
Then, you need to build regexp that will be something like this - ^(?=[0-9]*$)(?:.{4}|.{6})$ (checks that string contains only 4 digits or 6 digits).
Pattern annotation docs

Related

How do I add regex to #Pattern annotation to match one of two specific strings or a null?

I've added #Pattern annotation to a query parameter in my rest controller (SpringBoot Kotlin). I would like the regex in the pattern to accept -
optionA or optionB or null (nothing/an empty string)
The following works, but of course does not include the empty option -
#Pattern(regexp = "(?i)optionA||(?i)optionB")
This does not work -
#Pattern(regexp = "(?i)optionA||(?i)optionB||^\\s*\$")
Can anybody help me with this? :)
Thanks!
Inside the #Pattern annotation, the pattern is used to match the entire string, so you can use
#Pattern(regexp = "(?i)(?:optionA)?")
which is actually \A(?i)(?:optionA)?\z:
\A - start of string (implicit here)
(?i) - case insensitive embedded flag option
(?:optionA)? - an optional non-capturing group that matches optionA or empty string
\z - end of string (implicit here).
The null is a memory address, not a string, it cannot be matched with regex, as regex only deals with strings.
I tryed this optionA|optionB|^\s$ on https://regex101.com/ and worked well. Could you try on your app to check?

Spring boot Request Mapping URI Patterns

There is a request mapping like this:
#DeleteMapping(value = "/{version:.+}")
I not sure what is the .+ does, but from what i know, this delete mapping can accept a value and match to path variable version, something like:
DELETE
/abc
Value abc will map to path variable version
Why the .+ is needed?
Edited Question:
What is the difference with just /{version}, is there any special case that requires .+?
You can find details or URL matching on this link
URL matching
REGEX: .+ means one or more.
‘*’ Matches 0 or More Characters
'+' Matches 1 or More.
#DeleteMapping(value = "/{version:.+}")
.+ means "one or more of any characters" - thats standard regex/
version: means - put that match in path variable named version.

Restrict particular domain in email regular expression

I have an existing regex which validates the email input field.
[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-zA-Z0-9!$%&'*+/=?^_`{|}~-]+)*(\\.)?#(?:[a-zA-Z0-9ÄÖÜäöü](?:[a-zA-Z0-9-_ÄÖÜäöü]*[a-zA-Z0-9_ÄÖÜäöü])?\\.)+[a-zA-Z]{2,}
Now, I want this regex to not match for two particular type of email IDs. Which are wt.com and des.net
To do that I made the following changes in the above expression like this.
[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-zA-Z0-9!$%&'*+/=?^_`{|}~-]+)*(\\.)?#(?!wt\\.com)(?!des\\.net)(?:[a-zA-Z0-9ÄÖÜäöü](?:[a-zA-Z0-9-_ÄÖÜäöü]*[a-zA-Z0-9_ÄÖÜäöü])?\\.)+[a-zA-Z]{2,}
After this it does not matches with any email id which ends with the wt.com and des.net which is right.
But the problem is it does not match with wt.comm or any other letter after the restricted string too..
I just want to restrict email which ends with wt.com and des.net
How do I do that?
Below is the sample emails which should match or not.
ajcom#wt.com : no match
ajcom#aa.an : match
ajcom#wt.coms :match
ajcom#des.net : no match
ajcom#des.neta: match
If you want to prevent only wt.com and des.net which have no characters after it you can add $ anchor (which represents end of string) at the end of each negative-look-ahead.
So instead of (?!wt\\.com)(?!des\\.net) use (?!wt\\.com$)(?!des\\.net$)

#Pattern validation on a blank field

I am using validation annotations to validate the Email field, Here the email field is not mandatory, Still when I click on submit, It is validating and throwing error message when the field is empty,
Here are the annotations:
#Pattern(regexp = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*#[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$",
message = "registration.label.useremailformat.invalidformat")
#Size(min = 6, max = 20, message = "registration.label.useremail.length")
#SafeHtml
private String userEmailId;
How can I skip #Pattern validation when the email field is empty and validate only when the value is given? Any help would be highly appreciated.
Modify your regexp to allow empty string and remove #Size:
#Pattern("^$|(<<old regexp>>)")
#SafeHtml
private String userEmailId;
... where <<old regex>> is a placeholder for your current lengthy regexp.
It seems that the problem is that the #Size restriction does not allow form submission even after making the whole regex pattern optional.
The workaround I suggest is moving the size restriction into the regex pattern:
regexp = "^(?:(?=.{6,20}$)[_A-Za-z0-9-+]+(\\.[_A-Za-z0-9-]+)*#[A-Za-z0-9-]+(\\.[A-Za-z0-9]+‌​‌​)*(\\.[A-Za-z‌​]{2,}))?$"
Where the outer (?:...)? encloses your pattern making it possible to match an empty string, and (?=.{6,20}$) is a positive lookahead that - before matching the pattern - checks if there are 6 to 20 characters in the text. If there are fewer or more, the match is failed.
The only disadvantage is that you can only display one error message with it.

Tokenize a string using apache lucene

How to tokenize the string based on a patter?
Example. In following string
arg1:aaa,bbb AND arg2:ccc OR arg3:ddd,eee,fff
First I want to tokenize based on AND and OR
So
Token set 1 arg1:aaa,bbb
Token set 2 arg2:ccc
Token set 3 arg3:ddd,eee,fff
Later i want to pass these individual token sets to a method and tokenize based on ":"
Token set 1
Token 1 aaa
Token 2 bbb
Token set 2
Token 1 ccc
Token set 3
Token 1 ddd
Token 2 eee
Token 3 fff
How to tokenize using custom patter using Lucene?
To perform a custom tokenization implementation, you would generally implement your own Tokenizer. The primary method that needs to be implemented would be TokenStream.incrementToken().
Your Tokenizer can then be incorporated into an Analyzer.

Categories

Resources