Class: Grayskull::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/grayskull/validator.rb

Overview

The Validator class contains the core methods of the Grayskull library. It loads the passed files, converts them to Ruby types and validates them.

Instance Method Summary collapse

Constructor Details

#initialize(file, schema) ⇒ Validator

Creates a new Validator instance



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/grayskull/validator.rb', line 8

def initialize(file,schema)
  
  @file = file
  @schema = schema
  
  if file.kind_of?(String) && !Formats::FILENAME_PATTERN.match(file).nil?
    @loaded_file = DataFile.load(file)
  else
    @loaded_file = file
  end
  
  if schema.kind_of?(String) && !Formats::FILENAME_PATTERN.match(schema).nil?
    @loaded_schema = DataFile.load(schema)
  else
    @loaded_schema = schema
  end
        
  @types = @loaded_schema['types'] || {}
  
  @errors = []
end

Instance Method Details

#check_type(node, expected_type, label, accept_nil = false) ⇒ Object

Checks that the node is of the correct type

If the expected node is a custom node type as defined in the schema It will run ‘match_node` to check that the node schema matches the custom type.



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/grayskull/validator.rb', line 170

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

Checks file node matches the schema.

Checks type of node against expected type and checks any children are of the accepted types.



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
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/grayskull/validator.rb', line 65

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

#validateObject

Validates the file against the schema



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
59
# File 'lib/grayskull/validator.rb', line 31

def validate()
 failed = []
  
  @loaded_schema['sections'].each{
    |section|
    
    #check required sections are there
    if (section['required'] && !@loaded_file.has_key?(section['name']))
      @errors.push('Error: missing required section - ' + section['name'])
      failed.push(section['name'])
    elsif @loaded_file.has_key?(section['name'])
      node = @loaded_file[section['name']]
      validated = match_node(node,section,section['name'])
      if(!validated)
        failed.push(section['name'])
      end
    end
         
  }
  
  result = {}       
  result["result"] = failed.empty?
  
  if !failed.empty? && !@errors.empty?
    result["errors"] = @errors
  end
  
  return result
end