Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a "patternProperty" required in JSON Schema (Ruby)

Consider the following JSON :

 {
      "1234abcd" : {
                     "model" : "civic"
                      "made" : "toyota"
                      "year" : "2014"
                     }

 }

consider another JSON :

 {
      "efgh56789" : {
                     "model" : "civic"
                      "made" : "toyota"
                      "year" : "2014"
                     }

 }

the outermost alphanumeric key will vary and required, if the key was fixed; let's say "identifier" then the schema was straightforward, however since the key-name is variable, we have to use patternProperties, how can I come up with a schema that captures these requirement for the outermost key:

  1. property name (key) is variable
  2. required
  3. alphanumeric lowercase

using json-schema : https://github.com/ruby-json-schema/json-schema in ruby.

like image 455
EasyQuestions Avatar asked Jun 11 '15 00:06

EasyQuestions


People also ask

What is required in JSON Schema?

Required PropertiesThe 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.

How do I specify a schema in a JSON file?

If you want to use a custom schema of your own that's part of your project, just click on your schema in Solution Explorer and then drag it to that dropdown list. Alternatively, you can use the $schema keyword in a JSON file to associate it with a schema.

What is anyOf in JSON Schema?

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.

What does exclusion minimum property in JSON Schema mean?

ExclusiveMinimum Property. Gets or sets a flag indicating whether the value can not equal the number defined by the minimum attribute (Minimum). Namespace: Newtonsoft.Json.Schema.


2 Answers

The best you can do to require properties when those properties are variable is to use minProperties and maxProperties. If you want to say there must be one and only one of these alphanumeric keys in your object, you can use the following schema. If you want to say there has to be at least one, you could just leave out maxProperties.

{
  "type": "object",
  "patternProperties": {
    "^[a-z0-9]+$": {
      "type": "object",
      "properties": {
        "model": { "type": "string" },
        "make": { "type": "string" },
        "year": { "type": "string" }
      },
      "required": ["model", "make", "year"]
    }
  },
  "additionalProperties": false,
  "maxProperties": 1,
  "minProperties": 1
}
like image 132
Jason Desrosiers Avatar answered Sep 29 '22 13:09

Jason Desrosiers


You may have to change the regular expression to fit your valid keys:

{
"patternProperties": {
    "^[a-zA-Z0-9]*$":{
        "properties": {
              "model":{"type":"string"},
              "made":{"type":"string"},
              "year":{"type":"string"}
         }
    }
},
"additionalProperties":false
}
like image 22
jruizaranguren Avatar answered Sep 29 '22 12:09

jruizaranguren