0

I recently found jsonschema and I've been loving using it, however recently I've come across something that I want to do that I just haven't been able to figure out.

What I want to do is to validate that an array must contain an element that matches a schema, but I don't want to have validation fail on other elements that would be in the list.

Say that I have an array like the following:

arr = [
          {"some object": True},
          False,
          {"AnotherObj": "a string this time"},
          "test"
       ]

I want to be able to do something like "validate that arr contains an object that has a property 'some object' that is a boolean, and error if it doesn't, but don't care about other elements."

I don't want it to validate the other items in the list. I just want to make sure that the list contains an element that matches the schema at least once. I also do not know the order which the elements will arrive in the array.

I've tried this already with a schema like:

   {"type": "array",
    "items": {
        "type": "object",
        "properties": {
            "tool": {
                # A schema here to validate tool
            },
        "required": ["tool"]
        }
    }

The problem is that it requires every item in the array to have the property "tool", and not what I actually want.

Any help anyone can give me with this would be much appreciated! I've been stumped on this for a really long time with no forward progress.

Thanks!

4

1 回答 1

0

我已经得到了这个问题的答案:使用的架构是(其中 ... B ... 是需要的架构):

{
    "type": "array",
    "not": {
        "items": {
            "not": {... B ...}
        }
    }
}

它基本上类似于“确保不(项目与 B 不匹配)”之类的东西。我不是 100% 清楚为什么它会这样工作,但它确实如此,我想我会为后代分享它。

于 2013-07-09T22:40:38.100 回答