Module: Raml::Validation

Included in:
AbstractMethod, AbstractResource, Body, Response, Root
Defined in:
lib/raml/mixin/validation.rb

Instance Method Summary collapse

Instance Method Details

#classes_to_s(classes) ⇒ Object


45
46
47
# File 'lib/raml/mixin/validation.rb', line 45

def classes_to_s(classes)
  classes.join(', ').gsub(/, (\w)\z/, ' or \1')
end

#validate_array(name, array, element_classes = nil) ⇒ Object

Raises:


14
15
16
17
18
19
20
21
22
23
# File 'lib/raml/mixin/validation.rb', line 14

def validate_array(name, array, element_classes=nil)
    raise InvalidProperty, "#{camel_case name} property must be an array" unless
      array.is_a? Array

    if element_classes
      element_classes = [ element_classes ] unless element_classes.is_a? Array
     raise InvalidProperty, "#{camel_case name} property must be an array of #{classes_to_s element_classes}" unless
       array.all? { |element| element_classes.include? element.class }
    end
end

#validate_hash(name, hash, key_class = nil, value_class = nil) ⇒ Object

Raises:


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/raml/mixin/validation.rb', line 25

def validate_hash(name, hash, key_class=nil, value_class=nil)
    raise InvalidProperty, "#{camel_case name} property must be a map" unless 
      hash.is_a? Hash

    if key_class
    if key_class.is_a? Array
      raise InvalidProperty, "#{camel_case name} property must be a map with #{key_class} keys" unless
        hash.keys.all?  {|key| key_class.any? { |kc| key.is_a? kc } }
    else
      raise InvalidProperty, "#{camel_case name} property must be a map with #{key_class} keys" unless
        hash.keys.all?  {|key| key.is_a? key_class }
      end
    end

    if value_class
     raise InvalidProperty, "#{camel_case name} property must be a map with map values: #{hash}" unless
      hash.values.all?  {|value| value.is_a? Hash }
    end
end

#validate_property(name, value, classes) ⇒ Object

Raises:


4
5
6
7
# File 'lib/raml/mixin/validation.rb', line 4

def validate_property(name, value, classes)
  classes = [ classes ] unless classes.is_a? Array
  raise InvalidProperty, "#{camel_case name} property must be an #{classes_to_s classes}" unless classes.include? value.class
end

#validate_string(name, string) ⇒ Object

Raises:


9
10
11
12
# File 'lib/raml/mixin/validation.rb', line 9

def validate_string(name, string)
  validate_property name, string, String
    raise InvalidProperty, "#{camel_case name} property must be a non-empty string." if     string.empty?
end