Validate A Schema With Dynamic Keys

Hi
Is that any way to validate schema if my key dynamic to

"container": {
      "type": "object",
      "properties": {
        "image": {
          "type": "string"
        },
        "environment_variables": {
            "type": "object",
            "properties": {
                "^[a-z]+$": {
                    "type": "string"
                }
            },
            "required": [
                "^[a-z]+$"
            ]
        }
      },
      "required": [
        "image",
        "environment_variables"
      ]
    },

environment_variable has dynamic key and value (just lower case letters)
how can I check that

if I have 3 environment_variables

"environment_variables": {
          "type": "object",
          "properties": {
            "letter": {
              "type": "string"
            },
            "number": {
              "type": "string"
            },
            "color": {
              "type": "string"
            }
          },
          "required": [
            "letter",
            "number",
            "color"
          ]
        }

3 random environment_variables are ‘letter’, ‘number’, ‘color’

I wanna use that schema validation when I POST, but if not possible at least I wanna use at my GET call, maybe when I post I can set variable(for that key) and use at that variable at my json schema… Looking solutions

Have a look at the following topic on Stack Overflow.

Using RegEx in JSON Schema - Stack Overflow

It discusses using regex’s for keys and values.

It would appear this is possible using patternProperties.

yes, I did.
I go over all possible stackOverflow posts or any google results.
There is no example for key value dynamic

thanks by the way

I’m not quite following you.

JSON works on key\value pairs, and the above link is showing you dynamic schema for Key’s and Values using Regex patterns.

You can use pattern to check string values, or patternProperties if you are checking the key name.

Obviously the key or value needs to be able to meet that Regex pattern, but its certainly possible. (It might not be Dynamic enough for you).

It might be that I’m not quite understanding what you mean by “dynamic” and “random”.

You need to at least know what might be returned to be able to create any pattern for checking against. There needs to be some consistency. For example, is it always 3 environment variables?

I also don’t really understand the second part of your post about environment variables and what you are trying to achieve here. Perhaps if you can have another go at phrasing the question\flow, I might be able to advise better.

It’s certainly possible to overwrite or update the environment_variables key in your schema. Your schema will be defined as a variable, so if you know the value you need in there (because its stored in another variable) we should be able to update the schema definition. (If that is what we are talking about).

Otherwise there are options in JSONSchema for required vs optional elements that might help.

Ideally a response should show the same data each time you send the same request. (You need to be in control of your test data).

Hi
I will definetly double check with “patternProperties” and update, I tried before but I will do again and share screen shots and script

For my question, I will try to explain more.
There is web application, and at this web application there is a form.
In this form user can click button and add more than 1 environment_variable.
Each click little form come-up with 2 input box.
First input box for “Key” and second input box for “value”
So both key and value can be dynamic/depend of user entry

At post man schema test, even i know always value will be string( at some point of app we are changing type of value but not important), I dont know about my “key”, so I wanted to use regex on my json and schema.

Hopefully I can explain better, and I will share after retry that solution

best
thanks again

I’m still not quite understanding what you want to test.

If the end user can put in any key name, and they can have more than one key\environment, then I’m not sure how you can validate the schema without knowing what they entered in the first place.

You need to know what was submitted, and what you are expecting to be returned.

The expected vs actual response, that your tests should be founded on.

I would turn this on its head, and advise you to think about what you are trying to test first, and then work out how to craft appropriate tests in Postman.

If you are testing that what was sent is being submitted, then somewhere you must have the details on what you are sending as I’m assuming that you are mimicking the submission of the form to the API, and then testing the response.

If you are testing the number of environments is correct, then you need separate requests to the same end point, with a random selection of environments. (But again, you must know how many environments are being submitted and what you are expecting to be returned).

I don’t know how many environments your application\API is meant to be able to handle, but you would usually perform some sort of boundary test.

Ensuring that if you send zero environments, that you get an appropriate error message.

Also if you send over the max number of environments, you also get an error.

You would also send 1 environment, and the max number of environments. (Anything in between would be up to you).

You might want to try key names with special characters to see if the API can handle that.

You could consider using the Collection Runner with a CSV file to control the test data as it is the same end point, just the test data that is changing.

I’m not sure of the value of the schema test unless you are testing the API definition. You might be better just testing the values in the response, to ensure they match what was sent.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.