Schemas
OasObject
Represents an object schema in the OpenAPI Specification.
OasObject handles both:
- Objects: Types with fixed, named properties (like TypeScript interfaces)
- Records: Types with dynamic keys and consistent value types (like TypeScript Record<string, T>)
This class provides comprehensive support for object validation constraints, property management, and JSON Schema conversion. It supports nullable types through generic type parameters and handles complex property relationships.
Key Features
- Property Management: Add/remove properties with automatic required field handling
- Type Safety: Generic nullable type support with proper TypeScript inference
- Validation: Min/max properties, additional properties, and enum constraints
- JSON Schema: Convert to standard JSON Schema format for validation
- Immutability: All mutations return new instances (functional style)
Signature
import { OasObject } from '@skmtc/core'
class OasObject<Nullable extends boolean | undefined = boolean | undefined> extends OasBaseWhether the object value itself can be null
Examples
import { OasObject } from '@skmtc/core';
const userObject = new OasObject({
title: 'User',
description: 'A user in the system',
properties: {
id: new OasString({ title: 'User ID' }),
name: new OasString({ title: 'Full Name' }),
email: new OasString({ format: 'email' })
},
required: ['id', 'name'],
additionalProperties: false
});// Start with empty object
let schema = OasObject.empty();
// Add properties dynamically
schema = schema.addProperty({
name: 'id',
schema: new OasString(),
required: true
});
schema = schema.addProperty({
name: 'metadata',
schema: new OasObject({ additionalProperties: true }),
required: false
});
// Remove a property
schema = schema.removeProperty('metadata');const recordObject = new OasObject({
title: 'StringMap',
description: 'A map of string keys to string values',
additionalProperties: new OasString(), // Any string key -> string value
minProperties: 1 // At least one property required
});
// This allows: { [key: string]: string }const nullableUser = new OasObject<true>({
nullable: true,
properties: {
name: new OasString()
},
default: null // Can have null default when nullable
});
// This represents: { name: string } | nullConstructor
constructor
constructor(fields?: OasObjectFields<Nullable>, context?: ParseContextType)Properties
Object is part the 'schema' set which is used to define data types in an OpenAPI document.
Constant value 'object' useful for type narrowing and tagged unions.
A short summary of the object.
A description of the object.
External documentation for the object.
Indicates whether value can be null.
A record which maps property names of the object to their schemas.
An array of required property names.
Indicates whether additional properties are allowed.
This is equivalent to a Record type in TypeScript.
Specification Extension fields
An example of the object.
The default value of the object.
Maximum number of properties allowed in the object
Minimum number of properties required in the object
Whether the object is read-only
Whether the object is write-only
Whether the object schema is deprecated
Array of valid enum values for the object
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
OasObject.empty
OasObject.empty(): OasObject<false>Creates a new empty OasObject with no properties.
This factory method creates a non-nullable object with empty properties and required arrays, useful as a starting point for dynamic object building.
A new empty OasObject instance
addProperty
addProperty(args: AddPropertyArgs): OasObjectAdds a new property to the object.
This method returns a new OasObject instance with the added property, following an immutable pattern. If the property is marked as required, it will be added to the required array.
Property addition arguments
The name of the property to add
The schema definition for the property
Whether the property should be required (default: false)
A new OasObject with the added property
removeProperty
removeProperty(name: string): OasObjectRemoves a property from the object.
This method returns a new OasObject instance with the specified property removed. If the property was required, it will also be removed from the required array. If the property doesn't exist, returns the same instance.
The name of the property to remove
A new OasObject with the property removed, or the same instance if property doesn't exist
isRef
isRef(): this is OasRef<'schema'>isReadOnly
isReadOnly(): booleanWhether the value is readOnly: sent by the API only, never in a request.
isWriteOnly
isWriteOnly(): booleanWhether the value is writeOnly: sent in a request only, never by the API.
clone
clone(ref?: OasRef<'schema'>): OasObjectA copy of this schema with what ref adds at its site applied: its nullable, readOnly, writeOnly 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(): OasObjectresolveOnce
resolveOnce(): OasObjecttoJsonSchema
toJsonSchema(options: ToJsonSchemaOptions): OpenAPIV3.NonArraySchemaObjectConverts the OasObject to a standard JSON Schema object.
This method serializes the object to the JSON Schema format used in OpenAPI specifications. It handles property conversion, additional properties rules, and validation constraints.
Conversion options for handling references and context
A JSON Schema representation of the object