3

I ma working on a JSON schema pattern to exclude numbers and special characters in string, and here is what I have now:

"properties": {
  "applicationName": {
    "description": "TPG Application Name",
    "type": "string",
    "pattern": "[^0-9!@#$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>\\/?\\s\\n]"
},

This is not working as intended, e.g. it does not allow whitespace.

Input JSON:

{
   "applicationName": "TestName",    
}
3
  • and if i want space also? Commented Feb 5, 2018 at 9:52
  • Where's the question? Commented Feb 5, 2018 at 9:54
  • its working thank you Commented Feb 5, 2018 at 9:56

1 Answer 1

5

Judging by the "[^0-9!@#$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>\\/?\\s\\n]" pattern, you want to match any char but digits, whitespace and special characters (punctuation and symbols). Here, whitespace cannot be matched because \s is present inside the negated character class, and the pattern is matching partially, i.e. it will validate any string that contains a char other than the characters listed in the set. ?a! will match since there is a, e.g.

The simplest solution is to match letters and whitespace, from start till end of string:

"^[A-Za-z\\s]*$"

Details

  • ^ - start of string
  • [A-Za-z\\s]* - 0+ letters or whitespace
  • $ - end of string.
Sign up to request clarification or add additional context in comments.

Comments

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.