import type { OasRef } from '../ref/Ref.js'; import type { OasSchema, ToJsonSchemaOptions } from '../schema/Schema.js'; import type { CustomValue } from '../../types/CustomValue.js'; import type { OpenAPIV3 } from 'openapi-types'; /** * Constructor fields for {@link OasObject}. * * @template Nullable - Whether the object can be null (affects type unions) */ export type OasObjectFields = { /** A short summary of the object */ title?: string; /** A description of the object */ description?: string; /** Record mapping property names to their schemas */ properties?: Record | CustomValue> | undefined; /** Array of required property names */ required?: string[] | undefined; /** Default value for the object (null allowed if Nullable is true) */ default?: Nullable extends true ? Record | null | undefined : Record | undefined; /** Whether additional properties are allowed (true/false/schema) */ additionalProperties?: boolean | OasSchema | OasRef<'schema'> | undefined; /** Whether the object value can be null */ nullable?: Nullable; /** Maximum number of properties allowed */ maxProperties?: number; /** Minimum number of properties required */ minProperties?: number; /** Array of valid enum values for the object */ enums?: Nullable extends true ? (Record | null)[] | undefined : Record[] | undefined; /** Custom extension fields (x-* properties) */ extensionFields?: Record; /** Example value for the object (null allowed if Nullable is true) */ example?: Nullable extends true ? Record | null | undefined : Record | undefined; /** Whether the object is read-only */ readOnly?: boolean; /** Whether the object is write-only */ writeOnly?: boolean; /** Whether the object is deprecated */ deprecated?: boolean; }; /** * Arguments for the {@link OasObject.addProperty} method. */ export type AddPropertyArgs = { /** The name of the property to add */ name: string; /** The schema definition for the property */ schema: OasSchema | OasRef<'schema'> | CustomValue | undefined; /** Whether the property should be required */ required?: boolean; }; /** * 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) * * 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) * * @template Nullable - Whether the object value itself can be null * * @example Basic object schema * ```typescript * 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 * }); * ``` * * @example Dynamic property management * ```typescript * // 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'); * ``` * * @example Record-style object (additional properties) * ```typescript * 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 } * ``` * * @example Nullable object support * ```typescript * const nullableUser = new OasObject({ * nullable: true, * properties: { * name: new OasString() * }, * default: null // Can have null default when nullable * }); * * // This represents: { name: string } | null * ``` */ export declare class OasObject { /** * Object is part the 'schema' set which is used * to define data types in an OpenAPI document. */ oasType: "schema"; /** * Constant value 'object' useful for type narrowing and tagged unions. */ type: "object"; /** * A short summary of the object. */ title: string | undefined; /** * A description of the object. */ description: string | undefined; /** * Indicates whether value can be null. */ nullable: Nullable | undefined; /** * A record which maps property names of the object to their schemas. */ properties: Nullable extends true ? Record | CustomValue> | null | undefined : Record | CustomValue> | undefined; /** * An array of required property names. */ required: string[] | undefined; /** * Indicates whether additional properties are allowed. * * This is equivalent to a Record type in TypeScript. */ additionalProperties: boolean | OasSchema | OasRef<'schema'> | undefined; /** Specification Extension fields */ extensionFields: Record | undefined; /** * An example of the object. */ example: Nullable extends true ? Record | null | undefined : Record | undefined; /** * The default value of the object. */ default: Nullable extends true ? Record | null | undefined : Record | undefined; /** Maximum number of properties allowed in the object */ maxProperties?: number; /** Minimum number of properties required in the object */ minProperties?: number; /** Whether the object is read-only */ readOnly?: boolean; /** Whether the object is write-only */ writeOnly?: boolean; /** Whether the object schema is deprecated */ deprecated?: boolean; /** Array of valid enum values for the object */ enums?: Nullable extends true ? (Record | null)[] | undefined : Record[] | undefined; /** * Creates a new OasObject instance. * * @param fields - Object configuration fields * * @example * ```typescript * const userSchema = new OasObject({ * title: 'User', * properties: { * id: new OasString({ title: 'ID' }), * name: new OasString({ title: 'Name' }) * }, * required: ['id'], * additionalProperties: false * }); * ``` */ constructor(fields?: OasObjectFields); /** * 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 * * @example * ```typescript * // Start with empty object and build up * let schema = OasObject.empty(); * * schema = schema.addProperty({ * name: 'id', * schema: new OasString(), * required: true * }); * * schema = schema.addProperty({ * name: 'name', * schema: new OasString(), * required: true * }); * ``` */ static empty(): 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. * * @param args - Property addition arguments * @param args.name - The name of the property to add * @param args.schema - The schema definition for the property * @param args.required - Whether the property should be required (default: false) * @returns A new OasObject with the added property * * @example Adding a simple property * ```typescript * const original = OasObject.empty(); * const withName = original.addProperty({ * name: 'username', * schema: new OasString({ minLength: 3 }), * required: true * }); * * console.log(withName.required); // ['username'] * ``` * * @example Chaining property additions * ```typescript * const userSchema = OasObject.empty() * .addProperty({ * name: 'id', * schema: new OasInteger(), * required: true * }) * .addProperty({ * name: 'email', * schema: new OasString({ format: 'email' }), * required: true * }) * .addProperty({ * name: 'age', * schema: new OasInteger({ minimum: 0 }), * required: false * }); * ``` */ addProperty({ name, schema, required }: AddPropertyArgs): 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. * * @param 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 * * @example * ```typescript * const userSchema = new OasObject({ * properties: { * id: new OasString(), * name: new OasString(), * email: new OasString(), * temporaryField: new OasString() * }, * required: ['id', 'name', 'temporaryField'] * }); * * // Remove temporary field * const cleanedSchema = userSchema.removeProperty('temporaryField'); * * console.log(cleanedSchema.required); // ['id', 'name'] * console.log('temporaryField' in cleanedSchema.properties); // false * ``` */ removeProperty(name: string): OasObject; isRef(): this is OasRef<'schema'>; resolve(): OasObject; resolveOnce(): OasObject; /** * 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. * * @param options - Conversion options for handling references and context * @returns A JSON Schema representation of the object * * @example * ```typescript * const userObject = new OasObject({ * title: 'User', * properties: { * id: new OasString(), * name: new OasString() * }, * required: ['id'], * additionalProperties: false * }); * * const jsonSchema = userObject.toJsonSchema({ refContext: new Map() }); * * console.log(jsonSchema); * // { * // type: 'object', * // title: 'User', * // properties: { * // id: { type: 'string' }, * // name: { type: 'string' } * // }, * // required: ['id'], * // additionalProperties: false * // } * ``` */ toJsonSchema(options: ToJsonSchemaOptions): OpenAPIV3.NonArraySchemaObject; } //# sourceMappingURL=Object.d.ts.map