Open Janis-Hahn opened 11 months ago
It looks to me like you need to use oneOf (must match one and only one of the provided schemas) instead of allOf:
{
"$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
},
"$id": "CreateCustomerCommandPayloadV1",
"oneOf": [
{
"type": "object",
"additionalProperties": false,
"title": "CreateCustomerCommandPayloadV1Company",
"properties": {
"type": {
"type": "string",
"const": "COMPANY"
},
"customerKey": {
"type": "string"
},
"companyName": {
"type": "string"
}
},
"required": [
"type",
"companyName"
]
},
{
"type": "object",
"additionalProperties": false,
"title": "CreateCustomerCommandPayloadV1Person",
"properties": {
"type": {
"type": "string",
"const": "PERSON"
},
"customerKey": {
"type": "string"
},
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
}
},
"required": [
"type",
"firstName",
"lastName"
]
}
]
}
The schemas inside the oneOf could themselves be composed via allOf to reduce the duplication -- mainly just illustrating that you can/should use oneOf for a conditional case (and its worth reading about the difference between oneOf, allOf and anyOf (anyOf would probably give you a single object with optional properties for everything from both subschemas, where oneOf should give you two separate objects.
Not familiar with that metadata element. Usually I'd expect title/description field in the top-level of the schema. https://json-schema.org/draft-06/json-schema-validation#rfc.section.7
(n.b. Not associated with the project but happen to be working on something similar)
We are changing from JSON Type Definition to JSON Schema and are now using Quicktype to convert the JSON Schemas to TypeScript Types. For the most part, Quicktype is doing its job very well, but aparently it can't convert Discriminators, or the JSON Schema equivalent allOf with if-then. It seems to just ignore the allOf... Did we do anything wrong?
The bash command we use to convert JSON Schema to TypeScript:
Expected Result
Actual Result
Our JSON Schema File: