Module: DryValidationOpenapi::Convertable

Defined in:
lib/dry_validation_openapi/convertable.rb

Overview

Module to extend dry-validation contracts with OpenAPI schema generation

Examples:

class CreateUserContract < Dry::Validation::Contract
  extend DryValidationOpenapi::Convertable

  params do
    required(:email).value(:string)
    required(:age).value(:integer)
  end
end

# In your rswag spec
parameter name: :body, in: :body, schema: CreateUserContract.open_api_schema

Instance Method Summary collapse

Instance Method Details

#clear_schema_cache!nil

Clears the cached schema, forcing regeneration on next access Useful during development or when contract definition changes

Returns:

  • (nil)


36
37
38
39
# File 'lib/dry_validation_openapi/convertable.rb', line 36

def clear_schema_cache!
  @open_api_schema = nil
  @location = nil
end

#locationString

Returns the file path where this contract class is defined

Returns:

  • (String)

    the absolute path to the contract file



21
22
23
# File 'lib/dry_validation_openapi/convertable.rb', line 21

def location
  @location ||= Object.const_source_location(name)[0]
end

#open_api_schemaHash

Generates an OpenAPI schema from this contract’s params block

Returns:

  • (Hash)

    OpenAPI schema hash with type, properties, and required fields



28
29
30
# File 'lib/dry_validation_openapi/convertable.rb', line 28

def open_api_schema
  @open_api_schema ||= SchemaBuilder.build(location)
end