0

Im trying to find a way to validate flattened json-keys. For example, lets say I have a schema defined as below:

{
  "$schema":"http://json-schema.org/draft-04/schema#",
  "title":"Employee",
  "type":"object",
  "additional_properties":false,
  "properties":{
    "emp_category":{
      "type":"string",
      "oneOf":[
        {
          "enum":[
            "INDIVIDUAL_CONTRIBUTOR",
            "MANAGER"
          ]
        }
      ]
    },
    "id":{ "type":"string" },
    "emp_meta":{
      "oneOf":[
        { "$ref":"#/definitions/IndividualContributor" },
        { "$ref":"#/definitions/Manager" }
      ]
    }
  },
  "required":[
    "emp_category",
    "id"
  ],
  "definitions":{
    "IndividualContributor":{
      "title":"IndividualContributor",
      "type":"object",
      "properties":{
        "name":{ "type":"string" },
        "id":{ "type":"string" },
        "department":{ "type":"string" },
        "managed_by":{ "type":"string" }
      },
      "required":[
        "id",
        "department",
        "managed_by"
      ]
    },
    "Manager":{
      "title":"Manager",
      "type":"object",
      "properties":{
        "name":{ "type":"string" },
        "id":{ "type":"string" },
        "department":{ "type":"string" },
        "managed_by":{ "type":"string" },
        "manages":{
          "type":"array",
          "items":{ "type":"string" }
        }
      },
      "required":[
        "id",
        "department",
        "managed_by"
      ]
    }
  }
}

Now, we want to expose some upstream REST API to be able to query over objects pertaining to above schemas, lets say we have a REST payload as below:

{
  "empoloyee":{
    "AND":[
      { "emp_category":"MANAGER" },
      { "emp_meta.department":"R&D" },
      { "emp_meta.manages":"John*" }
    ]
  }
}

So, I am wondering if theres a generalized way to validate flattened json keys that are part of the query payload. I've tried to parse (dfs) the query payload by leaf and convert it into a dict object and validate against the schema. But required fields are making this quite challenging. So, wondering if theres a way to go about it. I'm open to considering a different design as well, especially as keys and objects can become deeply nested.

Byted
  • 579
  • 2
  • 12
V1666
  • 185
  • 3
  • 14
  • There's no out of the box solution for this in jsonschema. The schema you provided doesn't match the payload (e.g. the schema assumes all properties belong to one object but the payload is an array with one object per property). It's possible to create a jsonschema for your payload but before helping I want to make sure I correctly understand your situation. It seems like you try to use one schema for two representations / use cases. Can you describe a flow of data and when you would want to validate/verify the schema? – Byted Oct 01 '22 at 11:42
  • Hi @Byted We are using the above schema to validate two things - 1. while generating the corresponding structures and payload, 2. for validating upstream query REST API. The REST API accepts flattened keys and we then build a query suing the flattened keys paths. As you've said, required fields are making it hard to generalize the validation for both validation workflows. As of now we are recursively removing required-fields key when validating REST-API query payload. – V1666 Oct 02 '22 at 16:31

0 Answers0