Validation 将JSON.NET与模式创建和验证中的oneOf、anyOf行为一起使用?

Validation 将JSON.NET与模式创建和验证中的oneOf、anyOf行为一起使用?,validation,schema,json.net,Validation,Schema,Json.net,尝试使用JSON.NET构建模式以验证作为RGB或CMYK提供的颜色 这应该验证: { "color": { "RGB": { "r": 100, "g": 100, "b": 100 } } } 这不应: { "color": { "RGB": { "r"

尝试使用JSON.NET构建模式以验证作为RGB或CMYK提供的颜色

这应该验证:

    {
        "color": {
            "RGB": {
                "r": 100,
                "g": 100,
                "b": 100
            }
        }
    }
这不应:

{
    "color": {
        "RGB": {
            "r": 100,
            "g": 100,
            "b": 100
        },
        "CMYK": {
            "c": 5,
            "m": 10,
            "y": 60,
            "k": 30
        }
    }
}
这是我的模式(如下),我尝试过使用min/max项来完成其中一项(我认为)的工作。我不直接使用“oneOf”的原因是,似乎没有能力在代码中添加这个,而这正是我使用JSON.NET所需要的,而不是将模式作为字符串传递。 我希望能够在我有工作并且在JSON.NET中找不到任何与oneOf/anyOf相关的文档时使用JObject.Parse(…)。 非常感谢你的指点

{
    "id": "color_spec.json",
    "type": "object",
    "required" : true,
    "properties": {
        "color": {
            "type": "object",
            "required" : true,
            "minItems" : 1,
            "maxItems" : 1,
            "properties": 
                {
                    "RGB": {
                        "type": "object",
                        "required" : true,
                        "properties": 
                            {
                                "r": {
                                    "type": "number",
                                    "required" : true
                                },
                                "g": {
                                    "type": "number",
                                    "required" : true
                                },
                                "b": {
                                    "type": "number",
                                    "required" : true
                                }
                            }

                    },
                    "CMYK": {
                        "type": "object",
                        "properties": 
                            {
                                "c": {
                                    "type": "number",
                                    "required" : true
                                },
                                "m": {
                                    "type": "number",
                                    "required" : true
                                },
                                "y": {
                                    "type": "number",
                                    "required" : true
                                },
                                "k": {
                                    "type": "number",
                                    "required" : true
                                }
                            }

                    }
                }

        }
    }
}

required
应该是所需属性的数组。因此,在根目录下,它应该是:
“required”:[“color”]
。其他属性也是如此。你能解释一下为什么不使用其中一个吗?您是如何进行验证的?@JeffMercado-嗨,Jeff,我考虑过
“requires”:[“color”]
,但它仍然不能按预期工作。我在返回的
JObject
上使用JSON.net5.0和
JSON.Linq.JObject.Parse(jsontovalidate)
,然后是
JObject.IsValid(schema)
,它仍然在第二种情况下进行验证。我认为JSON.NET还不支持v4草案,所以我认为这就是其中一个不起作用的原因——模仿这种行为的替代方案?谢谢,帕,我知道了。我在看说明书,但无法验证。也许有一种方法可以做你正在尝试的事情,但我还不知道有什么方法。