Class: Csvlint::Schema

Inherits:
Object
  • Object
show all
Extended by:
Gem::Deprecate
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.



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

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.



5
6
7
# File 'lib/csvlint/schema.rb', line 5

def description
  @description
end

#fieldsObject (readonly)

Returns the value of attribute fields.



5
6
7
# File 'lib/csvlint/schema.rb', line 5

def fields
  @fields
end

#titleObject (readonly)

Returns the value of attribute title.



5
6
7
# File 'lib/csvlint/schema.rb', line 5

def title
  @title
end

#uriObject (readonly)

Returns the value of attribute uri.



5
6
7
# File 'lib/csvlint/schema.rb', line 5

def uri
  @uri
end

Class Method Details

.from_csvw_metadata(uri, json) ⇒ Object



27
28
29
# File 'lib/csvlint/schema.rb', line 27

def (uri, json)
  Csvlint::Csvw::TableGroup.from_json(uri, json)
end

.from_json_table(uri, json) ⇒ Object



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

def 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
  Schema.new(uri, fields, json["title"], json["description"])
end

.load_from_json(uri, output_errors = true) ⇒ Object

Deprecated method signature



32
33
34
# File 'lib/csvlint/schema.rb', line 32

def load_from_json(uri, output_errors = true)
  load_from_uri(uri, output_errors)
end

.load_from_string(uri, string, output_errors = true) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/csvlint/schema.rb', line 43

def load_from_string(uri, string, output_errors = true)
  json = JSON.parse(string)
  if json["@context"]
    uri = "file:#{File.expand_path(uri)}" unless /^http(s)?/.match?(uri.to_s)
    Schema.(uri, json)
  else
    Schema.from_json_table(uri, json)
  end
rescue TypeError => e
  # NO IDEA what this was even trying to do - SP 20160526
rescue Csvlint::Csvw::MetadataError => e
  raise e
rescue => e
  if output_errors === true
    warn e.class
    warn e.message
    warn e.backtrace
  end
  Schema.new(nil, [], "malformed", "malformed")
end

.load_from_uri(uri, output_errors = true) ⇒ Object



37
38
39
40
41
# File 'lib/csvlint/schema.rb', line 37

def load_from_uri(uri, output_errors = true)
  load_from_string(uri, URI.open(uri).read, output_errors)
rescue OpenURI::HTTPError, Errno::ENOENT => e
  raise e
end

Instance Method Details

#validate_header(header, source_url = nil, validate = true) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/csvlint/schema.rb', line 65

def validate_header(header, source_url = nil, validate = true)
  reset

  found_header = header.to_csv(row_sep: "")
  expected_header = @fields.map { |f| f.name }.to_csv(row_sep: "")
  if found_header != expected_header
    build_warnings(:malformed_header, :schema, 1, nil, found_header, "expectedHeader" => expected_header)
  end
  valid?
end

#validate_row(values, row = nil, all_errors = [], source_url = nil, validate = true) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/csvlint/schema.rb', line 76

def validate_row(values, row = nil, all_errors = [], source_url = nil, validate = true)
  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, all_errors)
    @errors += fields[i].errors
    @warnings += fields[i].warnings
  end

  valid?
end