Repository navigation
Open
Description
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:
- How to specify a discriminator with mapping in OpenApi/JsonSchema?
- How to specify the Person and Company schemas separately (not embedded within
oneOf/anyOf)? This is not needed ifmappingcan refer to the embedded schemas insideanyOf/oneOf
Thank you so much for the fantastic library!
Metadata
Metadata
Assignees
Labels
No labels
Type
Projects
- StatusShow more project fieldsTo do
Activity