_TASK_SCHEMA = {
"type": {
"enum": [
"import", "export", "clone"
],
"type": "string"
},
"status": {
"enum": [
"pending",
"processing",
"success",
"failure"
],
"type": "string"
},
"input": {
"type": "object",
"oneOf": [
{"$ref": "#/definitions/import"},
{"$ref": "#/definitions/export"},
{"$ref": "#/definitions/clone"},
],
},
}
definitions = {
"import": {
"additionalProperties": False,
"properties": {
"import_from": {"type": "string"},
"import_from_format": {"type": "string"},
"image_properties": {"type": "object"}
}
},
"export": {
"additionalProperties": False,
"properties": {
"export_from": {"type": "string"},
"export_from_format": {"type": "string"},
"export_properties": {"type": "object"}
}
},
"clone": {
"additionalProperties": False,
"properties": {
"clone_from": {"type": "string"},
"clone_from_format": {"type": "string"},
"clone_properties": {"type": "object"}
}
}
}
raw = {
'name': "testing",
'properties':_TASK_SCHEMA,
'definitions': definitions,
'additionalProperties': False,
}
data = {
"type": "import",
"input": {
"import_from": "import from url",
"import_from_format": "import from format",
"image_properties": {}
}
}
jsonschema.validate(data, raw)
In the above schema, I am trying to validate "input" type parameter value which I defined in oneOf schema definition according to the "type" parameter value. But it’s not working.
For Example:
If type = "import" then, Input must have {"import_from" , "import_from_format" ,"image_properties"} properties.
If type = "export" then, Input must have {"export_from" , "export_from_format" , "export_properties"} properties.
If type = "clone" then, Input must have {"clone_from" , "clone_from_format" , "clone_properties"} properties.
NOTE:
I don't want to change the schema format (due to some project specific constraints)
Can anyone help me for the same?
{
"name": "testing",
"properties": {
"type": {
"enum": [ "import", "export", "clone" ],
"type": "string"
},
"status": {
"enum": [ "pending", "processing", "success", "failure" ],
"type": "string"
},
"oneOf": [
{
"properties": {
"type": { "enum": [ "import" ] },
"input": { "$ref": "#/definitions/import" }
}
}, {
"properties": {
"type": { "enum": [ "export" ] },
"input": { "$ref": "#/definitions/export" }
}
}, {
"properties": {
"type": { "enum": [ "clone" ] },
"input": { "$ref": "#/definitions/clone" }
}
}]
}
},
"definitions": {
"import": {
"additionalProperties": false,
"properties": {
"import_from": {"type": "string"},
"import_from_format": {"type": "string"},
"image_properties": {"type": "object"}
}
},
"export": {
"additionalProperties": false,
"properties": {
"export_from": {"type": "string"},
"export_from_format": {"type": "string"},
"export_properties": {"type": "object"}
}
},
"clone": {
"additionalProperties": false,
"properties": {
"clone_from": {"type": "string"},
"clone_from_format": {"type": "string"},
"clone_properties": {"type": "object"}
}
}
},
"additionalProperties": false
}