# OasObject

CLASS in `@skmtc/core 0.63.4`

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

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

class OasObject<Nullable extends boolean | undefined = boolean | undefined> extends OasBase
```

- `Nullable`: Whether the object value itself can be null

## Examples

Basic object schema:

```ts
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
});
```

Dynamic property management:

```ts
// 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');
```

Record-style object (additional properties):

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

Nullable object support:

```ts
const nullableUser = new OasObject<true>({
  nullable: true,
  properties: {
    name: new OasString()
  },
  default: null // Can have null default when nullable
});

// This represents: { name: string } | null
```

## Constructor

### constructor

```ts
constructor(fields?: OasObjectFields<Nullable>, context?: ParseContextType)
```

## Properties

- `oasType` ('schema'): Object is part the 'schema' set which is used to define data types in an OpenAPI document.
- `type` ('object'): Constant value 'object' useful for type narrowing and tagged unions.
- `title` (string | undefined): A short summary of the object.
- `description` (string | undefined): A description of the object.
- `externalDocs` (OasExternalDocs | undefined): External documentation for the object.
- `nullable` (Nullable | undefined): Indicates whether value can be null.
- `properties` (Nullable extends true ? Record<string, OasSchema | OasRef<'schema'> | CustomValue> | null | undefined : Record<string, OasSchema | OasRef<'schema'> | CustomValue> | undefined): A record which maps property names of the object to their schemas.
- `required` (string[] | undefined): An array of required property names.
- `additionalProperties` (boolean | OasSchema | OasRef<'schema'> | undefined): Indicates whether additional properties are allowed. This is equivalent to a Record type in TypeScript.
- `extensionFields` (Record<string, unknown> | undefined): Specification Extension fields
- `example` (Nullable extends true ? Record<string, unknown> | null | undefined : Record<string, unknown> | undefined): An example of the object.
- `default` (Nullable extends true ? Record<string, unknown> | null | undefined : Record<string, unknown> | undefined): The default value of the object.
- `maxProperties` (number | undefined): Maximum number of properties allowed in the object
- `minProperties` (number | undefined): Minimum number of properties required in the object
- `readOnly` (boolean | undefined): Whether the object is read-only
- `writeOnly` (boolean | undefined): Whether the object is write-only
- `deprecated` (boolean | undefined): Whether the object schema is deprecated
- `enums` (Nullable extends true ? (Record<string, unknown> | null)[] | undefined : Record<string, unknown>[] | undefined): Array of valid enum values for the object
- `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

### OasObject.empty

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

Returns: A new empty OasObject instance

### addProperty

```ts
addProperty(args: AddPropertyArgs): OasObject
```

Adds 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.

- `args`: Property addition arguments
- `args.name`: The name of the property to add
- `args.schema`: The schema definition for the property
- `args.required`: Whether the property should be required (default: false)

Returns: A new OasObject with the added property

### removeProperty

```ts
removeProperty(name: string): OasObject
```

Removes 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.

- `name`: The name of the property to remove

Returns: A new OasObject with the property removed, or the same instance if property doesn't exist

### isRef

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

### isReadOnly

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

Whether the value is `readOnly`: sent by the API only, never in a request.

### isWriteOnly

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

Whether the value is `writeOnly`: sent in a request only, never by the API.

### clone

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

A 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

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

### resolve

```ts
resolve(): OasObject
```

### resolveOnce

```ts
resolveOnce(): OasObject
```

### toJsonSchema

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

Converts 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.

- `options`: Conversion options for handling references and context

Returns: A JSON Schema representation of the object
