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"?
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.
The patternProperties is used to restrict the names of extra properties. But the additionalProperties "false" means no extra properies.
Gets or sets a flag indicating whether the value can not equal the number defined by the minimum attribute (Minimum).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With