119

I am using json-schema and wanting to only allow properties that are declared in this file to pass validation. For instance if a user passes a "name" property in their json object it will fail this schema because "name" is not listed here as a property.

Is there some function similar to "required" that will only allow the listed properties to pass?

{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Accounting Resource - Add Item",
"type": "object",
"properties": {
    "itemNumber": {
        "type":"string",
        "minimum": 3
    },
    "title": {
        "type":"string",
        "minimum": 5
    },
    "description": {
        "type":"string",
        "minimum": 5
    }
},
"required": [
    "itemNumber",
    "title",
    "description"
]
}
jb.
  • 9,921
  • 12
  • 54
  • 90
ipengineer
  • 3,107
  • 7
  • 33
  • 37
  • 2
    Even if there is a way, this seems like shooting future extensibility in the foot. – Chris Pitman Jul 08 '13 at 16:16
  • 14
    Anytime in the future I will just add those properties to this Schema. – ipengineer Jul 08 '13 at 18:11
  • 1
    @ipengineer - that works (-ish) as long as *you* are the person doing the extending. It also means that you change a resource which some people might assume is static. – cloudfeet Oct 24 '13 at 11:37
  • 17
    There's no "-ish" about it. It's not difficult to add new properties to your schema if your API starts to accept new props in the future, no matter the size of your team. If it is, you're probably doing something else wrong. – AJB Feb 19 '17 at 03:30

3 Answers3

160

I believe what you need to do to achieve this is set additionalProperties to false. See the specification here

localheinz
  • 9,179
  • 2
  • 33
  • 44
Jules
  • 14,841
  • 9
  • 83
  • 130
  • In the latest draft: http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.5.6 – OneHoopyFrood Sep 13 '19 at 21:16
  • 1
    In [the specification](http://json-schema.org/draft/2020-12/json-schema-core.html#rfc.section.10.3.2.3) there is stated that _The value of "additionalProperties" MUST be a valid JSON Schema._ Can you specify, where it is stated that the value can be a boolean? – eNca Oct 05 '21 at 10:03
  • 2 of the 3 schema validators I tried said 'true' is a valid JSON schema. ::shrug Also the doc you reference uses booleans for additionalProperties: http://json-schema.org/draft/2020-12/json-schema-core.html#rfc.section.12.4 – TomDestry May 24 '22 at 19:45
21

Inside your definition provide:

  • all required fields inside "required": []
  • and set "additionalProperties": false

DEMO:

without "additionalProperties": false: enter image description here

with "additionalProperties": false: enter image description here

andilabs
  • 22,159
  • 14
  • 114
  • 151
6

FYI - it looks like v5 of the standard will describe a "ban unknown properties" validation mode.

So instead of making this requirement part of the format (which as Chris Pitman says in the comments, damages future extensibility), you can simply instruct your validator to flag unknown properties as errors. So, it's like a super-strict validation mode which is useful for dev.

Some validators already support this (e.g. tv4):

var result = tv4.validateMultiple(data, schema, checkRecursive, banUnknownProperties);

With this tool, checkRecursive should be used if your data might have circular references, and banUnknownProperties will do exactly what you want, without having to use "additionalProperties":false.

cloudfeet
  • 12,156
  • 1
  • 56
  • 57