Class: Csvlint::Schema
- Inherits:
-
Object
- Object
- Csvlint::Schema
- Extended by:
- Gem::Deprecate
- Includes:
- ErrorCollector
- Defined in:
- lib/csvlint/schema.rb
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Attributes included from ErrorCollector
#errors, #info_messages, #warnings
Class Method Summary collapse
- .from_csvw_metadata(uri, json) ⇒ Object
- .from_json_table(uri, json) ⇒ Object
-
.load_from_json(uri, output_errors = true) ⇒ Object
Deprecated method signature.
- .load_from_string(uri, string, output_errors = true) ⇒ Object
- .load_from_uri(uri, output_errors = true) ⇒ Object
Instance Method Summary collapse
-
#initialize(uri, fields = [], title = nil, description = nil) ⇒ Schema
constructor
A new instance of Schema.
- #validate_header(header, source_url = nil, validate = true) ⇒ Object
- #validate_row(values, row = nil, all_errors = [], source_url = nil, validate = true) ⇒ Object
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
#description ⇒ Object (readonly)
Returns the value of attribute description.
7 8 9 |
# File 'lib/csvlint/schema.rb', line 7 def description @description end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
7 8 9 |
# File 'lib/csvlint/schema.rb', line 7 def fields @fields end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
7 8 9 |
# File 'lib/csvlint/schema.rb', line 7 def title @title end |
#uri ⇒ Object (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_csvw_metadata(uri, json) ⇒ Object
30 31 32 |
# File 'lib/csvlint/schema.rb', line 30 def (uri, json) return Csvlint::Csvw::TableGroup.from_json(uri, json) end |
.from_json_table(uri, json) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/csvlint/schema.rb', line 21 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 if json["fields"] return Schema.new( uri , fields, json["title"], json["description"] ) end |
.load_from_json(uri, output_errors = true) ⇒ Object
Deprecated method signature
35 36 37 |
# File 'lib/csvlint/schema.rb', line 35 def load_from_json(uri, output_errors = true) load_from_uri(uri, output_errors) end |
.load_from_string(uri, string, output_errors = true) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/csvlint/schema.rb', line 46 def load_from_string(uri, string, output_errors = true) begin json = JSON.parse( string ) if json["@context"] uri = "file:#{File.(uri)}" unless uri.to_s =~ /^http(s)?/ return Schema.(uri,json) else return 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 STDERR.puts e.class STDERR.puts e. STDERR.puts e.backtrace end return Schema.new(nil, [], "malformed", "malformed") end end |
.load_from_uri(uri, output_errors = true) ⇒ Object
40 41 42 43 44 |
# File 'lib/csvlint/schema.rb', line 40 def load_from_uri(uri, output_errors = true) load_from_string(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
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/csvlint/schema.rb', line 72 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 return valid? end |
#validate_row(values, row = nil, all_errors = [], source_url = nil, validate = true) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/csvlint/schema.rb', line 83 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 return valid? end |