Module: Wavefront::Validators

Included in:
Alerting, BatchWriter, Metadata
Defined in:
lib/wavefront/validators.rb

Overview

A module of mixins to validate input. The Wavefront documentation lays down restrictions on types and sizes of various inputs, which we will check on the user’s behalf. Most of the information used in this file comes from community.wavefront.com/docs/DOC-1031 some comes from the Swagger API documentation.

Instance Method Summary collapse

Instance Method Details

#valid_path?(path) ⇒ Boolean



30
31
32
33
34
35
# File 'lib/wavefront/validators.rb', line 30

def valid_path?(path)
  fail Wavefront::Exception::InvalidMetricName unless \
    path.is_a?(String) && path.match(/^[a-z0-9\-_\.]+$/) &&
    path.length < 1024
  true
end

#valid_source?(source) ⇒ Boolean



10
11
12
13
14
15
16
17
18
19
# File 'lib/wavefront/validators.rb', line 10

def valid_source?(source)
  #
  # Check a source, according to
  #
  unless source.is_a?(String) && source.match(/^[a-z0-9\-_\.]+$/) &&
         source.length < 1024
    fail Wavefront::Exception::InvalidSource
  end
  true
end

#valid_string?(string) ⇒ Boolean



21
22
23
24
25
26
27
28
# File 'lib/wavefront/validators.rb', line 21

def valid_string?(string)
  #
  # Only allows PCRE "word" characters, spaces, full-stops and
  # commas in tags and descriptions. This might be too restrictive,
  # but if it is, this is the only place we need to change it.
  #
  string.match(/^[\-\w \.,]*$/)
end

#valid_tags?(tags) ⇒ Boolean



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/wavefront/validators.rb', line 49

def valid_tags?(tags)
  #
  # Operates on a hash of key-value point tags. These are
  # different from source tags.
  #
  tags.each do |k, v|
    fail Wavefront::Exception::InvalidTag unless (k.length +
         v.length < 254) && k.match(/^[a-z0-9\-_\.]+$/)
  end
  true
end

#valid_ts?(ts) ⇒ Boolean



42
43
44
45
46
47
# File 'lib/wavefront/validators.rb', line 42

def valid_ts?(ts)
  unless ts.is_a?(Time) || ts.is_a?(Date)
    fail Wavefront::Exception::InvalidTimestamp
  end
  true
end

#valid_value?(value) ⇒ Boolean



37
38
39
40
# File 'lib/wavefront/validators.rb', line 37

def valid_value?(value)
  fail Wavefront::Exception::InvalidMetricValue unless value.is_a?(Numeric)
  true
end