Schemas
OasUnion
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
import { OasUnion } from '@skmtc/core'
class OasUnion extends OasBaseExamples
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 | numberconst 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' propertyconst 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') | nullconst 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 }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 | AdminConstructor
constructor
constructor(fields: UnionFields, context?: ParseContextType)Properties
Object is part the 'schema' set which is used to define data types in an OpenAPI document.
Constant value 'union' useful for type narrowing and tagged unions.
A short summary of the union.
A description of the union.
External documentation for the union.
Indicates whether value can be null.
Discriminator object used to tag member types and make the union a tagged union.
Array of schemas or references to schemas that are part of the union.
Specification Extension fields
An example of the union type.
The default value of the union type.
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
isRef(): this is OasRef<'schema'>isReadOnly
isReadOnly(): booleanAlways false: this schema has no readOnly.
isWriteOnly
isWriteOnly(): booleanAlways false: this schema has no writeOnly.
clone
clone(ref?: OasRef<'schema'>): OasUnionA 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
traverse(path: SchemaPath): OasSchema | OasRef<'schema'>resolve
resolve(): OasUnionresolveOnce
resolveOnce(): OasUnionisDiscriminable
isDiscriminable(): booleanWhether 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
toJsonSchema(options: ToJsonSchemaOptions): OpenAPIV3.NonArraySchemaObject