# OasUnion

CLASS in `@skmtc/core 0.63.4`

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

## Signature

```ts
import { OasUnion } from '@skmtc/core'

class OasUnion extends OasBase
```

## Examples

Basic union type:

```ts
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
```

Discriminated union (tagged union):

```ts
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
```

Nullable union:

```ts
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
```

Complex nested union:

```ts
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<string, any> } | { error: string; code: number }
```

Using with references:

```ts
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
```

## Constructor

### constructor

```ts
constructor(fields: UnionFields, context?: ParseContextType)
```

## Properties

- `oasType` ('schema'): Object is part the 'schema' set which is used to define data types in an OpenAPI document.
- `type` ('union'): Constant value 'union' useful for type narrowing and tagged unions.
- `title` (string | undefined): A short summary of the union.
- `description` (string | undefined): A description of the union.
- `externalDocs` (OasExternalDocs | undefined): External documentation for the union.
- `nullable` (boolean | undefined): Indicates whether value can be null.
- `discriminator` (OasDiscriminator | undefined): Discriminator object used to tag member types and make the union a tagged union.
- `members` ((OasSchema | OasRef<'schema'>)[]): Array of schemas or references to schemas that are part of the union.
- `extensionFields` (Record<string, unknown> | undefined): Specification Extension fields
- `example` (unknown): An example of the union type.
- `default` (unknown): The default value of the union type.
- `not` (OasUnknown | undefined): The schema's `not`, kept as an `OasUnknown` under `skmtc.config.json` `parse.not: unknown`: the document says "not something", and core does not read what. `undefined` otherwise.

## Methods

### isRef

```ts
isRef(): this is OasRef<'schema'>
```

### isReadOnly

```ts
isReadOnly(): boolean
```

Always `false`: this schema has no `readOnly`.

### isWriteOnly

```ts
isWriteOnly(): boolean
```

Always `false`: this schema has no `writeOnly`.

### clone

```ts
clone(ref?: OasRef<'schema'>): OasUnion
```

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.

### traverse

```ts
traverse(path: SchemaPath): OasSchema | OasRef<'schema'>
```

### resolve

```ts
resolve(): OasUnion
```

### resolveOnce

```ts
resolveOnce(): OasUnion
```

### isDiscriminable

```ts
isDiscriminable(): boolean
```

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 `parse.danglingRefs` lists `schema`).

It reads the schemas only. A generator may refuse more: one that leaves
out `readOnly` properties cannot discriminate on one.

### toJsonSchema

```ts
toJsonSchema(options: ToJsonSchemaOptions): OpenAPIV3.NonArraySchemaObject
```
