10

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"?

Roger Costello
  • 3,007
  • 1
  • 22
  • 43
  • 3
    enum just indicates a list of values that are allowed, but doesn't mean that both are mandatory. – D. Mayen Oct 19 '18 at 18:44
  • Ah! Thank you @D. Mayen. So, how would the schema specify that both "num" and "name" must be used in instance documents? – Roger Costello Oct 19 '18 at 18:50
  • The "Understanding JSON Schema" site is becoming the De Facto source of understanding for JSON Schema authors. We appreciate that reading the actual specification document itself can be daunting. We are happy to help you understand it if you have further questions. Please feel free to join the official JSON Schema slack using the "Discussion" link on json-schema.org – Relequestual Oct 22 '18 at 09:06

3 Answers3

22

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.

Jason Desrosiers
  • 22,479
  • 5
  • 47
  • 53
-1

I'm not familiar with the schema approach taken by json-schema and find it quite confusing whether examples are definitions or implementations. However, my interpretation would be that property names are defined by an enumeration, which can either be a list of numbers

"propertyNames": {"enum": [1,2,3,4]}

or a list of names

"propertyNames": {"enum": ["Ellen","Sue","James","Tim"]}

an outside bet (which I doubt) would be that it should be:

"propertyNames": {"enum": [1, "Ellen", 2, "Sue", 3, "James", 4, "Tim"]}

The only scenario in which I can see your example being valid is if the word "enum" is intended to be replaced by the type of enum and that where an enum can have only one value that you needn't use an array but that equally valid should then be: {"num": [8,9,10]} and {"name": ["Ellen","Sue","James","Tim"]}

Apologies if this isn't the clear answer you were hoping for but I hope it might help trigger a solution.

sketchyTech
  • 5,746
  • 1
  • 33
  • 56
-1

Try this:

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

you can also specify the type of the properties like this:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "required": ["num", "name"]
    "properties": {
        "num": {"type": "number"},
        "name": {"type": "string"}
    }
}

For more information, you can visit this link: https://json-schema.org/understanding-json-schema/reference/object.html

D. Mayen
  • 424
  • 3
  • 8
  • 1
    Hello @D. Mayen I understand that approach. However, my intent is to learn what the new keyword "propertyNames" means and how to use it meaningfully. – Roger Costello Oct 19 '18 at 21:42