简体   繁体   中英

JSON Schema to prohibit an Array object to have properties

The json schema is:

"Deck": {
  "type": "array",      
  "items": {
    "$ref": "#/definitions/Card"
  },
  "minItems": 52,
  "maxItems": 52,
  "uniqueItems": true
},

So, how can I prohibit this:

let d = new Deck();
d.garbageField = 'fdsf';

with my schema?

我认为您正在寻找"additionalProperties": false

I believe that this problem has nothing to do with JSON Schema as it is a pure JavaScript issue.

JSON schema validates JSON documents not JavaScript objects. And JSON itself doesn't allow you putting additional properties on array.

Let me show you a simple example of JavaScript code running in the Chrome console:

let d = ['a', 'b', 'c'];
d.e = 'test';
JSON.stringify(d);

Will give the output of:

"["a","b","c"]"

So even though JavaScript allows you to put additional properties into an array the resulting JSON object won't have them. Thus it won't get to the point when the JSON is validated against the Schema.

So the answer to your question is:

You cannot do that in JSON schema because it is prohibited by JSON itself.

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