Module: Securial::Config::Validation

Extended by:
Validation
Included in:
Validation
Defined in:
lib/securial/config/validation.rb

Overview

Configuration validation and verification utilities.

This module provides methods to validate Securial configuration objects against the defined schema, ensuring type safety, required field presence, and business logic constraints are met before the application starts.

Instance Method Summary collapse

Instance Method Details

#raise_error(msg) ⇒ void (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Logs error message and raises configuration error.

Provides a consistent way to handle validation failures by logging the error at fatal level and raising the appropriate exception.

Parameters:

  • msg (String)

    The error message to log and include in the exception

Raises:



154
155
156
157
# File 'lib/securial/config/validation.rb', line 154

def raise_error(msg)
  Securial.logger.fatal msg
  raise Securial::Error::Config::InvalidConfigurationError, msg
end

#validate_all!(securial_config) ⇒ void

This method returns an undefined value.

Validates all configuration settings against the schema.

Performs comprehensive validation of the provided configuration object, including required field checks, type validation, value constraints, and cross-field dependency validation.

Examples:

config = Securial::Configuration.new
config.app_name = "MyApp"
config.session_secret = "my-secret-key"

Validation.validate_all!(config)
# Configuration is valid and ready to use

Parameters:

  • securial_config (Securial::Configuration)

    The configuration object to validate

Raises:



49
50
51
52
53
54
55
# File 'lib/securial/config/validation.rb', line 49

def validate_all!(securial_config)
  signature = Securial::Config::Signature.config_signature

  validate_required_fields!(signature, securial_config)
  validate_types_and_values!(signature, securial_config)
  validate_password_lengths!(securial_config)
end

#validate_password_lengths!(config) ⇒ void (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Validates password length configuration constraints.

Ensures that password minimum length does not exceed maximum length, which would create an impossible constraint for users.

Parameters:

  • config (Securial::Configuration)

    The configuration object to validate

Raises:



138
139
140
141
142
# File 'lib/securial/config/validation.rb', line 138

def validate_password_lengths!(config)
  if config.password_min_length > config.password_max_length
    raise_error("password_min_length cannot be greater than password_max_length.")
  end
end

#validate_required_fields!(signature, config) ⇒ void (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Validates that all required configuration fields are present.

Checks both unconditionally required fields and conditionally required fields based on other configuration values.

Parameters:

  • signature (Hash)

    The configuration schema from Signature

  • config (Securial::Configuration)

    The configuration object to validate

Raises:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/securial/config/validation.rb', line 70

def validate_required_fields!(signature, config)
  signature.each do |key, options|
    value = config.send(key)
    required = options[:required]

    if required == true && value.nil?
      raise_error("#{key} is required but not provided.")
    elsif required.is_a?(String)
      # Handle conditional requirements based on other config values
      dynamic_required = config.send(required)
      signature[key][:required] = dynamic_required
      if dynamic_required && value.nil?
        raise_error("#{key} is required but not provided when #{required} is true.")
      end
    end
  end
end

#validate_types_and_values!(signature, config) ⇒ void (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Validates types and value constraints for configuration fields.

Ensures that configuration values match their expected types and fall within acceptable ranges or allowed value sets.

Parameters:

  • signature (Hash)

    The configuration schema from Signature

  • config (Securial::Configuration)

    The configuration object to validate

Raises:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/securial/config/validation.rb', line 99

def validate_types_and_values!(signature, config)
  signature.each do |key, options|
    next unless signature[key][:required]

    value = config.send(key)
    types = Array(options[:type])

    # Type validation
    unless types.any? { |type| value.is_a?(type) }
      raise_error("#{key} must be of type(s) #{types.join(', ')}, but got #{value.class}.")
    end

    # Duration-specific validation
    if options[:type] == ActiveSupport::Duration && value <= 0
      raise_error("#{key} must be a positive duration, but got #{value}.")
    end

    # Numeric value validation
    if options[:type] == Numeric && value < 0
      raise_error("#{key} must be a non-negative numeric value, but got #{value}.")
    end

    # Allowed values validation
    if options[:allowed_values] && options[:allowed_values].exclude?(value)
      raise_error("#{key} must be one of #{options[:allowed_values].join(', ')}, but got #{value}.")
    end
  end
end