Module: CommandPost::SchemaValidation
- Included in:
- Persistence
- Defined in:
- lib/command_post/persistence/schema_validation.rb
Class Method Summary collapse
- .validate_allowed_values(field_name, field_info) ⇒ Object
- .validate_auto_load(field_name, field_info) ⇒ Object
- .validate_field_name(field_name) ⇒ Object
- .validate_keywords(field_name, field_info) ⇒ Object
- .validate_location(field_name, field_info) ⇒ Object
- .validate_lookup(field_name, field_info) ⇒ Object
- .validate_required(field_name, field_info) ⇒ Object
- .validate_schema(fields) ⇒ Object
- .validate_type(field_name, field_info) ⇒ Object
Class Method Details
.validate_allowed_values(field_name, field_info) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/command_post/persistence/schema_validation.rb', line 49 def self.validate_allowed_values field_name, field_info if field_info[:allowed_values].class != Array return ["Field Name '#{field_name}' : :allowed_values was specified but the allowed_values must be contained in an Array." ] end types = Hash.new field_info.each{|x| types[x.type] = 0 } if types.keys.count != 1 return ["Field Name '#{field_name}' : :allowed_values the values were not all of the same type - or no values were specified. " ] end [] end |
.validate_auto_load(field_name, field_info) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/command_post/persistence/schema_validation.rb', line 63 def self.validate_auto_load field_name, field_info if [true,false].include?(field_info[:auto_load])==false return ["Field Name '#{field_name}' : :auto_load was specified with an invalid value. Must be 'true' or 'false'." ] end if (field_info[:type] == Array) if !field_info[:of].kind_of?(CommandPost::Persistence) == false return ["Field Name '#{field_name}' : When :auto_load is true and :type is Array, then :of must be a kind_of CommandPost::Persistence." ] end else if field_info[:type].superclass.to_s != 'CommandPost::Persistence' return ["Field Name '#{field_name}' : When :auto_load is true then :type must be kind_of CommandPost::Persistence." ] end end [] end |
.validate_field_name(field_name) ⇒ Object
130 131 132 133 134 135 |
# File 'lib/command_post/persistence/schema_validation.rb', line 130 def self.validate_field_name field_name if field_name.class != Symbol return ["Field Name '#{field_name}' : :field_name must be a Symbol." ] end [] end |
.validate_keywords(field_name, field_info) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/command_post/persistence/schema_validation.rb', line 21 def self.validate_keywords field_name, field_info errors = Array.new if field_name == :lookup errors = self.validate_lookup field_name, field_info else keywords = [:required, :type, :of, :location, :auto_load, :allowed_values, :upcase ] field_info.keys.each do |key| if keywords.include?(key)==false errors << "Field Name: #{field_name} : #{key} is an invalid keyword." end end end errors end |
.validate_location(field_name, field_info) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/command_post/persistence/schema_validation.rb', line 80 def self.validate_location field_name, field_info if [:local,:remote].include?(field_info[:location]) == false return ["Field Name '#{field_name}' : :location was specified with an invalid value. Must be ':remote' or ':local'." ] end if field_info[:location] == :remote if (field_info[:type] == Array) if !field_info[:of] return ["Field Name '#{field_name}' : :type is Array, but :of is not present. Use ':of => <type>' to decare the type contained by Array." ] end if field_info[:of].included_modules.include?(CommandPost::Identity) == false return ["Field Name '#{field_name}' : When :location is :remote and :type is Array, then :of must be a type that includes module CommandPost::Identity." ] end else if field_info[:type].included_modules.include?(CommandPost::Identity) == false return ["Field Name '#{field_name}' : When :location is :remote then :type must be a type that includes module CommandPost::Identity." ] end end else # location is :local if (field_info[:type] == Array) if field_info[:of].included_modules.include?(CommandPost::Identity) return ["Field Name '#{field_name}' : When :location is :local and :type is Array, then :of cannot be an instance of CommandPost::Identity." ] end else if field_info[:type].included_modules.include?(CommandPost::Identity) return ["Field Name '#{field_name}' : When :location is :local then :type cannot be an instance of CommandPost::Identity." ] end end end [] end |
.validate_lookup(field_name, field_info) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/command_post/persistence/schema_validation.rb', line 37 def self.validate_lookup field_name, field_info errors = Array.new keywords = [:use ] field_info.keys.each do |key| if keywords.include?(key)==false errors << "Lookup Field has invalid keyword '#{key}'." end end errors end |
.validate_required(field_name, field_info) ⇒ Object
122 123 124 125 126 127 |
# File 'lib/command_post/persistence/schema_validation.rb', line 122 def self.validate_required field_name, field_info if [true,false].include?(field_info[:required]) == false return ["Field Name '#{field_name}' : :required was specified with an invalid value. Must be 'true' or 'false'." ] end [] end |
.validate_schema(fields) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/command_post/persistence/schema_validation.rb', line 6 def self.validate_schema fields errors = Array.new fields.keys.each do |field_name| errors += self.validate_field_name field_name errors += self.validate_keywords field_name, fields[field_name] errors += self.validate_required field_name, fields[field_name] if fields[field_name].keys.include? :required errors += self.validate_type field_name, fields[field_name] if fields[field_name].keys.include? :type errors += self.validate_location field_name, fields[field_name] if fields[field_name].keys.include? :location errors += self.validate_auto_load field_name, fields[field_name] if fields[field_name].keys.include? :auto_load errors += self.validate_allowed_values field_name, fields[field_name] if fields[field_name].keys.include? :allowed_values end errors end |
.validate_type(field_name, field_info) ⇒ Object
114 115 116 117 118 119 |
# File 'lib/command_post/persistence/schema_validation.rb', line 114 def self.validate_type field_name, field_info if (field_info[:type] == Array) && (field_info.keys.include?(:of) == false) return ["Field Name '#{field_name}' : :type is Array, but :of is not present. Use ':of => <type>' to decare the type contained by Array." ] end [] end |