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



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

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



70
71
72
73
# File 'lib/command_post/persistence/data_validation.rb', line 70

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



18
19
20
21
# File 'lib/command_post/persistence/data_validation.rb', line 18

def data_errors

  verify_data
end

#data_type_does_not_match_declaration(field_name, field_info) ⇒ Object



65
66
67
68
# File 'lib/command_post/persistence/data_validation.rb', line 65

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)


8
9
10
11
# File 'lib/command_post/persistence/data_validation.rb', line 8

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



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

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



60
61
62
63
# File 'lib/command_post/persistence/data_validation.rb', line 60

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



80
81
82
83
# File 'lib/command_post/persistence/data_validation.rb', line 80

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)


13
14
15
16
# File 'lib/command_post/persistence/data_validation.rb', line 13

def valid?
  
  verify_data.length == 0 
end

#value_not_among_the_list_of_allowed_values(field_name, field_info) ⇒ Object



75
76
77
78
# File 'lib/command_post/persistence/data_validation.rb', line 75

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



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
57
58
# File 'lib/command_post/persistence/data_validation.rb', line 23

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