Skip to content

cookbook page, with solutions to frequently asked questions #190

Closed as not planned
@karenetheridge

Description

@karenetheridge

I am opening this as an issue rather than a PR to start, in order to gather feedback and additional content.

There are a number of questions that are asked frequently on stackoverflow, slack, github issues etc that come down to "how do I express this pattern in JSON Schema?" Here are a few of them, and I'm sure we can come up with more:

  • "evaluate this subschema but do not collect annotations" (not so common but it has come up a few times, and it is weird enough that it deserves a mention IMO):
    { not: { not: { ... subschema ... } } }

  • add descriptions to a bunch of enum values, e.g. for form generation:

  {
    anyOf: [
      {
        "description": "...",
        "const": "value0",
      },
      {
        "description": "...",
        "const": "value1",
      },
      ...
    ]
  }
  • simulate OpenAPI's "discriminator":
  {
    allOf: [
      {
        if: {
          type: object,
          required: [ "foo" ],
          properties: { foo: { const: "value0" } }
        },
        then: {
          .. subschema when data has foo: value0
        }
      },
      {
        if: {
          type: object,
          required: [ "foo" ],
          properties: { foo: { const: "value1" } }
        },
        then: {
          .. subschema when data has foo: value1
        }
      },
      {
        if: {
          type: object,
          required: [ "bar" ],
          properties: { bar: { const: "value0" } }
        },
        then: {
          .. subschema when data has bar: value0
        }
      },
      ...
    ],
}

Metadata

Metadata

Assignees

Labels

Priority: LowThis issue can probably be picked up by anyone.Status: In ProgressThis issue is being worked on, and has someone assigned.Status: StaleIt's believed that this issue is no longer important to the requestor.✨ EnhancementIndicates that the issue suggests an improvement or new feature.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions