我有两个请求将是有姓或名,还有性别和州名或通过Userid
所以对NameSearch的请求要么是
{
"NameSearch":
{"LastName": "Doe",
"Gender":"Male",
"State" : "FL"
}
}
or
{
"NameSearch":
{
"FirstName": "John",
"Gender":"Male",
"State" : "FL"
}
}
对于UseridSearch,如下所示
{
"UIDSearch":
{
"UID": "12345"
}
}
我编写了一个模式来验证这一点,以适应所有三种类型的请求
{
"anyOf": [
{
"type":"object",
"additionalProperties": false,
"properties":{
"NameSearch": {
"anyOf":
[
{
"type":"object",
"additionalProperties": false,
"properties":
{
"FirstName":
{
"type":"string",
"required":true
},
"Gender":
{
"type":"string",
"required":true
},
"State":
{
"type":"string",
"required":true
}
}
},
{
"additionalProperties": false,
"properties":
{
"LastName":
{
"type":"string",
"required":true
},
"Gender":
{
"type":"string",
"required":true
},
"State":
{
"type":"string",
"required":true
}
}
}
]
}
}
},
{
"additionalProperties": false,
"properties":{
"UIDSearch": {
"type":"object",
"additionalProperties": false,
"properties":{
"UID":
{
"type":"string",
"required":true
}
}
}
}
}
]
}
但是SchemaValidator说无效。你能指出我的错误在哪里吗?有没有办法使模式变小
我可能迟到了,但我认为您已经忘记在您为JSON定义的模式中添加"type":"object",其中姓氏作为输入
所以你的最终模式是
{
"anyOf": [
{
"type":"object",
"additionalProperties": false,
"properties":{
"NameSearch": {
"anyOf":
[
{
"type":"object",
"additionalProperties": false,
"properties":
{
"FirstName":
{
"type":"string",
"required":true
},
"Gender":
{
"type":"string",
"required":true
},
"State":
{
"type":"string",
"required":true
}
}
},
{
"type":"object", //changed here
"additionalProperties": false,
"properties":
{
"LastName":
{
"type":"string",
"required":true
},
"Gender":
{
"type":"string",
"required":true
},
"State":
{
"type":"string",
"required":true
}
}
}
]
}
}
},
{
"additionalProperties": false,
"properties":{
"UIDSearch": {
"type":"object",
"additionalProperties": false,
"properties":{
"UID":
{
"type":"string",
"required":true
}
}
}
}
}
]
}