From a908c3d4d24faca698065f59effaeeaedb98044f Mon Sep 17 00:00:00 2001 From: Mick Lawitzke Date: Fri, 12 Feb 2021 12:59:35 +0100 Subject: [PATCH 1/2] DiscriminatorDescriptor should accept number, too When using an enum as `name` it works fine by adding `//@ts-ignore` but the interface didnt allow that. ``` @ValidateNested() @Type(() => MyDto, { discriminator: { property: 'type', subTypes: [ {value: MySubDto1, name: MyEnum.Something}, {value: MySubDto2, name: MyEnum.SomethingOther}, ], }, keepDiscriminatorProperty: true, }) cupData: MySubDto1 | MySubDto2; ``` --- .../type-discriminator-descriptor.interface.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/interfaces/decorator-options/type-discriminator-descriptor.interface.ts b/src/interfaces/decorator-options/type-discriminator-descriptor.interface.ts index 04aae0ee6..cc1fe3387 100644 --- a/src/interfaces/decorator-options/type-discriminator-descriptor.interface.ts +++ b/src/interfaces/decorator-options/type-discriminator-descriptor.interface.ts @@ -18,7 +18,7 @@ export interface DiscriminatorDescriptor { /** * Name of the type. */ - name: string; + name: string | number; /** * A class constructor which can be used to create the object. From 701ebd15df3f7768348df543a704f74d9198152b Mon Sep 17 00:00:00 2001 From: Mick Lawitzke Date: Fri, 12 Feb 2021 13:03:57 +0100 Subject: [PATCH 2/2] Fix prettier (even tho I didnt change the readme) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 97d5a3e2b..fd80da7c9 100644 --- a/README.md +++ b/README.md @@ -374,7 +374,7 @@ let album = plainToClass(Album, albumJson); ### Providing more than one type option[⬆](#table-of-contents) In case the nested object can be of different types, you can provide an additional options object, -that specifies a discriminator. The discriminator option must define a `property` that holds the subtype +that specifies a discriminator. The discriminator option must define a `property` that holds the subtype name for the object and the possible `subTypes` that the nested object can converted to. A sub type has a `value`, that holds the constructor of the Type and the `name`, that can match with the `property` of the discriminator.