Skip to content

Translate conditional requirements into JSON schema #32

@nealkruis

Description

@nealkruis

Here's a helpful post on the capabilities in JSON schema.

Here are the cases to handle:

Is defined

a:
  Data Type: "Boolean"
  Required: True
b:
  Data Type: "Numeric"  
  Required: "if a"
c:
  Data Type: "Numeric"  
  Required: True

gives

{
  "type": "object",
  "properties": {
    "a": { "type": "boolean" },
    "b": { "type": "number" },
    "c": { "type": "number" }
  },
  "required": ["a", "c"],
  "dependencies": {
    "a": ["b"]
  }
}

Is equal

a:
  Data Type: "Boolean"
  Required: True
b:
  Data Type: "Numeric"  
  Required: "if a=False"
c:
  Data Type: "Numeric"  
  Required: True

gives

{
  "type": "object",
  "properties": {
    "a": { "type": "boolean" },
    "b": { "type": "number" },
    "c": { "type": "number" }
  },
  "required": ["a", "c"],
  "if": {
      "properties": {
        "a": { "const": false} 
      }
  },
  "then": { "required": ["b"] }
}

Is not equal

a:
  Data Type: "Boolean"
  Required: True
b:
  Data Type: "Numeric"  
  Required: "if a!=False"
c:
  Data Type: "Numeric"  
  Required: True

gives

{
  "type": "object",
  "properties": {
    "a": { "type": "boolean" },
    "b": { "type": "number" },
    "c": { "type": "number" }
  },
  "required": ["a", "c"],
  "if": {
      "properties": {
        "a": { "not": {"const": false} }
      }
  },
  "then": { "required": ["b"] }
}

We may be able to find ways to combine these too. But I think covering these cases for now should be fine.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions