csv-probe 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -0
- data/README.md +2 -2
- data/csv-probe.gemspec +3 -1
- data/lib/csv/probe/checks.rb +29 -9
- data/lib/csv/probe/version.rb +1 -1
- data/lib/csv/probe.rb +2 -2
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83d63f020bf4ed1e557518b78820b440cc24da36a7d93f6ded23b604aa7161ed
|
4
|
+
data.tar.gz: df4e094f0f0451c9578ac5f02c34eb967d851a4782926dc64f0b96bdcd1eb9f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b3be13578b537577b62fd1efa51ec00f2d0a5a38755b2e8399dfeacf9f3ceaa1f30a65381336af5464cb4d08c32797bb0de5199f34f4e437041db9ec70ab681
|
7
|
+
data.tar.gz: 2eb6bfdeb32a7d3f819cf52b412b311984d7e45366426ed81a788ccf2f9a83504733cbe348c8da34421f23855eab2f8e5efe40e3fd4086a94a656758cdef3e4b
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -23,7 +23,7 @@ Or install it yourself as:
|
|
23
23
|
|
24
24
|
Example how to use Probe
|
25
25
|
|
26
|
-
```
|
26
|
+
```ruby
|
27
27
|
# load CSV into CSV::Table
|
28
28
|
csv_table = CSV.parse(<<~ROWS, headers: true)
|
29
29
|
col1,col2,col3,col4,col5,col6,col7
|
@@ -54,7 +54,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
54
54
|
|
55
55
|
## Contributing
|
56
56
|
|
57
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
57
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/homebase-dev/csv-probe.
|
58
58
|
|
59
59
|
## License
|
60
60
|
|
data/csv-probe.gemspec
CHANGED
@@ -9,12 +9,14 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.email = ["homebase.dev@gmail.com"]
|
10
10
|
|
11
11
|
spec.summary = "Allows validation of CSV::Table or CSV::Rows via custom rules"
|
12
|
-
spec.description = "This gem provides a simple framework for CSV::Table/CSV::Row
|
12
|
+
spec.description = "This gem provides a simple framework for CSV::Table/CSV::Row linting"\
|
13
|
+
"using custom rules for columns and rows"
|
13
14
|
spec.homepage = "https://gitlab.com/homebase-dev/csv-probe"
|
14
15
|
spec.license = "MIT"
|
15
16
|
spec.required_ruby_version = ">= 2.6.0"
|
16
17
|
|
17
18
|
# spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
|
19
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
18
20
|
|
19
21
|
spec.metadata["homepage_uri"] = spec.homepage
|
20
22
|
spec.metadata["source_code_uri"] = "https://gitlab.com/homebase-dev/csv-probe"
|
data/lib/csv/probe/checks.rb
CHANGED
@@ -5,30 +5,35 @@ module Probe
|
|
5
5
|
|
6
6
|
class LintingError < Error; end
|
7
7
|
|
8
|
+
# Linting error in CSV::Row, if the rule applies to multiple columns of a row
|
8
9
|
class RowError < LintingError
|
9
10
|
def initialize(msg = "CheckRowError")
|
10
11
|
super
|
11
12
|
end
|
12
13
|
end
|
13
14
|
|
15
|
+
# Linting warning in CSV::Row, if the rule applies to multiple columns of a row
|
14
16
|
class RowWarning < LintingError
|
15
17
|
def initialize(msg = "CheckRowWarning")
|
16
18
|
super
|
17
19
|
end
|
18
20
|
end
|
19
21
|
|
22
|
+
# Linting error in CSV::Column, if the rule applies to a single column of a row
|
20
23
|
class ColumnError < LintingError
|
21
24
|
def initialize(msg = "CheckColumnError")
|
22
25
|
super
|
23
26
|
end
|
24
27
|
end
|
25
28
|
|
29
|
+
# Linting warning in CSV::Column, if the rule applies to a single column of a row
|
26
30
|
class ColumnWarning < LintingError
|
27
31
|
def initialize(msg = "CheckColumnWarning")
|
28
32
|
super
|
29
33
|
end
|
30
34
|
end
|
31
35
|
|
36
|
+
# Abstract class of a CSV::Row check
|
32
37
|
class RowMeetsCondition
|
33
38
|
attr_accessor :ok_condition_fn, :fail_msg, :severity, :pre_checks
|
34
39
|
|
@@ -102,6 +107,7 @@ module Probe
|
|
102
107
|
end
|
103
108
|
end
|
104
109
|
|
110
|
+
# Abstract class of a CSV::Row single field (= column) check
|
105
111
|
class ColumnMeetsCondition < RowMeetsCondition
|
106
112
|
attr_accessor :varname, :ok_condition_fn, :fail_msg
|
107
113
|
|
@@ -120,10 +126,14 @@ module Probe
|
|
120
126
|
|
121
127
|
def evaluate(row, opts = {})
|
122
128
|
evaluate_pre_checks(row, opts)
|
123
|
-
|
129
|
+
|
130
|
+
unless @ok_condition_fn.call(row.fetch(@varname), opts) # rubocop:disable Style/GuardClause
|
131
|
+
raise @error_classes.fetch(@severity), error_msg(row, opts)
|
132
|
+
end
|
124
133
|
end
|
125
134
|
end
|
126
135
|
|
136
|
+
# Check if a column value is equal to a provided value (Pay attention to types!)
|
127
137
|
class ColumnIsEqualTo < ColumnMeetsCondition
|
128
138
|
def initialize(varname, expected_val, _placeholder = nil)
|
129
139
|
super(varname, nil, nil)
|
@@ -132,6 +142,7 @@ module Probe
|
|
132
142
|
end
|
133
143
|
end
|
134
144
|
|
145
|
+
# Check if a column value is one of a list of provided value
|
135
146
|
class ColumnIsOneOf < ColumnMeetsCondition
|
136
147
|
def initialize(varname, expected_vals_arr, _placeholder = nil)
|
137
148
|
super(varname, nil, nil)
|
@@ -140,8 +151,9 @@ module Probe
|
|
140
151
|
end
|
141
152
|
end
|
142
153
|
|
154
|
+
# Check if a column value is a date with a given format
|
143
155
|
class ColumnIsDate < ColumnMeetsCondition
|
144
|
-
def initialize(varname, expected_date_format, _placeholder = nil)
|
156
|
+
def initialize(varname, expected_date_format, _placeholder = nil) # rubocop:disable Metrics/MethodLength
|
145
157
|
super(varname, nil, nil)
|
146
158
|
@ok_condition_fn = lambda { |val, _cfg|
|
147
159
|
success = true
|
@@ -159,11 +171,13 @@ module Probe
|
|
159
171
|
# class ColumnIsInteger < ColumnMeetsCondition
|
160
172
|
# def initialize(varname, placeholder1=nil, placeholder=nil)
|
161
173
|
# super(varname, nil, nil)
|
162
|
-
# @ok_condition_fn = ->(val, cfg){ val.is_a? Integer }
|
174
|
+
# @ok_condition_fn = ->(val, cfg){ val.is_a? Integer }
|
175
|
+
# # TODO: ATTENTION 12abc -> integer, also 1.1 -> 1 ! better do it over regex?
|
163
176
|
# @fail_msg = "expected to be an Integer"
|
164
177
|
# end
|
165
178
|
# end
|
166
179
|
|
180
|
+
# Check if a column value is a date with a given format
|
167
181
|
class ColumnMatchesRegEx < ColumnMeetsCondition
|
168
182
|
def initialize(varname, expected_regex_pattern, _placeholder = nil)
|
169
183
|
super(varname, nil, nil)
|
@@ -172,9 +186,9 @@ module Probe
|
|
172
186
|
end
|
173
187
|
end
|
174
188
|
|
175
|
-
#
|
189
|
+
# Check if a tokenized column value is a list of given values
|
176
190
|
class ColumnIsListWithDomain < ColumnMeetsCondition
|
177
|
-
def initialize(varname, expected_items_arr, separator, _placeholder = nil)
|
191
|
+
def initialize(varname, expected_items_arr, separator, _placeholder = nil) # rubocop:disable Metrics/MethodLength
|
178
192
|
super(varname, nil, nil)
|
179
193
|
@ok_condition_fn = lambda { |val, _cfg|
|
180
194
|
items = val.split(separator)
|
@@ -183,13 +197,15 @@ module Probe
|
|
183
197
|
@fail_msg = lambda { |row, _opts|
|
184
198
|
items = row.fetch(@varname).split(separator)
|
185
199
|
diff_items = items - expected_items_arr
|
186
|
-
"expected that tokenized items of value #{items.inspect} are a subset of
|
200
|
+
"expected that tokenized items of value #{items.inspect} are a subset of "\
|
201
|
+
"#{expected_items_arr.inspect}, but items #{diff_items.inspect} are not"
|
187
202
|
}
|
188
203
|
end
|
189
204
|
end
|
190
205
|
|
206
|
+
# Check if a tokenized column value is a list of given values
|
191
207
|
class ColumnIsSet < ColumnMeetsCondition
|
192
|
-
def initialize(varname, separator, _placeholder = nil)
|
208
|
+
def initialize(varname, separator, _placeholder = nil) # rubocop:disable Metrics/MethodLength
|
193
209
|
super(varname, nil, nil)
|
194
210
|
@ok_condition_fn = lambda { |val, _cfg|
|
195
211
|
return true if val.to_s == ""
|
@@ -206,8 +222,9 @@ module Probe
|
|
206
222
|
end
|
207
223
|
end
|
208
224
|
|
225
|
+
# Check if a tokenized column value is a set of given values (no duplicates)
|
209
226
|
class ColumnIsSetWithDomain < ColumnMeetsCondition
|
210
|
-
def initialize(varname, expected_items_arr, separator, _placeholder = nil)
|
227
|
+
def initialize(varname, expected_items_arr, separator, _placeholder = nil) # rubocop:disable Metrics/MethodLength
|
211
228
|
super(varname, nil, nil)
|
212
229
|
@pre_checks << ColumnIsSet.new(varname, separator)
|
213
230
|
@ok_condition_fn = lambda { |val, _cfg|
|
@@ -221,7 +238,10 @@ module Probe
|
|
221
238
|
items = row.fetch(@varname).split(separator)
|
222
239
|
"expected that items of tokenized value #{items.inspect} are a subset of #{expected_items_arr.inspect}"
|
223
240
|
}
|
224
|
-
# @fail_msg =
|
241
|
+
# @fail_msg = lambda {|row, opts|
|
242
|
+
# "Unexpected value:#{row.fetch(@varname).inspect} for column:#{@varname.inspect},
|
243
|
+
# expected that items of tokenized value #{row.fetch(@varname).split(separator)}
|
244
|
+
# are uniqe and a subset of #{expected_items_arr.inspect}" }
|
225
245
|
end
|
226
246
|
end
|
227
247
|
|
data/lib/csv/probe/version.rb
CHANGED
data/lib/csv/probe.rb
CHANGED
@@ -43,7 +43,7 @@ module Probe
|
|
43
43
|
end
|
44
44
|
|
45
45
|
# Extend CSV::Table with .lint(...) method
|
46
|
-
class CSV::Table
|
46
|
+
class CSV::Table # rubocop:disable Style/ClassAndModuleChildren
|
47
47
|
def lint(checks, opts = {})
|
48
48
|
opts[:headers] = true unless opts.key?(:headers) # CSV::Table has always headers, hence set :headers = true
|
49
49
|
Probe.lint_rows(self, checks, opts)
|
@@ -51,7 +51,7 @@ class CSV::Table
|
|
51
51
|
end
|
52
52
|
|
53
53
|
# Extend CSV::Row with .lint(...) method
|
54
|
-
class CSV::Row
|
54
|
+
class CSV::Row # rubocop:disable Style/ClassAndModuleChildren
|
55
55
|
def lint(checks, opts = {})
|
56
56
|
Probe.lint_row(self, checks, opts)
|
57
57
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv-probe
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- homebase.dev
|
@@ -38,8 +38,8 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '3.0'
|
41
|
-
description: This gem provides a simple framework for CSV::Table/CSV::Row
|
42
|
-
|
41
|
+
description: This gem provides a simple framework for CSV::Table/CSV::Row lintingusing
|
42
|
+
custom rules for columns and rows
|
43
43
|
email:
|
44
44
|
- homebase.dev@gmail.com
|
45
45
|
executables: []
|
@@ -64,6 +64,7 @@ homepage: https://gitlab.com/homebase-dev/csv-probe
|
|
64
64
|
licenses:
|
65
65
|
- MIT
|
66
66
|
metadata:
|
67
|
+
rubygems_mfa_required: 'true'
|
67
68
|
homepage_uri: https://gitlab.com/homebase-dev/csv-probe
|
68
69
|
source_code_uri: https://gitlab.com/homebase-dev/csv-probe
|
69
70
|
changelog_uri: https://gitlab.com/homebase-dev/csv-probe
|