Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only allow properties that are declared in JSON schema

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"
]
}
like image 986
ipengineer Avatar asked Jul 08 '13 15:07

ipengineer


People also ask

What is JSON Schema properties?

Properties. The properties (key-value pairs) on an object are defined using the properties keyword. The value of properties is an object, where each key is the name of a property and each value is a schema used to validate that property.

What does exclusive minimum property in JSON Schema mean?

Gets or sets a flag indicating whether the value can not equal the number defined by the minimum attribute (Minimum).

What is required in JSON Schema?

JSON Schema Example A little description of the schema. The type keyword defines the first constraint on our JSON data: it has to be a JSON Object. Defines various keys and their value types, minimum and maximum values to be used in JSON file. This keeps a list of required properties.

Which key name is used to specify properties that must be included for JSON to be valid?

The property keyword is used to specify the key:value pairs of JSON documents.


3 Answers

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

like image 140
Jules Avatar answered Oct 05 '22 05:10

Jules


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

like image 35
andilabs Avatar answered Oct 05 '22 05:10

andilabs


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.

like image 30
cloudfeet Avatar answered Oct 05 '22 04:10

cloudfeet