Skip to content

Add conditional and dependency keywords #44

Description

@crmne

Goal

Add support for JSON Schema conditional and dependency keywords:

  • dependentRequired
  • dependentSchemas
  • if
  • then
  • else

These keywords let schemas express cross-property requirements, such as requiring billing details when a credit card field is present, or requiring state/country fields depending on a condition.

Proposed DSL

Inline property dependencies for simple dependentRequired output:

number :credit_card, required: false, requires: %i[billing_address cvv]

Block-based dependencies when additional validation is needed:

dependent :credit_card do
  requires :billing_address
  validates :billing_address, type: :string, min_length: 1
end

Conditional if / then / else output through a Ruby-friendly DSL:

given domestic: true do
  requires :state

  otherwise do
    requires :country
  end
end

Acceptance criteria

  • Works at root schema output.
  • Works in nested inline schemas.
  • Works in reusable schemas under $defs.
  • Inline requires: emits dependentRequired.
  • dependent with validations emits dependentSchemas.
  • given emits if / then / optional else.
  • Behavior tests cover then, else, dependentRequired, and dependentSchemas.
  • Generated schemas validate against Draft 2020-12 after the final output contract is decided.

Related work

PR #31 is expected to implement this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions