Module: CommandPost::DataValidation

Included in:
Persistence
Defined in:
lib/command_post/persistence/data_validation.rb

Instance Method Summary collapse

Instance Method Details

#accepted_values_supplied_but_they_are_not_all_the_same_type(field_name, field_info) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/command_post/persistence/data_validation.rb', line 83

def accepted_values_supplied_but_they_are_not_all_the_same_type(field_name, field_info)
  if (@data[field_name.to_s] != nil) && (@data[field_name].class == Array) && (field_info[:type] == Array) && (field_info[:local] == true)
    if field_info[:location] == :local
      expected_type = field_info[:of]
      @data[field_name.to_s].each do |object_in_array|
        if object_in_array.class != expected_type 
          return true
        end
      end
    end
  end    
end

#allowed_values_declared_but_array_of_values_not_supplied(field_name, field_info) ⇒ Object



68
69
70
71
# File 'lib/command_post/persistence/data_validation.rb', line 68

def allowed_values_declared_but_array_of_values_not_supplied field_name, field_info 

  (@data[field_name] != nil)  &&  (@data[field_name].class !=  Array)  &&  (field_info[:allowed_values])  &&  (field_info[:allowed_values].class != Array) 
end

#data_errorsObject



16
17
18
19
# File 'lib/command_post/persistence/data_validation.rb', line 16

def data_errors

  verify_data
end

#data_type_does_not_match_declaration(field_name, field_info) ⇒ Object



63
64
65
66
# File 'lib/command_post/persistence/data_validation.rb', line 63

def data_type_does_not_match_declaration field_name, field_info

  @data[field_name] != nil  &&  (@data[field_name].class !=  field_info[:type])
end

#empty?Boolean

Returns:

  • (Boolean)


6
7
8
9
# File 'lib/command_post/persistence/data_validation.rb', line 6

def empty?

  @data.nil? || @data == {}
end

#field_is_array_of_remote_objects_but_array_has_values_other_than_persistence_or_identity(field_name, field_info) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/command_post/persistence/data_validation.rb', line 96

def field_is_array_of_remote_objects_but_array_has_values_other_than_persistence_or_identity(field_name, field_info)
  if (@data[field_name.to_s] != nil) && (@data[field_name].class == Array) && (field_info[:type] == Array) && (field_info[:location] == :remote)
    # OK, :of was declared, forge ahead and check any objects in the array for the correct type (these are all local, no worries about aggregate pointer)
    expected_type = field_info[:of]
    @data[field_name.to_s].each do |object_in_array|
      if (object_in_array.class != expected_type) && (object_in_array.class != AggregatePointer) 
        return ["#{self.class}: #{field_name} is an Array and all objects should be of type #{expected_type} but one object was of type #{object_in_array.class} or of AggregatePointer."]
      end
    end
  end 
end

#missing_required_field(field_name, field_info) ⇒ Object



58
59
60
61
# File 'lib/command_post/persistence/data_validation.rb', line 58

def missing_required_field field_name, field_info
  puts "field_name is #{field_name} and has class of #{field_name.class}"
  @data.keys.include?(field_name)==false && field_info[:required] == true
end

#type_is_array_but_keyword___of___not_supplied(field_name, field_info) ⇒ Object



78
79
80
81
# File 'lib/command_post/persistence/data_validation.rb', line 78

def type_is_array_but_keyword___of___not_supplied field_name, field_info

  (@data[field_name.to_s] != nil) && (@data[field_name].class == Array) && (field_info[:type] == Array) && (field_info[:local] == true) && ((!field_info.keys.include?(:of)) || field_info[:of].nil?)
end

#valid?Boolean

Returns:

  • (Boolean)


11
12
13
14
# File 'lib/command_post/persistence/data_validation.rb', line 11

def valid?
  
  verify_data.length == 0 
end

#value_not_among_the_list_of_allowed_values(field_name, field_info) ⇒ Object



73
74
75
76
# File 'lib/command_post/persistence/data_validation.rb', line 73

def value_not_among_the_list_of_allowed_values field_name, field_info

  (@data[field_name] != nil)  &&  (@data[field_name].class !=  Array)  &&  (field_info[:allowed_values])  &&  (field_info[:allowed_values].include?(@data[field_name]) == false  )
end

#verify_dataObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/command_post/persistence/data_validation.rb', line 21

def verify_data  

  errors = Array.new 

  schema_fields.each do |field_name, field_info|
    if missing_required_field(field_name, field_info) 

      errors << "#{self.class}:#{field_name} - is a required field." 
    end
    if data_type_does_not_match_declaration(field_name, field_info) 

      errors << "#{self.class}: #{field_name}: expected type: #{field_info[:type].name}, but received type #{@data[field_name].class.name}."  
    end 
    if allowed_values_declared_but_array_of_values_not_supplied(field_name, field_info) 

      errors << "#{self.class}: #{field_name}: expected type: #{field_info[:type].name}, but received type #{@data[field_name].class.name}."  
    end 
    if value_not_among_the_list_of_allowed_values(field_name, field_info) 

      errors << "#{self.class}: #{field_name}: The value supplied was not in the list of acceptable values."
    end
    if type_is_array_but_keyword___of___not_supplied(field_name, field_info)

      errors << "#{self.class}: #{field_name}: is an Array, but the ':of' keyword was not set to declare the class type for objects in the array."
    end 
    if accepted_values_supplied_but_they_are_not_all_the_same_type(field_name, field_info)
      
      errors << "#{self.class}: #{field_name} is an Array and all objects should be of type #{expected_type} but one object was of type #{object_in_array.class}."
    end
    if field_is_array_of_remote_objects_but_array_has_values_other_than_persistence_or_identity(field_name, field_info)
 
      errors << "#{self.class}: #{field_name} is an Array and all objects should be of type #{expected_type} but one object was of type #{object_in_array.class} or of AggregatePointer." 
    end
  end
  errors 
end