Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unclear about the meaning of propertyNames

Tags:

jsonschema

Here is a JSON schema that uses propertyNames:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "propertyNames": {"enum": ["num", "name"]}
}

I "think" the meaning of that schema is this: A conforming JSON instance must be an object and the object must contain a "num" property and a "name" property; the value of the two properties is unspecified. Is that correct, is that what the schema means?

I created this instance:

{
   "num": 10
}

I validated that instance against the schema and the validator says it is valid. Hmm, why? Doesn't the schema specify that the object must contain both "num" and "name"?

like image 252
Roger Costello Avatar asked Oct 19 '18 18:10

Roger Costello


People also ask

How do you validate a JSON file against a schema?

The simplest way to check if JSON is valid is to load the JSON into a JObject or JArray and then use the IsValid(JToken, JsonSchema) method with the JSON Schema. To get validation error messages, use the IsValid(JToken, JsonSchema, IList<String> ) or Validate(JToken, JsonSchema, ValidationEventHandler) overloads.

What is patternProperties?

The patternProperties is used to restrict the names of extra properties. But the additionalProperties "false" means no extra properies.

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 should be the value of required keyword in JSON Schema?

The required keyword takes an array of zero or more strings. Each of these strings must be unique. In Draft 4, required must contain at least one string.


1 Answers

propertyNames is a schema that all of an object's properties must be valid against. Let's take a look at less confusing example.

{
  "type": "object",
  "propertyNames": { "maxLength": 3, "minLength": 3 },
  "patternProperties": {
    "": { "type": "number" }
  }
}

This describes an object where all properties names must be of length 3 and all property values must be numbers. Here's an example.

{
  "usd": 1,
  "eur": 0.86815,
  "gbp": 0.76504,
  "cad": "1.31004",  <= Invalid property value
  "xx": 1.11         <= Invalid property name
}

Going back to your example, there is one property, "num", which valid against the propertyNames schema { "enum": ["num", "name"] }. Therefore, the value in your example is valid.

like image 199
Jason Desrosiers Avatar answered Oct 21 '22 04:10

Jason Desrosiers