Class: Longleaf::ServiceMappingValidator
- Inherits:
-
ConfigurationValidator
- Object
- ConfigurationValidator
- Longleaf::ServiceMappingValidator
- Defined in:
- lib/longleaf/services/service_mapping_validator.rb
Overview
Validates application configuration of service to location mappings
Class Method Summary collapse
-
.validate_config(config) ⇒ Object
Validates service mapping configuration to ensure that it is syntactically and referentially correct.
- .validate_mapping_field(field, mapping, valid_values) ⇒ Object private
Methods inherited from ConfigurationValidator
Class Method Details
.validate_config(config) ⇒ Object
Validates service mapping configuration to ensure that it is syntactically and referentially correct.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/longleaf/services/service_mapping_validator.rb', line 14 def self.validate_config(config) assert("Configuration must be a hash, but a #{config.class} was provided", config.class == Hash) assert("Configuration must contain a root '#{AF::SERVICE_MAPPINGS}' key", config.key?(AF::SERVICE_MAPPINGS)) mappings = config[AF::SERVICE_MAPPINGS] return if mappings.nil? || mappings.empty? assert("'#{AF::SERVICE_MAPPINGS}' must be an array of mappings", mappings.is_a?(Array)) service_names = config[AF::SERVICES].keys location_names = config[AF::LOCATIONS].keys mappings.each do |mapping| assert("Mapping must be a hash, but received #{mapping.inspect} instead", mapping.is_a?(Hash)) validate_mapping_field(AF::LOCATIONS, mapping, location_names) validate_mapping_field(AF::SERVICES, mapping, service_names) end end |
.validate_mapping_field(field, mapping, valid_values) ⇒ Object (private)
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/longleaf/services/service_mapping_validator.rb', line 32 def self.validate_mapping_field(field, mapping, valid_values) assert("Mapping must contain a '#{field}' field", mapping.key?(field)) field_values = mapping[field] assert("Mapping '#{field}' field must be either a string or an array, but received '#{field_values.inspect}' instead", field_values.is_a?(Array) || field_values.is_a?(String)) assert("Mapping must specify one or more value in the '#{field}' field", !field_values.empty?) check_values = field_values.is_a?(String) ? [field_values] : field_values check_values.each do |value| assert("Mapping '#{field}' specifies value '#{value}', but no #{field} with that name exist", valid_values.include?(value)) end end |