Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enforce object non emptyness using json schema

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

  1. is of object type,
  2. dont have any predefined properties
  3. can have properties of type string or numeric
  4. should not be empty

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?

like image 874
Mahesha999 Avatar asked Feb 26 '16 12:02

Mahesha999


People also ask

How do you make a property required in JSON Schema?

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.

What does allOf mean in JSON Schema?

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.

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 JSON Schema additionalProperties?

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.


1 Answers

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.

like image 99
cloudfeet Avatar answered Oct 06 '22 08:10

cloudfeet