valigator-csv 1.3.2 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/valigator/csv.rb +1 -0
- data/lib/valigator/csv/row_validators.rb +7 -0
- data/lib/valigator/csv/row_validators/ragged.rb +50 -0
- data/lib/valigator/csv/validator.rb +31 -11
- data/lib/valigator/csv/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9790896dcecf7ab173199ebe493c2f568e0c0993
|
4
|
+
data.tar.gz: c19072ad47075f87e2679797fef326aa1de06289
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 35b73a4021a28aa0f87940e5e145fbd4193df80836632285bdb707e4fe28c954d0e3c7c3848425060728fc35ca85ca874baf29d822df561179049020007025e0
|
7
|
+
data.tar.gz: 763289f77dcabb13230e89563a901d8b8b43c76b3278ecb333becc062a45427d9a3459972509ecfd11227dd74e68cc259206e828380ef2625a62f3a7dda4cbd9
|
data/lib/valigator/csv.rb
CHANGED
@@ -5,6 +5,7 @@ module Valigator
|
|
5
5
|
autoload :ErrorsLimitReachedError, 'valigator/csv/error'
|
6
6
|
autoload :Validator, 'valigator/csv/validator'
|
7
7
|
autoload :FieldValidators, 'valigator/csv/field_validators'
|
8
|
+
autoload :RowValidators, 'valigator/csv/row_validators'
|
8
9
|
autoload :Version, 'valigator/csv/version'
|
9
10
|
end
|
10
11
|
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Valigator
|
2
|
+
module CSV
|
3
|
+
module RowValidators
|
4
|
+
class Ragged
|
5
|
+
attr_reader :options
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
def initialize(options = {})
|
10
|
+
@options = options
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
def valid?(row)
|
16
|
+
return true unless fields
|
17
|
+
|
18
|
+
row.size >= fields.size
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
def error_type
|
24
|
+
'ragged_row'
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
def error_message
|
30
|
+
'Ragged or empty row'
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
def ==(other)
|
36
|
+
self.class == other.class && options == other.options
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def fields
|
44
|
+
options[:fields]
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -18,8 +18,11 @@ module Valigator
|
|
18
18
|
def validate(options = {})
|
19
19
|
config.merge! options
|
20
20
|
|
21
|
-
::CSV.foreach(
|
21
|
+
::CSV.foreach(filename, csv_options(options)) do |row|
|
22
22
|
validate_fields row, options
|
23
|
+
validate_row row, options
|
24
|
+
|
25
|
+
stop_if_error_limit_reached
|
23
26
|
end
|
24
27
|
rescue ErrorsLimitReachedError
|
25
28
|
rescue ::CSV::MalformedCSVError, ArgumentError => error
|
@@ -57,26 +60,43 @@ module Valigator
|
|
57
60
|
|
58
61
|
|
59
62
|
|
60
|
-
def validate_fields(row, options={})
|
63
|
+
def validate_fields(row, options = {})
|
61
64
|
return unless options[:fields] && options[:field_validators]
|
62
65
|
|
63
66
|
options[:fields].each_with_index do |field, index|
|
64
|
-
|
65
|
-
|
66
|
-
if field_validator and !field_validator.valid?(row[index])
|
67
|
-
add_field_error(field, field_validator)
|
67
|
+
validator = options[:field_validators][field]
|
68
68
|
|
69
|
-
|
70
|
-
|
71
|
-
raise ErrorsLimitReachedError
|
72
|
-
end
|
69
|
+
if validator && !validator.valid?(row[index])
|
70
|
+
add_error_for(validator, field)
|
73
71
|
end
|
74
72
|
end
|
75
73
|
end
|
76
74
|
|
77
75
|
|
78
76
|
|
79
|
-
def
|
77
|
+
def stop_if_error_limit_reached
|
78
|
+
if config[:errors_limit] && errors.size >= config[:errors_limit]
|
79
|
+
errors << CSV::Error.new({type: 'too_many_errors', message: 'Too many errors were found'})
|
80
|
+
raise ErrorsLimitReachedError
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
def validate_row(row, options = {})
|
87
|
+
return unless options[:fields] && options[:row_validators]
|
88
|
+
|
89
|
+
options[:row_validators].each do |validator_class|
|
90
|
+
validator = validator_class.new(options)
|
91
|
+
next if validator.valid?(row)
|
92
|
+
|
93
|
+
add_error_for validator
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
def add_error_for(validator, field = nil)
|
80
100
|
errors << CSV::Error.new({
|
81
101
|
type: validator.error_type,
|
82
102
|
message: validator.error_message,
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: valigator-csv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Nagy
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2015-12-
|
13
|
+
date: 2015-12-15 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|
@@ -80,6 +80,8 @@ files:
|
|
80
80
|
- lib/valigator/csv/field_validators/float.rb
|
81
81
|
- lib/valigator/csv/field_validators/integer.rb
|
82
82
|
- lib/valigator/csv/field_validators/mandatory.rb
|
83
|
+
- lib/valigator/csv/row_validators.rb
|
84
|
+
- lib/valigator/csv/row_validators/ragged.rb
|
83
85
|
- lib/valigator/csv/validator.rb
|
84
86
|
- lib/valigator/csv/version.rb
|
85
87
|
- valigator-csv.gemspec
|