Json Schema中的错误

时间:2014-06-11 15:20:34

标签: json jsonschema

我有两个请求 一个姓氏或名字以及性别和国家 或通过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说无效。你能指出我的错误在哪里,有没有办法让架构变小?

1 个答案:

答案 0 :(得分:0)

我可能会迟到,但我认为您忘记在为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
                                    }
                    }
                }
                }
                }

                ]
                }