@tsed/schema
Version:
JsonSchema module for Ts.ED Framework
76 lines (75 loc) • 2.03 kB
TypeScript
import { Type } from "@tsed/core";
/**
* Manages discriminator mappings for polymorphic type resolution.
*
* A discriminator enables polymorphism in schemas by using a specific property
* value to determine which subtype schema to apply. This is essential for
* inheritance hierarchies and union types in OpenAPI specifications.
*
* ### Concept
*
* When a base class has multiple derived classes, the discriminator property
* (e.g., "type", "kind") indicates which concrete class an object represents.
*
* ### Usage
*
* ```typescript
* // Base class
* @DiscriminatorKey("type")
* abstract class Animal {
* type: string;
* }
*
* // Derived classes
* @DiscriminatorValue("dog")
* class Dog extends Animal {
* breed: string;
* }
*
* @DiscriminatorValue("cat")
* class Cat extends Animal {
* indoor: boolean;
* }
* ```
*
* ### OpenAPI Mapping
*
* Generates OpenAPI discriminator objects:
* ```json
* {
* "discriminator": {
* "propertyName": "type",
* "mapping": {
* "dog": "#/components/schemas/Dog",
* "cat": "#/components/schemas/Cat"
* }
* }
* }
* ```
*
* ### Key Features
*
* - **Type Mapping**: Maps discriminator values to TypeScript classes
* - **Bidirectional Lookup**: Find type by value or values by type
* - **Default Values**: Get the default discriminator value for a type
* - **Children Discovery**: List all derived types
*
* @public
*/
export declare class Discriminator {
propertyName: string;
base: Type<any>;
values: Map<string, Type>;
types: Map<Type, string[]>;
constructor({ base, propertyName, types, values }?: Partial<{
base: Type<any>;
propertyName: string;
values: Map<string, Type<any>>;
types: Map<Type, string[]>;
}>);
add(type: Type<string>, values: string[]): this;
getType(discriminatorValue: string): Type<any>;
getValues(type: Type): string[] | undefined;
getDefaultValue(type: Type<any>): string | undefined;
children(): Type<any>[];
}