Validating flattened json keys and schemas

220 Views Asked by At

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.

0

There are 0 best solutions below