1

How to add regex validation for a json schema where the attribute is getting created on the fly and in the format of api pattern and can only contain all upper/lower alphabets, digits, slashes (/) and curly braces {}

E.g:

/someapi/v1/{usename}

/someApi/v1/{userName}

Here is my Uschema:

     {
      "type": "object",
      "patternProperties": {
       "^[a-zA-Z0-9{}/]": {
         "additionalProperties": false,
         "type": "object",
         "patternProperties": {
           "^(EMP|MGR|ACCT)$": {
            "additionalProperties": false,
            "type": "object"
          }
        }
     }
   }
 }

and here is the JSON and the results using ajv (spec 7)

My JSON Sample:

{"/employee/v1/{empId}":{"EMP":{}}} - PASS
{"/employee/v1/{empId}":{"E1MP":{}}} - FAIL (I can understand as E1MP is there)
{"/employee/v1/{empId}/<fooBar>":{"E1MP":{}}} - FAIL 
(I can understand as E1MP is there but didn't complain about < > 
brackets as it was the first entry to validate)

{"/employee/v1/{empId}/<fooBar>":{"EMP":{}}} - PASS (?)
(Actually it should FAIL ???, Why it is considering the < > brackets though I have the regex in place for the outer parent property.)

Also, if I tweak the outer regex to validate any empty space like: ^[a-zA-Z0-9{}/\s], it wont complain any error for the spaces:

{"/emp loye  e/v1/{empI   d}/{f  ooBar}":{"EMP":{}}} -- PASS? (Actually it shoud FAIL ???)
4
  • You have the right approach but you’re missing how the subschemas are applied. I’ll try to provide you with a solution this evening =] Commented Dec 28, 2019 at 16:26
  • Thanks for reverting. In the meantime, how should I understand your phrase of your comment 'how the subschemas are applied' ? Commented Dec 28, 2019 at 17:28
  • I have updated to bring more clarity. Sorry for any inconvenience Commented Dec 29, 2019 at 4:24
  • With your updated question, my comment no longer applies. Commented Dec 30, 2019 at 16:33

1 Answer 1

2

There are two problems with your schema.

First, the regex's are not anchored by default. You need to anchor them. Your first regex is not fully anchored.

Second, even when it's fully anchored, you didn't disallow additional properties.

Here's an updated schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "additionalProperties": false,
  "patternProperties": {
    "^[a-zA-Z0-9{}/]+$": {
      "additionalProperties": false,
      "type": "object",
      "patternProperties": {
        "^(EMP|MGR|ACCT)$": {
          "additionalProperties": false,
          "type": "object"
        }
      }
    }
  }
}

Now, any property of the object which doesn't match the regex will cause a validation error as you expect.

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

5 Comments

Thanks for that. However it is not validating this json: {"/employee/v1/{empId}":{"EMP":{}}}. Also, not sure how to address space validation.
I've amended the regex to work as you expect. Needed to add a repeat.
What do you mean by "handle address space validation"?
I was mentioning about validating spaces in the API key. But I have checked now and its seems, it also got fixed after the amendment. So in nutshell, the regex is now validating: upper/lower alphabets, digits, slashes (/), curly braces {} and No Spaces in the key. Thanks it is now working as expected.
You're welcome. If you have any JSON Schema questions which don't fit on StackOverflow, feel free to join our open slack via the website.

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.