Skip to content

Producing JsonSchema with discriminator field #1465

Open

Description

@kaisellgren

What a wonderful library!

However, I am struggling to find a way to produce OpenApi spec with the following schemas:

Company

{
  "type": "object",
  "properties": {
    "businessId": {
      "type": "string"
    },
    "companyName": {
      "type": "string"
    },
    "type": {
      "const": "company"
    }
  },
  "required": ["businessId", "companyName", "type"]
}

Person

{
  "type": "object",
  "properties": {
    "familyName": {
      "type": "string"
    },
    "givenName": {
      "type": "string"
    },
    "personalIdentityCode": {
      "type": "string"
    },
    "type": {
      "const": "person"
    }
  },
  "required": [
    "familyName",
    "givenName",
    "personalIdentityCode",
    "type"
  ]
}

And then I need to combine them with using a discriminator:

{
  "type": "array",
  "items": [
    {
      "oneOf": [
        {
          "$ref": "#/components/schemas/Person"
        },
        {
          "$ref": "#/components/schemas/Company"
        }
      ],
      "discriminator": {
        "propertyName": "type",
        "mapping": {
          "person": "#/components/schemas/Person",
          "company": "#/components/schemas/Company"
        }
      }
    }
  ]
}

I understand that I can use .configure({ name: 'Person' }) to embed information about the name of the schema, but I haven't figured out if it's possible to define the discriminator somehow?

Here's my code right now, which is close to what I need:

const Person = type({
  type: "'person'",
  givenName: 'string',
  familyName: 'string',
  personalIdentityCode: 'string',
}).configure({ name: 'Person' })

const Company = type({
  type: "'company'",
  companyName: 'string',
  businessId: 'string',
}).configure({ name: 'Company' })

const Customer = Person.or(Company).configure({
  discriminator: {
    propertyName: 'type',
    mapping: {
      person: '#/components/schemas/Person',
      company: '#/components/schemas/Company',
    },
  },
})

console.log(JSON.stringify(Customer.toJsonSchema(), null, 2))

it produces this:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "anyOf": [
    {
      "type": "object",
      "name": "Company",
      "discriminator": "$ark.object189",
      "properties": {
        "businessId": {
          "type": "string"
        },
        "companyName": {
          "type": "string"
        },
        "type": {
          "const": "company"
        }
      },
      "required": [
        "businessId",
        "companyName",
        "type"
      ]
    },
    {
      "type": "object",
      "name": "Person",
      "properties": {
        "familyName": {
          "type": "string"
        },
        "givenName": {
          "type": "string"
        },
        "personalIdentityCode": {
          "type": "string"
        },
        "type": {
          "const": "person"
        }
      },
      "required": [
        "familyName",
        "givenName",
        "personalIdentityCode",
        "type"
      ],
      "discriminator": "$ark.object189"
    }
  ],
  "discriminator": "$ark.object189"
}

My questions are:

  1. How to specify a discriminator with mapping in OpenApi/JsonSchema?
  2. How to specify the Person and Company schemas separately (not embedded within oneOf/anyOf)? This is not needed if mapping can refer to the embedded schemas inside anyOf/oneOf

Thank you so much for the fantastic library!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions