csvlint 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/dependabot.yml +4 -0
- data/.github/workflows/push.yml +14 -2
- data/.ruby-version +1 -1
- data/.standard_todo.yml +43 -0
- data/Dockerfile +16 -0
- data/Gemfile +2 -2
- data/README.md +9 -9
- data/Rakefile +7 -7
- data/csvlint.gemspec +14 -16
- data/docker_notes_for_windows.txt +20 -0
- data/features/step_definitions/cli_steps.rb +11 -11
- data/features/step_definitions/information_steps.rb +4 -4
- data/features/step_definitions/parse_csv_steps.rb +11 -11
- data/features/step_definitions/schema_validation_steps.rb +10 -10
- data/features/step_definitions/sources_steps.rb +1 -1
- data/features/step_definitions/validation_errors_steps.rb +19 -19
- data/features/step_definitions/validation_info_steps.rb +9 -9
- data/features/step_definitions/validation_warnings_steps.rb +11 -11
- data/features/support/aruba.rb +6 -6
- data/features/support/earl_formatter.rb +39 -39
- data/features/support/env.rb +10 -11
- data/features/support/load_tests.rb +107 -103
- data/features/support/webmock.rb +2 -2
- data/lib/csvlint/cli.rb +133 -130
- data/lib/csvlint/csvw/column.rb +279 -280
- data/lib/csvlint/csvw/date_format.rb +90 -92
- data/lib/csvlint/csvw/metadata_error.rb +1 -3
- data/lib/csvlint/csvw/number_format.rb +40 -32
- data/lib/csvlint/csvw/property_checker.rb +714 -717
- data/lib/csvlint/csvw/table.rb +49 -52
- data/lib/csvlint/csvw/table_group.rb +24 -23
- data/lib/csvlint/error_collector.rb +2 -0
- data/lib/csvlint/error_message.rb +0 -1
- data/lib/csvlint/field.rb +153 -141
- data/lib/csvlint/schema.rb +34 -42
- data/lib/csvlint/validate.rb +161 -143
- data/lib/csvlint/version.rb +1 -1
- data/lib/csvlint.rb +22 -23
- data/spec/csvw/column_spec.rb +15 -16
- data/spec/csvw/date_format_spec.rb +5 -7
- data/spec/csvw/number_format_spec.rb +2 -4
- data/spec/csvw/table_group_spec.rb +103 -105
- data/spec/csvw/table_spec.rb +71 -73
- data/spec/field_spec.rb +116 -121
- data/spec/schema_spec.rb +129 -139
- data/spec/spec_helper.rb +6 -6
- data/spec/validator_spec.rb +167 -190
- metadata +22 -55
data/lib/csvlint/cli.rb
CHANGED
@@ -1,15 +1,13 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require 'thor'
|
1
|
+
require "csvlint"
|
2
|
+
require "rainbow"
|
3
|
+
require "active_support/json"
|
4
|
+
require "json"
|
5
|
+
require "thor"
|
7
6
|
|
8
|
-
require
|
7
|
+
require "active_support/inflector"
|
9
8
|
|
10
9
|
module Csvlint
|
11
10
|
class Cli < Thor
|
12
|
-
|
13
11
|
desc "myfile.csv OR csvlint http://example.com/myfile.csv", "Supports validating CSV files to check their syntax and contents"
|
14
12
|
|
15
13
|
option :dump_errors, desc: "Pretty print error and warning objects.", type: :boolean, aliases: :d
|
@@ -36,162 +34,167 @@ module Csvlint
|
|
36
34
|
|
37
35
|
private
|
38
36
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
37
|
+
def read_source(source)
|
38
|
+
if source.nil?
|
39
|
+
# If no source is present, try reading from stdin
|
40
|
+
if !$stdin.tty?
|
41
|
+
source = begin
|
42
|
+
StringIO.new($stdin.read)
|
43
|
+
rescue
|
44
|
+
nil
|
45
45
|
end
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
46
|
+
return_error "No CSV data to validate" if !options[:schema] && source.nil?
|
47
|
+
end
|
48
|
+
else
|
49
|
+
# If the source isn't a URL, it's a file
|
50
|
+
unless /^http(s)?/.match?(source)
|
51
|
+
begin
|
52
|
+
source = File.new(source)
|
53
|
+
rescue Errno::ENOENT
|
54
|
+
return_error "#{source} not found"
|
54
55
|
end
|
55
56
|
end
|
56
|
-
|
57
|
-
source
|
58
57
|
end
|
59
58
|
|
60
|
-
|
61
|
-
|
62
|
-
schema = Csvlint::Schema.load_from_uri(schema, false)
|
63
|
-
rescue Csvlint::Csvw::MetadataError => e
|
64
|
-
return_error "invalid metadata: #{e.message}#{" at " + e.path if e.path}"
|
65
|
-
rescue OpenURI::HTTPError, Errno::ENOENT
|
66
|
-
return_error "#{options[:schema]} not found"
|
67
|
-
end
|
59
|
+
source
|
60
|
+
end
|
68
61
|
|
69
|
-
|
70
|
-
|
71
|
-
|
62
|
+
def get_schema(schema)
|
63
|
+
begin
|
64
|
+
schema = Csvlint::Schema.load_from_uri(schema, false)
|
65
|
+
rescue Csvlint::Csvw::MetadataError => e
|
66
|
+
return_error "invalid metadata: #{e.message}#{" at " + e.path if e.path}"
|
67
|
+
rescue OpenURI::HTTPError, Errno::ENOENT
|
68
|
+
return_error "#{options[:schema]} not found"
|
69
|
+
end
|
72
70
|
|
73
|
-
|
71
|
+
if schema.instance_of?(Csvlint::Schema) && schema.description == "malformed"
|
72
|
+
return_error "invalid metadata: malformed JSON"
|
74
73
|
end
|
75
74
|
|
76
|
-
|
77
|
-
|
75
|
+
schema
|
76
|
+
end
|
78
77
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
78
|
+
def fetch_schema_tables(schema, options)
|
79
|
+
valid = true
|
80
|
+
|
81
|
+
unless schema.instance_of? Csvlint::Csvw::TableGroup
|
82
|
+
return_error "No CSV data to validate."
|
83
|
+
end
|
84
|
+
schema.tables.keys.each do |source|
|
85
|
+
unless /^http(s)?/.match?(source)
|
83
86
|
begin
|
84
|
-
source = source.sub("file:","")
|
85
|
-
source = File.new(
|
87
|
+
source = source.sub("file:", "")
|
88
|
+
source = File.new(source)
|
86
89
|
rescue Errno::ENOENT
|
87
90
|
return_error "#{source} not found"
|
88
|
-
end unless source =~ /^http(s)?/
|
89
|
-
valid &= validate_csv(source, schema, options[:dump_errors], nil, options[:werror])
|
90
|
-
end
|
91
|
-
|
92
|
-
exit 1 unless valid
|
93
|
-
end
|
94
|
-
|
95
|
-
def print_error(index, error, dump, color)
|
96
|
-
location = ""
|
97
|
-
location += error.row.to_s if error.row
|
98
|
-
location += "#{error.row ? "," : ""}#{error.column.to_s}" if error.column
|
99
|
-
if error.row || error.column
|
100
|
-
location = "#{error.row ? "Row" : "Column"}: #{location}"
|
101
|
-
end
|
102
|
-
output_string = "#{index+1}. "
|
103
|
-
if error.column && @schema && @schema.class == Csvlint::Schema
|
104
|
-
if @schema.fields[error.column - 1] != nil
|
105
|
-
output_string += "#{@schema.fields[error.column - 1].name}: "
|
106
91
|
end
|
107
92
|
end
|
108
|
-
|
109
|
-
|
110
|
-
output_string += ". #{error.content}" if error.content
|
93
|
+
valid &= validate_csv(source, schema, options[:dump_errors], nil, options[:werror])
|
94
|
+
end
|
111
95
|
|
112
|
-
|
96
|
+
exit 1 unless valid
|
97
|
+
end
|
113
98
|
|
114
|
-
|
115
|
-
|
99
|
+
def print_error(index, error, dump, color)
|
100
|
+
location = ""
|
101
|
+
location += error.row.to_s if error.row
|
102
|
+
location += "#{error.row ? "," : ""}#{error.column}" if error.column
|
103
|
+
if error.row || error.column
|
104
|
+
location = "#{error.row ? "Row" : "Column"}: #{location}"
|
105
|
+
end
|
106
|
+
output_string = "#{index + 1}. "
|
107
|
+
if error.column && @schema && @schema.instance_of?(Csvlint::Schema)
|
108
|
+
if @schema.fields[error.column - 1] != nil
|
109
|
+
output_string += "#{@schema.fields[error.column - 1].name}: "
|
116
110
|
end
|
117
111
|
end
|
112
|
+
output_string += error.type.to_s
|
113
|
+
output_string += ". #{location}" unless location.empty?
|
114
|
+
output_string += ". #{error.content}" if error.content
|
118
115
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
116
|
+
puts Rainbow(output_string).color(color)
|
117
|
+
|
118
|
+
if dump
|
119
|
+
pp error
|
123
120
|
end
|
121
|
+
end
|
124
122
|
|
125
|
-
|
126
|
-
|
127
|
-
|
123
|
+
def print_errors(errors, dump)
|
124
|
+
if errors.size > 0
|
125
|
+
errors.each_with_index { |error, i| print_error(i, error, dump, :red) }
|
128
126
|
end
|
127
|
+
end
|
129
128
|
|
130
|
-
|
131
|
-
|
129
|
+
def return_error(message)
|
130
|
+
puts Rainbow(message).red
|
131
|
+
exit 1
|
132
|
+
end
|
132
133
|
|
133
|
-
|
134
|
-
|
135
|
-
else
|
136
|
-
validator = Csvlint::Validator.new( source, {}, schema, { lambda: report_lines } )
|
137
|
-
end
|
134
|
+
def validate_csv(source, schema, dump, json, werror)
|
135
|
+
@error_count = 0
|
138
136
|
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
csv = "CSV"
|
145
|
-
end
|
137
|
+
validator = if json === true
|
138
|
+
Csvlint::Validator.new(source, {}, schema)
|
139
|
+
else
|
140
|
+
Csvlint::Validator.new(source, {}, schema, {lambda: report_lines})
|
141
|
+
end
|
146
142
|
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
}
|
155
|
-
}.to_json
|
156
|
-
print json
|
157
|
-
else
|
158
|
-
puts "\r\n#{csv} is #{validator.valid? ? Rainbow("VALID").green : Rainbow("INVALID").red}"
|
159
|
-
print_errors(validator.errors, dump)
|
160
|
-
print_errors(validator.warnings, dump)
|
161
|
-
end
|
143
|
+
csv = if source.instance_of?(String)
|
144
|
+
source
|
145
|
+
elsif source.instance_of?(File)
|
146
|
+
source.path
|
147
|
+
else
|
148
|
+
"CSV"
|
149
|
+
end
|
162
150
|
|
163
|
-
|
164
|
-
|
151
|
+
if json === true
|
152
|
+
json = {
|
153
|
+
validation: {
|
154
|
+
state: validator.valid? ? "valid" : "invalid",
|
155
|
+
errors: validator.errors.map { |v| hashify(v) },
|
156
|
+
warnings: validator.warnings.map { |v| hashify(v) },
|
157
|
+
info: validator.info_messages.map { |v| hashify(v) }
|
158
|
+
}
|
159
|
+
}.to_json
|
160
|
+
print json
|
161
|
+
else
|
162
|
+
puts "\r\n#{csv} is #{validator.valid? ? Rainbow("VALID").green : Rainbow("INVALID").red}"
|
163
|
+
print_errors(validator.errors, dump)
|
164
|
+
print_errors(validator.warnings, dump)
|
165
165
|
end
|
166
166
|
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
category: error.category,
|
171
|
-
row: error.row,
|
172
|
-
col: error.column
|
173
|
-
}
|
174
|
-
|
175
|
-
if error.column && @schema && @schema.class == Csvlint::Schema && @schema.fields[error.column - 1] != nil
|
176
|
-
field = @schema.fields[error.column - 1]
|
177
|
-
h[:header] = field.name
|
178
|
-
h[:constraints] = Hash[field.constraints.map {|k,v| [k.underscore, v] }]
|
179
|
-
end
|
167
|
+
return false if werror && validator.warnings.size > 0
|
168
|
+
validator.valid?
|
169
|
+
end
|
180
170
|
|
181
|
-
|
171
|
+
def hashify(error)
|
172
|
+
h = {
|
173
|
+
type: error.type,
|
174
|
+
category: error.category,
|
175
|
+
row: error.row,
|
176
|
+
col: error.column
|
177
|
+
}
|
178
|
+
|
179
|
+
if error.column && @schema && @schema.instance_of?(Csvlint::Schema) && @schema.fields[error.column - 1] != nil
|
180
|
+
field = @schema.fields[error.column - 1]
|
181
|
+
h[:header] = field.name
|
182
|
+
h[:constraints] = field.constraints.map { |k, v| [k.underscore, v] }.to_h
|
182
183
|
end
|
183
184
|
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
185
|
+
h
|
186
|
+
end
|
187
|
+
|
188
|
+
def report_lines
|
189
|
+
lambda do |row|
|
190
|
+
new_errors = row.errors.count
|
191
|
+
if new_errors > @error_count
|
192
|
+
print Rainbow("!").red
|
193
|
+
else
|
194
|
+
print Rainbow(".").green
|
193
195
|
end
|
196
|
+
@error_count = new_errors
|
194
197
|
end
|
195
|
-
|
198
|
+
end
|
196
199
|
end
|
197
200
|
end
|