Class: Skeletor::Skeletons::Validator
- Inherits:
-
Object
- Object
- Skeletor::Skeletons::Validator
- Defined in:
- lib/skeletor/skeletons/validator.rb
Constant Summary collapse
- SCHEMA_FILE =
File.join Skeletons::Loader::TEMPLATE_PATH,'template-schema.yml'
Instance Method Summary collapse
- #check_type(node, expected_type, label, accept_nil = false) ⇒ Object
-
#initialize(template, schema = SCHEMA_FILE) ⇒ Validator
constructor
A new instance of Validator.
- #match_node(node, expected, label) ⇒ Object
- #validate ⇒ Object
Constructor Details
#initialize(template, schema = SCHEMA_FILE) ⇒ Validator
Returns a new instance of Validator.
9 10 11 12 13 14 |
# File 'lib/skeletor/skeletons/validator.rb', line 9 def initialize(template,schema=SCHEMA_FILE) @errors = [] @template = template @schema = Loader.loadTemplate(schema) @types = @schema['types'] || {} end |
Instance Method Details
#check_type(node, expected_type, label, accept_nil = false) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/skeletor/skeletons/validator.rb', line 152 def check_type(node,expected_type,label,accept_nil = false) valid_type = true; if(@types.has_key?(expected_type)) valid_type = match_node(node,@types[expected_type],label) elsif node.class.to_s != expected_type && !(node.kind_of?(NilClass) && (expected_type=='empty' || accept_nil)) valid_type = false end return valid_type end |
#match_node(node, expected, label) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/skeletor/skeletons/validator.rb', line 52 def match_node(node,expected,label) #check type if !check_type(node,expected['type'],label,expected['ok_empty']) return false end if (node.kind_of?(Hash) || node.kind_of?(Array)) if node.empty? && !expected['ok_empty'] @errors.push('Error: node ' + label + ' cannot be empty') return false elsif !node.empty? && expected.has_key?('accepts') valid_content = false if node.kind_of?(Hash) matched = [] unmatched = [] node.each_pair{ |key,value| expected['accepts'].each{ |accepts| result = check_type(value,accepts,key) if result matched.push(key) if !unmatched.find_index(key).nil? unmatched.slice(unmatched.find_index(key)) end break else unmatched.push(key) end } } if(matched.count==node.count) valid_content = true else unmatched.each{ |node| @errors.push('Error: node ' + node + ' is not of an accepted type. Should be one of ' + expected['accepts'].join(', ')) } end elsif node.kind_of?(Array) matched = [] unmatched = [] node.each_index{ |n| expected['accepts'].each{ |accepts| key = label + '[' + n.to_s + ']' result = check_type(node[n],accepts,key) if result matched.push(key) if !unmatched.find_index(key).nil? unmatched.slice(unmatched.find_index(key)) end break else unmatched.push(key) end } } if(matched.count==node.count) valid_content = true else unmatched.each{ |node| @errors.push('Error: node ' + node + ' is not of an accepted type. Should be one of ' + expected['accepts'].join(', ')) } end end if !valid_content @errors.push('Error: node ' + label + ' contains an unaccepted type.') return false end end end return true end |
#validate ⇒ Object
16 17 18 19 20 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 |
# File 'lib/skeletor/skeletons/validator.rb', line 16 def validate() failed = [] @schema['sections'].each{ |section| #check required sections are there if (section['required'] && !@template.has_key?(section['name'])) @errors.push('Error: missing required section - ' + section['name']) failed.push(section['name']) elsif @template.has_key?(section['name']) node = @template[section['name']] validated = match_node(node,section,section['name']) if(!validated) failed.push(section['name']) end end } puts 'Result: ' + (failed.empty? ? 'Validated Successfully!' : 'Validation Failed!') if !failed.empty? && !@errors.empty? puts 'Validation Failed with ' + @errors.count.to_s + ' errors'; puts '' @errors.each{ |error| puts error } return false else return true end end |