Class: Verquest::Configuration

Inherits:
Object
  • Object
show all
Includes:
Base::HelperClassMethods
Defined in:
lib/verquest/configuration.rb

Overview

Configuration for the Verquest gem

This class manages configuration settings for the Verquest gem, including validation behavior, JSON Schema version, and version resolution strategy. It’s used to customize the behavior of versioned API requests.

Examples:

Basic configuration

Verquest.configure do |config|
  config.validate_params = true
  config.current_version = -> { Current.api_version }
end

Constant Summary collapse

SCHEMAS =

Mapping of supported JSON Schema versions to their implementation classes

This constant maps the symbolic names of JSON Schema versions to their corresponding JSONSchemer implementation classes. These are used for schema validation and generation based on the configured schema version.

Examples:

Accessing a schema implementation

schema_class = Verquest::Configuration::SCHEMAS[:draft2020_12]

Returns:

  • (Hash<Symbol, Class>)

    A frozen hash mapping schema version names to implementation classes

{
  draft4: JSONSchemer::Draft4,
  draft6: JSONSchemer::Draft6,
  draft7: JSONSchemer::Draft7,
  draft2019_09: JSONSchemer::Draft201909,
  draft2020_12: JSONSchemer::Draft202012,
  openapi30: JSONSchemer::OpenAPI30,
  openapi31: JSONSchemer::OpenAPI31
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Base::HelperClassMethods

#camelize, #snake_to_camel

Constructor Details

#initializeConfiguration

Initialize a new Configuration with default values



80
81
82
83
84
85
86
87
88
89
# File 'lib/verquest/configuration.rb', line 80

def initialize
  @validate_params = true
  @json_schema_version = :draft2020_12
  @validation_error_handling = :raise
  @remove_extra_root_keys = true
  @version_resolver = VersionResolver
  @insert_property_defaults = true
  @custom_field_types = {}
  @default_additional_properties = false
end

Instance Attribute Details

#current_version#call

A callable object that returns the current API version to use when not explicitly specified

Returns:

  • (#call)

    An object responding to call that determines the current version



75
76
77
# File 'lib/verquest/configuration.rb', line 75

def current_version
  @current_version
end

#custom_field_typesObject

Returns the value of attribute custom_field_types.



75
# File 'lib/verquest/configuration.rb', line 75

attr_reader :current_version, :version_resolver, :custom_field_types

#default_additional_propertiesBoolean

Controls the default behavior for handling properties not defined in the schema

Returns:

  • (Boolean)

    false to disallow additional properties (default), true to allow them



61
62
# File 'lib/verquest/configuration.rb', line 61

attr_accessor :validate_params, :json_schema_version, :validation_error_handling,
:remove_extra_root_keys, :insert_property_defaults, :default_additional_properties

#insert_property_defaultsBoolean

Controls whether default values defined in property schemas should be inserted when not provided during validation

Returns:

  • (Boolean)

    true if default values should be inserted, false otherwise



61
62
# File 'lib/verquest/configuration.rb', line 61

attr_accessor :validate_params, :json_schema_version, :validation_error_handling,
:remove_extra_root_keys, :insert_property_defaults, :default_additional_properties

#json_schema_versionSymbol

The JSON Schema draft version to use for validation and schema generation (see Configuration::SCHEMAS)

Returns:

  • (Symbol)

    The JSON Schema version (e.g., :draft2020_12, :draft2019_09, :draft7)



61
62
# File 'lib/verquest/configuration.rb', line 61

attr_accessor :validate_params, :json_schema_version, :validation_error_handling,
:remove_extra_root_keys, :insert_property_defaults, :default_additional_properties

#remove_extra_root_keysBoolean

Controls if extra root keys not defined in the schema should be removed from the parameters

Returns:

  • (Boolean)

    true if extra keys should be removed, false otherwise



61
62
# File 'lib/verquest/configuration.rb', line 61

attr_accessor :validate_params, :json_schema_version, :validation_error_handling,
:remove_extra_root_keys, :insert_property_defaults, :default_additional_properties

#validate_paramsBoolean

Controls whether parameters are automatically validated against the schema

Returns:

  • (Boolean)

    true if validation is enabled, false otherwise



61
62
63
# File 'lib/verquest/configuration.rb', line 61

def validate_params
  @validate_params
end

#validation_error_handlingSymbol

Controls how errors during parameter processing are handled

Returns:

  • (Symbol)

    :raise to raise errors (default) or :result to return errors in the Result object



61
62
# File 'lib/verquest/configuration.rb', line 61

attr_accessor :validate_params, :json_schema_version, :validation_error_handling,
:remove_extra_root_keys, :insert_property_defaults, :default_additional_properties

#version_resolver#call

The resolver used to map version strings/identifiers to version objects

Returns:

  • (#call)

    An object that responds to call for resolving versions



75
# File 'lib/verquest/configuration.rb', line 75

attr_reader :current_version, :version_resolver, :custom_field_types

Instance Method Details

#json_schemaClass

Gets the JSON Schema class based on the configured version

Returns:

  • (Class)

    The JSON Schema class matching the configured version

Raises:

  • (ArgumentError)

    If the configured json_schema_version is not supported



150
151
152
# File 'lib/verquest/configuration.rb', line 150

def json_schema
  SCHEMAS[json_schema_version] || raise(ArgumentError, "Unsupported JSON Schema version: #{json_schema_version}")
end

#json_schema_uriString

Gets the JSON Schema URI for the configured schema version

Returns:

  • (String)

    The base URI for the configured JSON Schema version



157
158
159
# File 'lib/verquest/configuration.rb', line 157

def json_schema_uri
  json_schema::BASE_URI.to_s
end