Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between properties and patternProperties in json schema?

Tags:

jsonschema

For the following json string :

{
    "abc" : 123,
    "def" : 345
}

The following schema considers it valid :

{
    "$schema": "http://json-schema.org/draft-03/schema#",
    "title": "My Schema",
    "description": "Blah",
    "type": "object",
    "patternProperties": {
        ".+": {
            "type": "number"
        }
    }
}

However, changing the the patternProperties to properties still considers it valid. What then, is the difference between these 2 tags?

like image 222
Ashwin Lakshmanan Avatar asked Jun 19 '15 10:06

Ashwin Lakshmanan


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 Exclusiveminimum 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 id in JSON Schema?

$id is a reserved keyword. It serves for: Declaring an identifier for the schema or subschema. Declaring a base URL against which $ref URLs are resolved.

What does $Ref mean in JSON Schema?

In a JSON schema, a $ref keyword is a JSON Pointer to a schema, or a type or property in a schema. A JSON pointer takes the form of A # B in which: A is the relative path from the current schema to a target schema. If A is empty, the reference is to a type or property in the same schema, an in-schema reference.


2 Answers

For the schema above all properties should be number. This data is invalid:

{ a: 'a' }

If you replace patternProperties with properties only property '.+' should be number. All other properties can be anything. This would be invalid:

{ '.+': 'a' }

This would be valid:

{ a: 'a' }
like image 175
esp Avatar answered Oct 07 '22 11:10

esp


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 JSON schema used to validate that property.

additionalProperties can restrict the object so that it either has no additional properties that weren’t explicitly listed, or it can specify a schema for any additional properties on the object. Sometimes that isn’t enough, and you may want to restrict the names of the extra properties, or you may want to say that, given a particular kind of name, the value should match a particular schema. That’s where patternProperties comes in: it is a new keyword that maps from regular expressions to schemas. If an additional property matches a given regular expression, it must also validate against the corresponding schema.

Note: When defining the regular expressions, it’s important to note that the expression may match anywhere within the property name. For example, the regular expression "p" will match any property name with a p in it, such as "apple", not just a property whose name is simply "p". It’s therefore usually less confusing to surround the regular expression in ^...$, for example, "^p$".

for further reference --http://spacetelescope.github.io/understanding-json-schema/reference/object.html

like image 2
Kandy Avatar answered Oct 07 '22 12:10

Kandy