import type { OasUnknown } from '../unknown/Unknown.js'; import type { SchemaPath } from '../../types/SchemaPath.js'; import type { OasDiscriminator } from '../discriminator/Discriminator.js'; import type { OasExternalDocs } from '../externalDocs/ExternalDocs.js'; import type { OasSchema } from '../schema/Schema.js'; import type { OasRef } from '../ref/Ref.js'; import type { ToJsonSchemaOptions } from '../schema/Schema.js'; import type { OpenAPIV3 } from 'openapi-types'; import { OasBase } from '../../types/OasBase.js'; import type { ParseContextType } from '../../context/parseTypes.js'; /** * Constructor fields for {@link OasUnion}. */ export type UnionFields = { /** A short summary of the union type */ title?: string; /** A description of the union type */ description?: string; /** External documentation for the union type */ externalDocs?: OasExternalDocs | undefined; /** Whether the union value can be null */ nullable?: boolean; /** Discriminator object for creating tagged unions */ discriminator?: OasDiscriminator; /** Example value for the union */ example?: unknown; /** Default value for the union */ default?: unknown; /** Array of schemas that make up the union members */ members: (OasSchema | OasRef<'schema'>)[]; /** Custom extension fields (x-* properties) */ extensionFields?: Record; }; /** * Represents a union type schema in the OpenAPI Specification. * * `OasUnion` handles both OpenAPI `oneOf` and `anyOf` constructs by mapping them * to TypeScript union types. While OpenAPI distinguishes between these concepts, * in TypeScript they both represent union types (A | B | C), making the distinction * less meaningful for code generation. * * This class supports both simple unions and discriminated (tagged) unions through * the discriminator property, which enables more precise type narrowing in generated code. * * ## Key Features * * - **Union Types**: Represents multiple possible schema types as a single union * - **Tagged Unions**: Supports discriminator properties for type narrowing * - **Reference Resolution**: Handles references to other schemas within union members * - **Nullable Support**: Can represent nullable union types (A | B | null) * - **JSON Schema**: Converts to standard JSON Schema format for validation * * @example Basic union type * ```typescript * import { OasUnion, OasString, OasInteger } from '@skmtc/core'; * * const stringOrNumber = new OasUnion({ * title: 'StringOrNumber', * description: 'A value that can be either a string or number', * members: [ * new OasString({ title: 'String Value' }), * new OasInteger({ title: 'Integer Value' }) * ] * }); * * // This represents: string | number * ``` * * @example Discriminated union (tagged union) * ```typescript * const shape = new OasUnion({ * title: 'Shape', * description: 'Different types of geometric shapes', * discriminator: new OasDiscriminator({ * propertyName: 'type', * mapping: { * 'circle': '#/components/schemas/Circle', * 'square': '#/components/schemas/Square' * } * }), * members: [ * new OasRef({ $ref: '#/components/schemas/Circle' }), * new OasRef({ $ref: '#/components/schemas/Square' }) * ] * }); * * // This creates a tagged union that can be narrowed by the 'type' property * ``` * * @example Nullable union * ```typescript * const nullableStatus = new OasUnion({ * title: 'NullableStatus', * nullable: true, * members: [ * new OasString({ enum: ['active', 'inactive'] }), * new OasString({ enum: ['pending', 'suspended'] }) * ], * default: null * }); * * // This represents: ('active' | 'inactive' | 'pending' | 'suspended') | null * ``` * * @example Complex nested union * ```typescript * const apiResponse = new OasUnion({ * title: 'ApiResponse', * description: 'Response from API endpoint', * members: [ * new OasObject({ * title: 'SuccessResponse', * properties: { * success: new OasBoolean({ default: true }), * data: new OasObject({ additionalProperties: true }) * } * }), * new OasObject({ * title: 'ErrorResponse', * properties: { * error: new OasString(), * code: new OasInteger() * } * }) * ] * }); * * // This represents: { success: boolean; data: Record } | { error: string; code: number } * ``` * * @example Using with references * ```typescript * const userOrAdmin = new OasUnion({ * title: 'UserOrAdmin', * description: 'Either a regular user or an admin user', * members: [ * new OasRef({ $ref: '#/components/schemas/User' }), * new OasRef({ $ref: '#/components/schemas/Admin' }) * ] * }); * * // References will be resolved during processing * // This represents: User | Admin * ``` */ export declare class OasUnion extends OasBase { /** * Object is part the 'schema' set which is used * to define data types in an OpenAPI document. */ oasType: "schema"; /** * Constant value 'union' useful for type narrowing and tagged unions. */ type: "union"; /** * A short summary of the union. */ title: string | undefined; /** * A description of the union. */ description: string | undefined; /** * External documentation for the union. */ externalDocs: OasExternalDocs | undefined; /** * Indicates whether value can be null. */ nullable: boolean | undefined; /** * Discriminator object used to tag member types and make the union a tagged union. */ discriminator: OasDiscriminator | undefined; /** * Array of schemas or references to schemas that are part of the union. */ members: (OasSchema | OasRef<'schema'>)[]; /** Specification Extension fields */ extensionFields: Record | undefined; /** * An example of the union type. */ example?: unknown; /** * The default value of the union type. */ default?: unknown; /** * The schema's `not`, kept as an `OasUnknown` under `client.json` * `system.not: unknown`: the document says "not something", and core does * not read what. `undefined` otherwise. */ not: OasUnknown | undefined; constructor(fields: UnionFields, context?: ParseContextType); isRef(): this is OasRef<'schema'>; /** Always `false`: this schema has no `readOnly`. */ isReadOnly(): boolean; /** Always `false`: this schema has no `writeOnly`. */ isWriteOnly(): boolean; /** * A copy of this schema with what `ref` adds at its site applied: its * `nullable` and the keywords next to it (`SiblingKeywords`) that * this type takes, a sibling's value winning. A value that does not fit * this type is left out (the parser reports it). For `OasRef.resolve()`. * This schema does not change, and the copy shares its child schemas. */ clone(ref?: OasRef<'schema'>): OasUnion; traverse(path: SchemaPath): OasSchema | OasRef<'schema'>; resolve(): OasUnion; resolveOnce(): OasUnion; /** * Whether the discriminator alone picks the member of a value: the union * names a discriminator, and each member, followed through references, is * an object that is not nullable, that requires the discriminator's * property, and that allows only string literals for it. No value may * belong to two members. A member that is a union, or anything other than * an object, makes it `false`, as does a reference on the way that names * nothing in the document (kept when `system.danglingRefs` lists `schema`). * * It reads the schemas only. A generator may refuse more: one that leaves * out `readOnly` properties cannot discriminate on one. */ isDiscriminable(): boolean; toJsonSchema(options: ToJsonSchemaOptions): OpenAPIV3.NonArraySchemaObject; }