Class: Csvlint::Schema

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

Instance Attribute Summary collapse

Attributes included from ErrorCollector

#errors, #info_messages, #warnings

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ErrorCollector

#build_errors, #build_info_messages, #build_warnings, #reset, #valid?

Constructor Details

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

Returns a new instance of Schema.



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

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.



7
8
9
# File 'lib/csvlint/schema.rb', line 7

def description
  @description
end

#fieldsObject (readonly)

Returns the value of attribute fields.



7
8
9
# File 'lib/csvlint/schema.rb', line 7

def fields
  @fields
end

#titleObject (readonly)

Returns the value of attribute title.



7
8
9
# File 'lib/csvlint/schema.rb', line 7

def title
  @title
end

#uriObject (readonly)

Returns the value of attribute uri.



7
8
9
# File 'lib/csvlint/schema.rb', line 7

def uri
  @uri
end

Class Method Details

.from_json_table(uri, json) ⇒ Object



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

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



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

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



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

def validate_header(header)
  reset

  found_header = header.join(',')
  expected_header = @fields.map{ |f| f.name }.join(',')
  if found_header != expected_header
    build_warnings(:malformed_header, :schema, 1, nil, found_header, expected_header)
  end

  return valid?
end

#validate_row(values, row = nil) ⇒ Object



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

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