user2740992
user2740992

Reputation:

Regex for json schema pattern

I would like to validate a human input in json schema with pattern, but I cannot figure out the regex for it.

Valid formats are:

"Joe":"day1","Mitch":"day2"

or

"Joe":"day1"

So any number of iterations of "somebody":"someday" separated with , (comma).

Invalid formats are:

"Joe":"day1","Mitch":"day2",

Or

"Joe":"day1";"Mitch":"day2"

Example Json schema (pattern is not working here):

{
    "title": "Test",
    "type": "object",
    "properties": {
"meeting_date": {
            "type": "string",
            "description": "Give me a name and a day with the following format: \"Joe\":\"day1\" ",
            "default": "",
            "pattern": "(\"\\w*\")(?::)(\"\\w*\")"
        }
        }
} 

Upvotes: 1

Views: 3488

Answers (2)

bato3
bato3

Reputation: 2815

try this solution https://regex101.com/r/vW8m6K/2/

^("\w+":"\w+",)*"\w+":"\w+"$

But it fails on extra spaces, for it test:

^("\w+"\s*:\s*"\w+"\s*(?:,\s*|$))+$

Upvotes: 1

Simon
Simon

Reputation: 83

Your pattern acutally almost works. You just have to remove the backslashes in front of your quotation marks.

("\w*")(?::)("\w*")

You can test your Regex on https://regex101.com/ (or some simular website).

Upvotes: 1

Related Questions