Our transition from using JSON Type Definition to JSON Schema includes the utilization of Quicktype to convert JSON Schemas into TypeScript Types. While Quicktype has been effective in most cases, it seems to struggle with converting Discriminators and more specifically the JSON Schema equivalent allOf with if-then scenarios, as it appears to disregard the allOf component altogether. Have we made an error in this process?
The command-line code we employ for converting JSON Schema to TypeScript is:
quicktype -o ./src/typings.ts --just-types --acronym-style camel --src-lang schema
Expected Output
export type CreateCustomerCommandPayloadV1 = CreateCustomerCommandPayloadV1Person | CreateCustomerCommandPayloadV1Company;
export interface CreateCustomerCommandPayloadV1Person {
type: CreateCustomerCommandPayloadV1Type.Person;
customerKey: string
firstName: string
lastName: string
}
export interface CreateCustomerCommandPayloadV1Company {
type: CreateCustomerCommandPayloadV1Type.Company;
customerKey: string
companyName: string
}
export enum CreateCustomerCommandPayloadV1Type {
Company = "COMPANY",
Person = "PERSON",
}
Actual Output
export interface CreateCustomerCommandPayloadV1 {
type: CreateCustomerCommandPayloadV1Type;
}
export enum CreateCustomerCommandPayloadV1Type {
Company = "COMPANY",
Person = "PERSON",
}
Details Regarding Our JSON Schema File:
{
"$schema": "http://json-schema.org/draft-07/schema",
"metadata": {
"description": "Command: Creates a new customer (Types: COMPANY | PERSON)",
"subject": "commands.crm.customers.createCustomer",
"authenticationRequired": true
},
"type": "object",
"additionalProperties": false,
"$id": "CreateCustomerCommandPayloadV1",
"properties": {
"type": {
"type": "string",
"enum": [
"COMPANY",
"PERSON"
]
},
"customerKey": {
"type": "string"
}
},
"required": [
"type"
],
"$comment": "discriminator",
"allOf": [
{
"if": {
"properties": {
"type": {
"const": "COMPANY"
}
}
},
"then": {
"properties": {
"companyName": {
"type": "string"
}
},
"required": [
"companyName"
]
}
},
{
"if": {
"properties": {
"type": {
"const": "PERSON"
}
}
},
"then": {
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
},
"type": "object",
"additionalProperties": false,
"title": "CreateCustomerCommandPayloadV1Person",
"required": [
"firstName",
"lastName"
]
}
}
]
}