Skip to content

Feature request: Conditionally Required Fields #1

@er1c

Description

@er1c

Consider this as a trite example:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "LoanOffer",
  "type": "object",
  "properties": {
    "status": {
      "type": "string",
      "enum": ["none", "active"]
    },
    "rate": { "type": "number" },
    "termYears": { "type": "integer", "enum": [15, 30] },
    "rateType": {
      "type": "string",
      "enum": ["fixed", "adjustable"]
    },
    "introYears": { "type": "integer", "enum": [3, 5] },
    "creditScore": { "type": "integer", "minimum": 300 },
    "downPayment": { "type": "number", "minimum": 0 }
  },
  "required": ["status"],
  "allOf": [
    {
      "if": {
        "properties": { "status": { "const": "none" } }
      },
      "then": {
        "required": ["creditScore", "downPayment"]
      }
    },
    {
      "if": {
        "properties": { "status": { "const": "active" } }
      },
      "then": {
        "required": ["rate", "termYears", "rateType"]
      }
    },
    {
      "if": {
        "properties": { "rateType": { "const": "adjustable" } }
      },
      "then": {
        "required": ["introYears"],
        "properties": {
          "termYears": { "const": 30 }
        }
      },
      "else": {
        "not": { "required": ["introYears"] }
      }
    }
  ]
}

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions