Home > OS >  How to validate schema of list of dicts in python?
How to validate schema of list of dicts in python?

Time:05-12

I have a dictionary with config info:

“request”:
    {
        “request_id”: uuid,
        "points": {
            "<point_id>": {"weight": float},
        }
    }

For example

"request":{
     "request_id": "553380e1-fa37-4666-886e-7f56c0540ed8",
     "points": {
          "0": {
             "weight": 5
          },
          "1": {
             "weight":10
          }
     }
}

It should be like this?

"request": {
    "type": "object",
    "properties": {
        "request_id": {
            "type": "string",
        },
        "points":{
             "type": "array",
             "items": {
                ????
             }
        }
    }

I am not able to find the validation schema in my flask project. Is there any solution for this?

CodePudding user response:

points is an object, not an integer. You can specify variable property names with a regular expression in patternProperties, or if the property names can be anything, use additionalProperties to constrain the values.

...
"points": {
  "patternProperties": {
    "^[0-9] $": {
      "type": "object",
      "properties": {
        "weight": {
          "type": "integer"
        }
      },
      "additionalProperties": false
    }
  },
  "additionalProperties": false
}
  • Related