valigator-csv 1.3.1 → 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3603756bf634bc260cf48f545f60775106fd7dd5
4
- data.tar.gz: 363ddd31daac8f72f47ba0b3db1f3a6db44df74b
3
+ metadata.gz: 1500ba49fcf43b33da16bf6fa4c1afc9b5cb3a09
4
+ data.tar.gz: 9f561c1980c74376fd7aaf403220425627bb74e3
5
5
  SHA512:
6
- metadata.gz: c3886a84957480ad2169738266d259406e6f76083c3fda709d720746b59e351343f0423553cb3fbb8489fd17b1950b302682acae53a96e120926cb1e74383a79
7
- data.tar.gz: 1ad3987358197d2362fe411dedb7102166973664063ec15f2e8256b70baba9925da2724f82213b99378a425c226fc1e55a43bade28ad7de8ff7abf66aa3cb48a
6
+ metadata.gz: 4b74a540d50691ebf09e7185872d7508ba57270e0d2a3b220537a4cd757c3341944dfe3df7f63fa9e2b17c736d9f0a22b2c1fe388da0400f05f3d5bf9b6717cf
7
+ data.tar.gz: 5d587bff1096e951c4c9acab9c7db9724dfc2216668571346b0abef50dc2ffb21595311d3dbde4ab329e23e8e1894ee6842566fff3b8c3f8de9e8909b6f19997
@@ -1,5 +1,9 @@
1
1
  module Valigator
2
2
  module CSV
3
+ class ErrorsLimitReachedError < StandardError
4
+ end
5
+
6
+
3
7
  class UnhandledTypeError < StandardError
4
8
  end
5
9
 
@@ -4,9 +4,7 @@ module Valigator
4
4
  class Float < Integer
5
5
 
6
6
  def valid?(value)
7
- formatted = formatted_value(value)
8
-
9
- super || value.is_a?(::Float) || formatted.to_f.to_s == formatted
7
+ super || value.is_a?(::Float) || formatted_float?(value)
10
8
  end
11
9
 
12
10
 
@@ -31,11 +29,19 @@ module Valigator
31
29
 
32
30
 
33
31
 
32
+ def formatted_float?(value)
33
+ Float(formatted_value(value))
34
+ true
35
+ rescue ArgumentError, TypeError
36
+ false
37
+ end
38
+
39
+
40
+
34
41
  def formatted_value(value)
35
42
  decimal_mark ? value.to_s.gsub(decimal_mark, '.') : value
36
43
  end
37
44
 
38
-
39
45
  end
40
46
  end
41
47
  end
@@ -3,31 +3,42 @@ require 'csv'
3
3
  module Valigator
4
4
  module CSV
5
5
  class Validator
6
- attr_reader :filename, :errors
6
+ attr_reader :filename, :errors, :config
7
7
 
8
8
 
9
9
 
10
10
  def initialize(filename)
11
11
  @filename = filename
12
12
  @errors = []
13
+ @config = default_config
13
14
  end
14
15
 
15
16
 
16
17
 
17
18
  def validate(options = {})
19
+ config.merge! options
20
+
18
21
  ::CSV.foreach(@filename, csv_options(options)) do |row|
19
22
  validate_fields row, options
20
23
  end
24
+ rescue ErrorsLimitReachedError
21
25
  rescue ::CSV::MalformedCSVError, ArgumentError => error
22
26
  raise if unrelated_error?(error)
23
27
 
24
- @errors << CSV::Error.new(error)
28
+ errors << CSV::Error.new(error)
25
29
  end
26
30
 
27
31
 
28
32
 
29
33
  private
30
34
 
35
+
36
+ def default_config
37
+ {errors_limit: 1000}
38
+ end
39
+
40
+
41
+
31
42
  def csv_options(options = {})
32
43
  {
33
44
  col_sep: options[:col_sep] || ',',
@@ -54,6 +65,11 @@ module Valigator
54
65
 
55
66
  if field_validator and !field_validator.valid?(row[index])
56
67
  add_field_error(field, field_validator)
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
57
73
  end
58
74
  end
59
75
  end
@@ -61,7 +77,7 @@ module Valigator
61
77
 
62
78
 
63
79
  def add_field_error(field, validator)
64
- @errors << CSV::Error.new({
80
+ errors << CSV::Error.new({
65
81
  type: validator.error_type,
66
82
  message: validator.error_message,
67
83
  row: $INPUT_LINE_NUMBER,
@@ -1,5 +1,5 @@
1
1
  module Valigator
2
2
  module CSV
3
- VERSION = "1.3.1"
3
+ VERSION = "1.3.2"
4
4
  end
5
5
  end
data/lib/valigator/csv.rb CHANGED
@@ -2,6 +2,7 @@ module Valigator
2
2
  module CSV
3
3
  autoload :Error, 'valigator/csv/error'
4
4
  autoload :UnhandledTypeError, 'valigator/csv/error'
5
+ autoload :ErrorsLimitReachedError, 'valigator/csv/error'
5
6
  autoload :Validator, 'valigator/csv/validator'
6
7
  autoload :FieldValidators, 'valigator/csv/field_validators'
7
8
  autoload :Version, 'valigator/csv/version'
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.1
4
+ version: 1.3.2
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-10 00:00:00.000000000 Z
13
+ date: 2015-12-12 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler