Jak mogę wymagać jednego lub drugiego pola lub (jednego z dwóch innych), ale nie wszystkich z nich?

Mam problem z wymyśleniem schematu JSON, który potwierdzi, czy JSON Zawiera albo:

  • tylko jedno pole
  • tylko inne pole
  • (jedno z dwóch innych pól) tylko

Ale nie pasować, gdy ich wielokrotność jest obecna.

W moim przypadku szczególnie, chcę jeden z

  • copyAll
  • fileNames
  • matchesFiles i / lub doesntMatchFiles

Do walidacji, ale nie chcę akceptować, gdy więcej niż to jest tam.

Oto co mam do tej pory:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "required": [ "unrelatedA" ],
    "properties": {
    "unrelatedA": {
        "type": "string"
    },
    "fileNames": {
        "type": "array"
    },
    "copyAll": {
        "type": "boolean"
    },
    "matchesFiles": {
        "type": "array"
    },
    "doesntMatchFiles": {
        "type": "array"
        }
    },
    "oneOf": [
         {"required": ["copyAll"], "not":{"required":["matchesFiles"]}, "not":{"required":["doesntMatchFiles"]}, "not":{"required":["fileNames"]}},
         {"required": ["fileNames"], "not":{"required":["matchesFiles"]}, "not":{"required":["doesntMatchFiles"]}, "not":{"required":["copyAll"]}},
         {"anyOf": [
               {"required": ["matchesFiles"], "not":{"required":["copyAll"]}, "not":{"required":["fileNames"]}},
               {"required": ["doesntMatchFiles"], "not":{"required":["copyAll"]}, "not":{"required":["fileNames"]}}]}
    ]
} ;
To pasuje bardziej niż chcę. Chcę, aby to pasowało do wszystkich z następujących:
{"copyAll": true, "unrelatedA":"xxx"}
{"fileNames": ["aab", "cab"], "unrelatedA":"xxx"}
{"matchesFiles": ["a*"], "unrelatedA":"xxx"}
{"doesntMatchFiles": ["a*"], "unrelatedA":"xxx"}
{"matchesFiles": ["a*"], "doesntMatchFiles": ["*b"], "unrelatedA":"xxx"}

Ale nie pasować:

{"copyAll": true, "matchesFiles":["a*"], "unrelatedA":"xxx"}
{"fileNames": ["a"], "matchesFiles":["a*"], "unrelatedA":"xxx"}
{"copyAll": true, "doesntMatchFiles": ["*b"], "matchesFiles":["a*"], "unrelatedA":"xxx"}
{"fileNames": ["a"], "matchesFiles":["a*"], "unrelatedA":"xxx"}
{"unrelatedA":"xxx"}

Zgaduję, że jest coś oczywistego, czego mi brakuje - chciałbym wiedzieć, co to jest.

Author: Seanny123, 2014-06-03

1 answers

Problemem jest semantyka "nie". "Nie Wymagane "nie oznacza"zabronione włączenie". Oznacza to po prostu, że nie musisz go dodawać, aby zweryfikować ten schemat.

Możesz jednak użyć "oneOf", aby spełnić swoją specyfikację w prostszy sposób. Pamiętaj, że oznacza to, że "tylko jeden z tych schematów może potwierdzić". Poniższy schemat umożliwia zmianę właściwości, którą próbujesz rozwiązać:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "required": [
        "unrelatedA"
    ],
    "properties": {
        "unrelatedA": {
            "type": "string"
        },
        "fileNames": {
            "type": "array"
        },
        "copyAll": {
            "type": "boolean"
        },
        "matchesFiles": {
            "type": "array"
        },
        "doesntMatchFiles": {
            "type": "array"
        }
    },
    "oneOf": [
        {
            "required": [
                "copyAll"
            ]
        },
        {
            "required": [
                "fileNames"
            ]
        },
        {
            "anyOf": [
                {
                    "required": [
                        "matchesFiles"
                    ]
                },
                {
                    "required": [
                        "doesntMatchFiles"
                    ]
                }
            ]
        }
    ]
}
 62
Author: jruizaranguren,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-22 08:44:51