lib/csvlint/validate.rb in csvlint-0.2.6 vs lib/csvlint/validate.rb in csvlint-0.3.0

- old
+ new

@@ -66,10 +66,11 @@ @schema = schema @dialect = dialect @csv_header = true @headers = {} @lambda = options[:lambda] + @validate = options[:validate].nil? ? true : options[:validate] @leading = "" @limit_lines = options[:limit_lines] @extension = parse_extension(source) unless @source.nil? @@ -149,10 +150,11 @@ # If it's not a full line, then prepare to add it to the beginning of the next chunk @leading = line end rescue ArgumentError => ae build_errors(:invalid_encoding, :structure, @current_line, nil, @current_line) unless @reported_invalid_encoding + @current_line = @current_line+1 @reported_invalid_encoding = true end def validate_line(input = nil, index = nil) @input = input @@ -179,11 +181,10 @@ row = LineCSV.parse_line(stream, @csv_options) rescue LineCSV::MalformedCSVError => e build_exception_messages(e, stream, current_line) end - @data << row if row if current_line <= 1 && @csv_header # this conditional should be refactored somewhere row = row.reject { |col| col.nil? || col.empty? } validate_header(row) @@ -193,30 +194,31 @@ @col_counts << row.reject { |col| col.nil? || col.empty? }.size @expected_columns = row.size unless @expected_columns != 0 build_errors(:blank_rows, :structure, current_line, nil, stream.to_s) if row.reject { |c| c.nil? || c.empty? }.size == 0 # Builds errors and warnings related to the provided schema file if @schema - @schema.validate_row(row, current_line, all_errors, @source) + @schema.validate_row(row, current_line, all_errors, @source, @validate) @errors += @schema.errors all_errors += @schema.errors @warnings += @schema.warnings else build_errors(:ragged_rows, :structure, current_line, nil, stream.to_s) if !row.empty? && row.size != @expected_columns end end end + @data << row end def finish sum = @col_counts.inject(:+) unless sum.nil? build_warnings(:title_row, :structure) if @col_counts.first < (sum / @col_counts.size.to_f) end # return expected_columns to calling class build_warnings(:check_options, :structure) if @expected_columns == 1 check_consistency - check_foreign_keys + check_foreign_keys if @validate check_mixed_linebreaks validate_encoding end def validate_metadata @@ -248,11 +250,11 @@ begin url = URI.join(@source_url, uri) schema = Schema.load_from_json(url) if schema.instance_of? Csvlint::Csvw::TableGroup if schema.tables[@source_url] - link_schema = schema + @schema = schema else warn_if_unsuccessful = true build_warnings(:schema_mismatch, :context, nil, nil, @source_url, schema) end end @@ -291,10 +293,11 @@ rescue schema_dialect = {} end @dialect = { "header" => true, + "headerRowCount" => 1, "delimiter" => ",", "skipInitialSpace" => true, "lineTerminator" => :auto, "quoteChar" => '"', "trim" => :true @@ -356,11 +359,11 @@ else names << name end end if @schema - @schema.validate_header(header, @source) + @schema.validate_header(header, @source, @validate) @errors += @schema.errors @warnings += @schema.warnings end return valid? end @@ -439,19 +442,15 @@ return else @schema = nil end end - link_schema = nil - @schema = link_schema if link_schema - paths = [] if @source_url =~ /^http(s)?/ begin well_known_uri = URI.join(@source_url, "/.well-known/csvm") - well_known = open(well_known_uri).read - # TODO + paths = open(well_known_uri).read.split("\n") rescue OpenURI::HTTPError, URI::BadURIError end end paths = ["{+url}-metadata.json", "csv-metadata.json"] if paths.empty? paths.each do |template| @@ -462,20 +461,18 @@ url = File.new(url.to_s.sub(/^file:/, "")) if url.to_s =~ /^file:/ schema = Schema.load_from_json(url) if schema.instance_of? Csvlint::Csvw::TableGroup if schema.tables[@source_url] @schema = schema + return else warn_if_unsuccessful = true build_warnings(:schema_mismatch, :context, nil, nil, @source_url, schema) end end rescue Errno::ENOENT rescue OpenURI::HTTPError, URI::BadURIError, ArgumentError rescue => e - STDERR.puts e.class - STDERR.puts e.message - STDERR.puts e.backtrace raise e end end build_warnings(:schema_mismatch, :context, nil, nil, @source_url, schema) if warn_if_unsuccessful @schema = nil