Module: Lapsoss::Validators

Extended by:
ActiveSupport::Concern
Included in:
Adapters::Base, Configuration
Defined in:
lib/lapsoss/validators.rb

Class Method Summary collapse

Class Method Details

.loggerObject



11
12
13
# File 'lib/lapsoss/validators.rb', line 11

def logger
  Lapsoss.configuration.logger
end

.validate_api_key!(value, name, format: nil) ⇒ Object

API key validation using AS blank?



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/lapsoss/validators.rb', line 76

def validate_api_key!(value, name, format: nil)
  return false if value.blank? && logger.warn("#{name} is missing")

  # Optional format hints
  case format
  when :uuid
    logger.info "#{name} doesn't look like a UUID, but continuing anyway" unless value.match?(/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/i)
  when :alphanumeric
    logger.info "#{name} contains special characters, but continuing anyway" unless value.match?(/\A[a-z0-9]+\z/i)
  end

  true
end

.validate_callable!(value, name) ⇒ Object

Check if callable



23
24
25
26
27
# File 'lib/lapsoss/validators.rb', line 23

def validate_callable!(value, name)
  return true if value.nil? || value.respond_to?(:call)
  logger.warn "#{name} should be callable but got #{value.class}"
  false
end

.validate_dsn!(dsn_string, name = "DSN") ⇒ Object

DSN validation - just log issues



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/lapsoss/validators.rb', line 30

def validate_dsn!(dsn_string, name = "DSN")
  return true if dsn_string.blank?

  uri = URI.parse(dsn_string)
  logger.warn "#{name} appears to be missing public key" if uri.user.blank?
  logger.warn "#{name} appears to be missing host" if uri.host.blank?
  true
rescue URI::InvalidURIError => e
  logger.error "#{name} couldn't be parsed: #{e.message}"
  false
end

.validate_environment!(value, name = "environment") ⇒ Object

Environment validation using AS presence



65
66
67
68
69
70
71
72
73
# File 'lib/lapsoss/validators.rb', line 65

def validate_environment!(value, name = "environment")
  return true if value.blank?

  value_str = value.to_s.strip
  return true if value_str.present?

  logger.warn "#{name} should not be empty"
  false
end

.validate_presence!(value, name) ⇒ Object

Simple presence check using AS blank?



16
17
18
19
20
# File 'lib/lapsoss/validators.rb', line 16

def validate_presence!(value, name)
  return true if value.present?
  logger.warn "#{name} is missing or blank"
  false
end

.validate_retries!(value, name) ⇒ Object



57
58
59
60
61
62
# File 'lib/lapsoss/validators.rb', line 57

def validate_retries!(value, name)
  return true if value.nil?
  return true if value >= 0
  logger.warn "#{name} should be non-negative, got #{value}"
  false
end

.validate_sample_rate!(value, name) ⇒ Object

Validate numeric ranges using AS Range#cover?



43
44
45
46
47
48
# File 'lib/lapsoss/validators.rb', line 43

def validate_sample_rate!(value, name)
  return true if value.nil?
  return true if (0..1).cover?(value)
  logger.warn "#{name} should be between 0 and 1, got #{value}"
  false
end

.validate_timeout!(value, name) ⇒ Object



50
51
52
53
54
55
# File 'lib/lapsoss/validators.rb', line 50

def validate_timeout!(value, name)
  return true if value.nil?
  return true if value.positive?
  logger.warn "#{name} should be positive, got #{value}"
  false
end

.validate_url!(value, name) ⇒ Object

URL validation



91
92
93
94
95
96
97
98
# File 'lib/lapsoss/validators.rb', line 91

def validate_url!(value, name)
  return true if value.nil?
  URI.parse(value)
  true
rescue URI::InvalidURIError => e
  logger.warn "#{name} couldn't be parsed as URL: #{e.message}"
  false
end