Class: Csvlint::Schema

Inherits:
Object
  • Object
show all
Includes:
ErrorCollector
Defined in:
lib/csvlint/schema.rb

Constant Summary

Constants included from ErrorCollector

ErrorCollector::MESSAGE_LEVELS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ErrorCollector

#build_message, #reset, #valid?

Constructor Details

#initialize(uri, fields = [], title = nil, description = nil) ⇒ Schema

Returns a new instance of Schema.



11
12
13
14
15
16
17
# File 'lib/csvlint/schema.rb', line 11

def initialize(uri, fields=[], title=nil, description=nil)
  @uri = uri
  @fields = fields
  @title = title
  @description = description
  reset
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



9
10
11
# File 'lib/csvlint/schema.rb', line 9

def description
  @description
end

#fieldsObject (readonly)

Returns the value of attribute fields.



9
10
11
# File 'lib/csvlint/schema.rb', line 9

def fields
  @fields
end

#titleObject (readonly)

Returns the value of attribute title.



9
10
11
# File 'lib/csvlint/schema.rb', line 9

def title
  @title
end

#uriObject (readonly)

Returns the value of attribute uri.



9
10
11
# File 'lib/csvlint/schema.rb', line 9

def uri
  @uri
end

Class Method Details

.from_json_table(uri, json) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/csvlint/schema.rb', line 50

def Schema.from_json_table(uri, json)
  fields = []
  json["fields"].each do |field_desc|
    fields << Csvlint::Field.new( field_desc["name"] , field_desc["constraints"], 
      field_desc["title"], field_desc["description"] )
  end if json["fields"]
  return Schema.new( uri , fields, json["title"], json["description"] )
end

.load_from_json_table(uri) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/csvlint/schema.rb', line 59

def Schema.load_from_json_table(uri)
  begin
    json = JSON.parse( open(uri).read )
    return Schema.from_json_table(uri,json)
  rescue
    return nil
  end
end

Instance Method Details

#validate_header(header) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/csvlint/schema.rb', line 19

def validate_header(header)
  reset
  header.each_with_index do |name,i|
    build_warnings(:header_name, :schema, nil, i+1, name) if fields[i].name != name
  end
  return valid?
end

#validate_row(values, row = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/csvlint/schema.rb', line 27

def validate_row(values, row=nil)
  reset
  if values.length < fields.length
    fields[values.size..-1].each_with_index do |field, i|
      build_warnings(:missing_column, :schema, row, values.size+i+1)
    end
  end
  if values.length > fields.length
    values[fields.size..-1].each_with_index do |data_column, i|
      build_warnings(:extra_column, :schema, row, fields.size+i+1)
    end
  end
  
  fields.each_with_index do |field,i|
    value = values[i] || ""
    result = field.validate_column(value, row, i+1)
    @errors += fields[i].errors
    @warnings += fields[i].warnings        
  end
        
  return valid?
end