We can enforce empty attribute of type object as follows:
{
"description": "voice mail record",
"type": "object",
"additionalProperties": false,
"properties": {}
}
as explained here.
Now I want to validate attribute which
Enforcing non-emptyness (point 4) is what I am unable to guess. This is somewhat opposite of enforcing emptyness as in above example. My current json schema excerpt looks like this:
"attribute":
{
"type": "object",
"additionalProperties": { "type": ["string","number","integer"] }
}
But above does not enforce non-emptyness. How can I accomplish this?
Required Properties By default, the properties defined by the properties keyword are not required. However, one can provide a list of required properties using the required keyword. The required keyword takes an array of zero or more strings. Each of these strings must be unique.
The keywords used to combine schemas are: allOf: (AND) Must be valid against all of the subschemas. anyOf: (OR) Must be valid against any of the subschemas. oneOf: (XOR) Must be valid against exactly one of the subschemas.
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 value of "additionalProperties" MUST be a boolean or an object. If it is an object, it MUST also be a valid JSON Schema. The value of "properties" MUST be an object. Each value of this object MUST be an object, and each object MUST be a valid JSON Schema.
Sounds like minProperties
is what you want.
{
"type": "object",
"additionalProperties": {"type": ["string", "number", "integer"]},
"minProperties": 1
}
There is also maxProperties
, which can be used as an alternative solution to the opposite question that you linked to.
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