129

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"
]
}
4
  • 2
    Even if there is a way, this seems like shooting future extensibility in the foot. Commented Jul 8, 2013 at 16:16
  • 14
    Anytime in the future I will just add those properties to this Schema.
    – ipengineer
    Commented Jul 8, 2013 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
    Commented Oct 24, 2013 at 11:37
  • 18
    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
    Commented Feb 19, 2017 at 3:30

3 Answers 3

172

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

3
  • In the latest draft: json-schema.org/latest/… Commented Sep 13, 2019 at 21:16
  • 1
    In the specification 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
    Commented Oct 5, 2021 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: json-schema.org/draft/2020-12/…
    – TomDestry
    Commented May 24, 2022 at 19:45
27

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

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.