Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Schema: verifying object's values, without keys

Not to confuse anybody, I'll start with validating arrays...

Regarding arrays, JSON Schema can check whether elements of an (((...)sub)sub)array conform to a structure:

"type": "array",
"items": {
  ...
}

When validating objects, I know I can pass certain keys with their corresponding value types, such as:

"type": "object",
"properties": {
  // key-value pairs, might also define subschemas
}

But what if I've got an object which I want to use to validate values only (without keys)?

My real-case example is that I'm configuring buttons: there might be edit, delete, add buttons and so on. They all have specific, rigid structure, which I do have JSON schema for. But I don't want to limit myself to ['edit', 'delete', 'add'] only, there might be publish or print in the future. But I know they all will conform to my subschema.

Each button is:

BUTTON = {
  "routing": "...",
  "params": { ... },
  "className": "...",
  "i18nLabel": "..."
}

And I've got an object (not an array) of buttons:

{
  "edit": BUTTON,
  "delete": BUTTON,
  ...
}

How can I write such JSON schema? Is there any way of combining object with items (I know there are object-properties and array-items relations).

like image 465
ducin Avatar asked Jun 29 '15 13:06

ducin


1 Answers

You can use additionalProperties for this. If you set additionalProperties to a schema instead of a boolean, then any properties that aren't explicitly declared using the properties or patternProperties keywords must match the given schema.

{
  "type": "object",
  "additionalProperties": {
    ... BUTTON SCHEMA ...
  }
}

http://json-schema.org/latest/json-schema-validation.html#anchor64

like image 77
Jason Desrosiers Avatar answered Sep 18 '22 15:09

Jason Desrosiers