valigator-csv 1.3.2 → 1.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1500ba49fcf43b33da16bf6fa4c1afc9b5cb3a09
4
- data.tar.gz: 9f561c1980c74376fd7aaf403220425627bb74e3
3
+ metadata.gz: 9790896dcecf7ab173199ebe493c2f568e0c0993
4
+ data.tar.gz: c19072ad47075f87e2679797fef326aa1de06289
5
5
  SHA512:
6
- metadata.gz: 4b74a540d50691ebf09e7185872d7508ba57270e0d2a3b220537a4cd757c3341944dfe3df7f63fa9e2b17c736d9f0a22b2c1fe388da0400f05f3d5bf9b6717cf
7
- data.tar.gz: 5d587bff1096e951c4c9acab9c7db9724dfc2216668571346b0abef50dc2ffb21595311d3dbde4ab329e23e8e1894ee6842566fff3b8c3f8de9e8909b6f19997
6
+ metadata.gz: 35b73a4021a28aa0f87940e5e145fbd4193df80836632285bdb707e4fe28c954d0e3c7c3848425060728fc35ca85ca874baf29d822df561179049020007025e0
7
+ data.tar.gz: 763289f77dcabb13230e89563a901d8b8b43c76b3278ecb333becc062a45427d9a3459972509ecfd11227dd74e68cc259206e828380ef2625a62f3a7dda4cbd9
@@ -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,7 @@
1
+ module Valigator
2
+ module CSV
3
+ module RowValidators
4
+ autoload :Ragged, 'valigator/csv/row_validators/ragged'
5
+ end
6
+ end
7
+ 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(@filename, csv_options(options)) do |row|
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
- field_validator = options[:field_validators][field]
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
- if config[:errors_limit] && errors.size >= config[:errors_limit]
70
- errors << CSV::Error.new({type: 'too_many_errors', message: 'Too many errors were found'})
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 add_field_error(field, validator)
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,
@@ -1,5 +1,5 @@
1
1
  module Valigator
2
2
  module CSV
3
- VERSION = "1.3.2"
3
+ VERSION = "1.4.0"
4
4
  end
5
5
  end
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.3.2
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-12 00:00:00.000000000 Z
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