Docs@skmtc/core

OpenAPI document

CLASS

OasParameter

@skmtc/core 0.63.4

Represents a Parameter Object in the OpenAPI Specification.

The OasParameter class encapsulates the definition of a single operation parameter, including its location, data type, validation rules, and serialization behavior. Parameters can be located in the path, query string, headers, or cookies.

This class provides comprehensive support for parameter validation, serialization styles, and complex data types through schema definitions or content specifications.

Key Features

  • Location Support: Path, query, header, and cookie parameters
  • Schema Validation: Comprehensive data type validation through schemas
  • Serialization Styles: Various parameter serialization formats
  • Content Types: Support for complex parameter content with media types
  • Examples & Documentation: Parameter examples and descriptive documentation
  • Advanced Options: Empty values, reserved characters, and explode behavior

Signature

import { OasParameter } from '@skmtc/core'

class OasParameter extends OasBase

Examples

Basic path parameter
import { OasParameter, OasString } from '@skmtc/core';

const userIdParam = new OasParameter({
  name: 'userId',
  location: 'path',
  description: 'Unique identifier for the user',
  required: true,
  schema: new OasString({
    pattern: '^[0-9a-f]{24}$',
    example: '507f1f77bcf86cd799439011'
  }),
  style: 'simple',
  explode: false
});

// Used in path: /users/{userId}
Query parameter with validation
const pageSizeParam = new OasParameter({
  name: 'pageSize',
  location: 'query',
  description: 'Number of items to return per page',
  required: false,
  schema: new OasInteger({
    minimum: 1,
    maximum: 100,
    default: 20
  }),
  style: 'form',
  explode: true,
  examples: {
    small: new OasExample({ value: 10 }),
    medium: new OasExample({ value: 25 }),
    large: new OasExample({ value: 50 })
  }
});

// Used in query: ?pageSize=20
Header parameter
const authHeaderParam = new OasParameter({
  name: 'Authorization',
  location: 'header',
  description: 'Bearer token for authentication',
  required: true,
  schema: new OasString({
    pattern: '^Bearer [A-Za-z0-9+/=]+$',
    example: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...'
  }),
  style: 'simple',
  explode: false
});
Complex parameter with content
const filterParam = new OasParameter({
  name: 'filter',
  location: 'query',
  description: 'Complex filter object',
  required: false,
  content: {
    'application/json': new OasMediaType({
      schema: new OasObject({
        properties: {
          category: new OasString(),
          priceRange: new OasObject({
            properties: {
              min: new OasNumber(),
              max: new OasNumber()
            }
          })
        }
      })
    })
  },
  style: 'deepObject',
  explode: true
});

// Used as: ?filter[category]=electronics&filter[priceRange][min]=100
Array parameter with explode
const tagsParam = new OasParameter({
  name: 'tags',
  location: 'query',
  description: 'Filter by multiple tags',
  required: false,
  schema: new OasArray({
    items: new OasString(),
    minItems: 1,
    maxItems: 10
  }),
  style: 'form',
  explode: true // Results in: ?tags=tech&tags=api&tags=rest
});

// Without explode: ?tags=tech,api,rest
// With explode: ?tags=tech&tags=api&tags=rest
Cookie parameter
const sessionParam = new OasParameter({
  name: 'sessionId',
  location: 'cookie',
  description: 'Session identifier cookie',
  required: false,
  schema: new OasString({
    minLength: 32,
    maxLength: 128
  }),
  style: 'form',
  explode: false
});

Constructor

constructor

constructor(fields: ParameterFields, context?: ParseContextType)

Creates a new OasParameter instance.

fields

Parameter configuration fields including name, location, schema, and serialization options

Properties

oasType, 'parameter'

Type identifier for OAS parameter objects

name, string

The name of the parameter

Where the parameter is located (path, query, header, cookie)

description, string | undefined

A brief description of the parameter's purpose and usage

required, boolean | undefined

Determines whether this parameter is mandatory for the operation

deprecated, boolean | undefined

Indicates that the parameter is deprecated and should be avoided

allowEmptyValue, boolean | undefined

Whether to allow empty values for this parameter

allowReserved, boolean | undefined

Whether reserved characters are allowed in parameter values

schema, OasSchema | OasRef<'schema'> | undefined

The schema defining the parameter's data type and validation rules

examples, Record<string, OasExample | OasRef<'example'>> | undefined

Example values demonstrating parameter usage

content, Record<string, OasMediaType> | undefined

Media type definitions for complex parameter content

style, OasParameterStyle | undefined

The serialization style for the parameter (form, simple, etc.)

explode, boolean | undefined

Whether to explode parameter values into separate key-value pairs

extensionFields, Record<string, unknown> | undefined

Custom extension fields (x-* properties) defined for this parameter

Methods

isRef

isRef(): this is OasRef<'parameter'>

Determines if this parameter is a reference object.

Returns

Always false since this is a concrete parameter instance, not a reference

isReadOnly

isReadOnly(): boolean

Always false: readOnly is a schema keyword.

isWriteOnly

isWriteOnly(): boolean

Always false: writeOnly is a schema keyword.

resolve

resolve(): OasParameter

Resolves this parameter object.

Returns

The parameter instance itself since it's already a concrete object

resolveOnce

resolveOnce(): OasParameter

Resolves this parameter object one level.

Returns

The parameter instance itself since it's already a concrete object

toSchema

toSchema(mediaType?: string): OasSchema | OasRef<'schema'>

Extracts the schema for this parameter.

Returns the direct schema if available, or extracts schema from content for the specified media type. When content has exactly one entry, its schema is used whatever the media type.

mediaType

Media type to extract schema from when using content definitions, an exact lookup. Omitted: the JSON media type (application/json, else the first JSON-like type, else *\/*).

Returns

The parameter's schema object

Throws
Error

Error if the parameter has neither schema nor content, or no schema is found for the specified media type

toJsonSchema

toJsonSchema(options: ToJsonSchemaOptions): OpenAPIV3.ParameterObject

Converts this OAS parameter to an OpenAPI v3 JSON schema representation.

options

Conversion options including reference handling and formatting preferences

Returns

OpenAPI v3 parameter object with all properties and validation constraints