valigator-csv 1.2.0 → 1.3.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: 170abf203c29a7cdf2543ced369e864844a8c2d8
4
- data.tar.gz: d220e53500d76ab4ee8d4a63e38ffee6cf291c59
3
+ metadata.gz: 36b7a576342bf50503a9116df95032cb815a88e9
4
+ data.tar.gz: 55257cf5e9b02ea0c66820279fdbc2caad6270e6
5
5
  SHA512:
6
- metadata.gz: 667130ce6748b198ee754439b7492d91f0e168d2fbeebb38a6fa7af4e06f6d7ba0597b22bc40a45f6bba2601b9ba4bf7dd01c4c52630eb6fbbf1d145e662231a
7
- data.tar.gz: 4891810ae82bbe5428c1447b4d3149432994c05915e8f7808ef578402bb99c21b8c5aefddbf7aefcca9c42234f264f7510ce57f28b505ce7a3ffa93e6cc215db
6
+ metadata.gz: d41045648d6d141622f6d0b9bd4d48330af1e2e3e8a10c8d5dc141b8cb6047232443a4f3d62064bef15dd8e80ecfcda037d35bccadd050754ac7314ad6cd2240
7
+ data.tar.gz: 077b6fc2fc0bec290557ea9b2860cebef2cd2e94c3888ffcb3c0bd75dc2bc6e4003e611494b538a27ffe2cfb46117d7d180de0a6a8f73d7992907f4099bac6fa
@@ -6,7 +6,7 @@ module Valigator
6
6
 
7
7
  class Error
8
8
 
9
- attr_reader :row, :type, :message
9
+ attr_reader :row, :type, :message, :field
10
10
 
11
11
 
12
12
 
@@ -22,7 +22,7 @@ module Valigator
22
22
 
23
23
 
24
24
  def ==(other)
25
- row == other.row && message == other.message && type == other.type
25
+ row == other.row && message == other.message && type == other.type && field == other.field
26
26
  end
27
27
 
28
28
 
@@ -31,7 +31,8 @@ module Valigator
31
31
  {
32
32
  row: row,
33
33
  type: type,
34
- message: message
34
+ message: message,
35
+ field: field
35
36
  }
36
37
  end
37
38
 
@@ -40,7 +41,7 @@ module Valigator
40
41
  private
41
42
 
42
43
  def build_from_hash(error)
43
- build error[:type], error[:message], error[:row]
44
+ build error[:type], error[:message], error[:row], error[:field]
44
45
  end
45
46
 
46
47
 
@@ -58,10 +59,11 @@ module Valigator
58
59
 
59
60
 
60
61
 
61
- def build(type, message, row)
62
+ def build(type, message, row, field=nil)
62
63
  @type = type
63
64
  @row = row
64
65
  @message = message
66
+ @field = field
65
67
  end
66
68
 
67
69
 
@@ -0,0 +1,42 @@
1
+ module Valigator
2
+ module CSV
3
+ module FieldValidators
4
+ class Date
5
+
6
+ def initialize(options={})
7
+ @options = options
8
+ end
9
+
10
+
11
+
12
+ def valid?(value)
13
+ format ? ::Date.strptime(value, format) : ::Date.parse(value)
14
+ true
15
+ rescue ArgumentError
16
+ false
17
+ end
18
+
19
+
20
+
21
+ def error_type
22
+ 'invalid_date'
23
+ end
24
+
25
+
26
+
27
+ def error_message
28
+ 'Invalid date field'
29
+ end
30
+
31
+
32
+
33
+ private
34
+
35
+ def format
36
+ @options[:format]
37
+ end
38
+
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,48 @@
1
+ module Valigator
2
+ module CSV
3
+ module FieldValidators
4
+ class Float < Integer
5
+
6
+ def initialize(options = {})
7
+ @options = options
8
+ end
9
+
10
+
11
+
12
+ def valid?(value)
13
+ formatted = formatted_value(value)
14
+
15
+ formatted.to_f.to_s == formatted || super
16
+ end
17
+
18
+
19
+
20
+ def error_type
21
+ 'invalid_float'
22
+ end
23
+
24
+
25
+
26
+ def error_message
27
+ 'Invalid float field'
28
+ end
29
+
30
+
31
+
32
+ private
33
+
34
+ def decimal_mark
35
+ @options[:decimal_mark]
36
+ end
37
+
38
+
39
+
40
+ def formatted_value(value)
41
+ decimal_mark ? value.gsub(decimal_mark, '.') : value
42
+ end
43
+
44
+
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,26 @@
1
+ module Valigator
2
+ module CSV
3
+ module FieldValidators
4
+ class Integer
5
+
6
+ def valid?(value)
7
+ return true if value.to_s.empty?
8
+
9
+ value.to_i.to_s == value.to_s
10
+ end
11
+
12
+
13
+
14
+ def error_type
15
+ 'invalid_integer'
16
+ end
17
+
18
+
19
+
20
+ def error_message
21
+ 'Invalid integer field'
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,24 @@
1
+ module Valigator
2
+ module CSV
3
+ module FieldValidators
4
+ class Mandatory
5
+
6
+ def valid?(value)
7
+ !value.to_s.empty?
8
+ end
9
+
10
+
11
+
12
+ def error_type
13
+ 'missing_field'
14
+ end
15
+
16
+
17
+
18
+ def error_message
19
+ 'Missing mandatory field'
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,10 @@
1
+ module Valigator
2
+ module CSV
3
+ module FieldValidators
4
+ autoload :Date, 'valigator/csv/field_validators/date'
5
+ autoload :Float, 'valigator/csv/field_validators/float'
6
+ autoload :Integer, 'valigator/csv/field_validators/integer'
7
+ autoload :Mandatory, 'valigator/csv/field_validators/mandatory'
8
+ end
9
+ end
10
+ end
@@ -15,7 +15,9 @@ module Valigator
15
15
 
16
16
 
17
17
  def validate(options = {})
18
- ::CSV.foreach(@filename, build_options(options)) { |_row|}
18
+ ::CSV.foreach(@filename, csv_options(options)) do |row|
19
+ validate_fields row, options
20
+ end
19
21
  rescue ::CSV::MalformedCSVError, ArgumentError => error
20
22
  raise if unrelated_error?(error)
21
23
 
@@ -26,11 +28,13 @@ module Valigator
26
28
 
27
29
  private
28
30
 
29
- def build_options(options = {})
31
+ def csv_options(options = {})
30
32
  {
31
33
  col_sep: options[:col_sep] || ',',
32
34
  quote_char: options[:quote_char] || '"',
33
- encoding: options[:encoding] || 'UTF-8'
35
+ encoding: options[:encoding] || 'UTF-8',
36
+ headers: options[:headers] || false,
37
+ return_headers: options[:return_headers] || false
34
38
  }
35
39
  end
36
40
 
@@ -40,6 +44,31 @@ module Valigator
40
44
  error.is_a?(ArgumentError) && error.message != 'invalid byte sequence in UTF-8'
41
45
  end
42
46
 
47
+
48
+
49
+ def validate_fields(row, options={})
50
+ return unless options[:fields] && options[:field_validators]
51
+
52
+ options[:fields].each_with_index do |field, index|
53
+ field_validator = options[:field_validators][field]
54
+
55
+ if field_validator and !field_validator.valid?(row[index])
56
+ add_field_error(field, field_validator)
57
+ end
58
+ end
59
+ end
60
+
61
+
62
+
63
+ def add_field_error(field, validator)
64
+ @errors << CSV::Error.new({
65
+ type: validator.error_type,
66
+ message: validator.error_message,
67
+ row: $INPUT_LINE_NUMBER,
68
+ field: field
69
+ })
70
+ end
71
+
43
72
  end
44
73
  end
45
74
  end
@@ -1,5 +1,5 @@
1
1
  module Valigator
2
2
  module CSV
3
- VERSION = "1.2.0"
3
+ VERSION = "1.3.0"
4
4
  end
5
5
  end
data/lib/valigator/csv.rb CHANGED
@@ -3,6 +3,7 @@ module Valigator
3
3
  autoload :Error, 'valigator/csv/error'
4
4
  autoload :UnhandledTypeError, 'valigator/csv/error'
5
5
  autoload :Validator, 'valigator/csv/validator'
6
+ autoload :FieldValidators, 'valigator/csv/field_validators'
6
7
  autoload :Version, 'valigator/csv/version'
7
8
  end
8
9
  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.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Nagy
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-12-03 00:00:00.000000000 Z
12
+ date: 2015-12-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -72,6 +72,11 @@ files:
72
72
  - lib/valigator.rb
73
73
  - lib/valigator/csv.rb
74
74
  - lib/valigator/csv/error.rb
75
+ - lib/valigator/csv/field_validators.rb
76
+ - lib/valigator/csv/field_validators/date.rb
77
+ - lib/valigator/csv/field_validators/float.rb
78
+ - lib/valigator/csv/field_validators/integer.rb
79
+ - lib/valigator/csv/field_validators/mandatory.rb
75
80
  - lib/valigator/csv/validator.rb
76
81
  - lib/valigator/csv/version.rb
77
82
  - valigator-csv.gemspec