简体   繁体   中英

How can i define a single, unique Key-Valuepair in JsonSchema?

The Schema should allow only the following constellation: {"status":"nok"} . The Key must always be "status" and the value should allow "ok","nok","inProgress" No differen or additional objects,... should be allowed

I have tried this:

{
"description": "blabla",
"type": "object",
"properties": {
    "status": {
        "type": "string",
        "enum": [
            "ok",
            "inProgress",
            "nok"
        ],
        "required": true,
        "additionalItems": false
    }
},
"required": true,
"additionalProperties": false
}

This works, but this scheme allows that i can send the same key/value pair twice like {"status":"nok","status":"nok"} I would be also happy, if it would work without this "object"-container that i'm using, because to reduce overhead. Maybe someone knows a solution, thanks

There is a more fundamental issue with that input:

{"status":"nok","status":"nok"}

mainly: that input is not valid JSON . RFC 4627 , section 2.2, explicitly states that "The names within an object SHOULD be unique". And in your case, they are not.

Which means the JSON parser you use can do whatever it wants to with such an input. Some JSON APIs will grab whatever value they come upon first, other parsers will grab the last value they read, others will even coalesce values -- none of this is illegal as per the RFC.

In essence: given such input, you cannot guarantee what the output of the JSON parser will be; and as such, you cannot guarantee JSON Schema validation of such an input either.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM