Open
Description
I have an array of contributors on an author
, and other similar, properties.
I would like to be able to discriminate between Organization
and Person
Contributors, without having to adding a common property to the JSON.
Here's the code:
export abstract class Contributor {
name: string;
constructor(name: string) {
this.name = name;
}
}
export class Organization extends Contributor {
name: string;
location?: string;
constructor(name: string, location?: string) {
super(name);
this.location = location;
}
sortName(): string {
return this.name;
}
}
export class Person extends Contributor {
name: string;
familyName: string;
givenName: string;
constructor(name: string, givenName: string, familyName: string) {
super(name);
this.givenName = givenName;
this.familyName = familyName;
}
displayName(initialize: boolean): string {
return `${this.givenName} ${this.familyName}`;
}
sortName(): string {
return `${this.familyName}, ${this.givenName}`;
}
}
... and then, on a Reference
class:
@Type(() => Contributor, {
discriminator: {
property: 'type',
subTypes: [
{ value: Person, name: 'person' },
{ value: Organization, name: 'organization' },
],
},
})
author?: (Person | Organization)[];
And it all works, except I'd rather not add the "type" property, since I already know how to distinguish them based on their properties.
This particular model is likely to be edited by humans, so I want to keep the JSON schema I export from it as clean as possible.
So is there a way to have my cake and eat it too?
Or alternately, have a default mapping, if the type
property isn't there?