poro_validator 0.2.1 → 0.2.2

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: 8ee244b3419abaf9da6d989f4f2ee3aa85438808
4
- data.tar.gz: d8ab4bf0e662566812d6f587f91be927c2d5f2e5
3
+ metadata.gz: 7ab2a0ee9dbb6cacf404dd00408207233968b22c
4
+ data.tar.gz: 5c43eeef7b7be9250b85f3c632648ff9f6cf8ffb
5
5
  SHA512:
6
- metadata.gz: e2badd5a79da6691e5e9c5a19cf879166690f01306630e90f424b6644a9a9c6e4818ac3d613a3b10e46dfee753ab302de912c918139d53f278edc6671ba9fbd5
7
- data.tar.gz: be0f9fd1d0b8f56a6bc100e94883330a0456ce3962277603fcbbf67b5b6c7b29e0f518bd4f35f834fc61b74c1d0e2f7e9d5825890335beeb879a83070e10e2ee
6
+ metadata.gz: d7bd91417be08c22f3a2bedecd8d932ada2e23f3dc6f5b9890a16fbaf2c81ee2cd9d252bece9b0aa4381aa941f70beb4a74d4220c8982cb35a5e4ae56fa24f4c
7
+ data.tar.gz: b3967f199b60e1778c94fb5e481532858ce451457d7afe4067dae966df0d645c60ea28b6c4e5f1a71afc4ee0cb498103d911d04d2410f086eba10009c3b5a95a
@@ -53,10 +53,10 @@ require "poro_validator/validators/with_validator"
53
53
  require "poro_validator/validators/range_array_validator"
54
54
  require "poro_validator/validators/inclusion_validator"
55
55
  require "poro_validator/validators/exclusion_validator"
56
+ require "poro_validator/validators/numeric_validator"
56
57
  require "poro_validator/validators/integer_validator"
57
58
  require "poro_validator/validators/float_validator"
58
59
  require "poro_validator/validators/length_validator"
59
- require "poro_validator/validators/numeric_validator"
60
60
 
61
61
 
62
62
  # Modules
@@ -8,6 +8,10 @@ module PoroValidator
8
8
  integer: lambda { "is not an integer" },
9
9
  float: lambda { "is not a float" },
10
10
 
11
+ integer_or_float: lambda do
12
+ "is not an integer or float type"
13
+ end,
14
+
11
15
  inclusion: lambda do |range|
12
16
  "is not within the range of #{range.inspect}"
13
17
  end,
@@ -1,17 +1,14 @@
1
1
  module PoroValidator
2
2
  module Validators
3
- class FloatValidator < BaseClass
3
+ class FloatValidator < NumericValidator
4
4
  def validate(attribute, value, options)
5
- message = options.fetch(:message, :float)
6
-
7
- begin
8
- Kernel.Float(value.to_s)
9
- nil
10
- rescue
11
- errors.add(attribute, message)
5
+ if is_numeric?(value.to_s, FLOAT_MATCHER)
6
+ return
12
7
  end
8
+
9
+ message = options[:message] || :float
10
+ errors.add(attribute, message)
13
11
  end
14
12
  end
15
13
  end
16
14
  end
17
-
@@ -1,15 +1,13 @@
1
1
  module PoroValidator
2
2
  module Validators
3
- class IntegerValidator < BaseClass
3
+ class IntegerValidator < NumericValidator
4
4
  def validate(attribute, value, options)
5
- message = options.fetch(:message, :integer)
6
-
7
- begin
8
- Kernel.Integer(value.to_s)
9
- nil
10
- rescue
11
- errors.add(attribute, message)
5
+ if is_numeric?(value.to_s, INTEGER_MATCHER)
6
+ return nil
12
7
  end
8
+
9
+ message = options[:message] || :integer
10
+ errors.add(attribute, message)
13
11
  end
14
12
 
15
13
  end
@@ -1,47 +1,77 @@
1
1
  module PoroValidator
2
2
  module Validators
3
3
  class NumericValidator < BaseClass
4
- def validate(attribute, value, options)
5
- message = options.fetch(:message, :numeric)
4
+ INTEGER_MATCHER = /\A[-+]?(\d+|\d{1,3}(,\d{3})*)\z/.freeze
5
+ FLOAT_MATCHER = /\A[-+]?(\d+|\d{1,3}(,\d{3})*)(\.\d+)\z/.freeze
6
+
7
+ VALID_OPTIONS = [
8
+ :extremum, :max, :min, :in
9
+ ].freeze
6
10
 
7
- begin
8
- Kernel.Integer(value.to_s)
9
- nil
10
- rescue
11
- errors.add(attribute, :integer)
11
+ VALID_OPTION_VALUES = [
12
+ ::Range, ::Array, ::Fixnum, ::Numeric
13
+ ].freeze
14
+
15
+ def validate(attribute, value, options)
16
+ unless is_numeric?(value.to_s, INTEGER_MATCHER) ||
17
+ is_numeric?(value.to_s, FLOAT_MATCHER)
18
+ errors.add(attribute, :integer_or_float)
12
19
  return
13
20
  end
14
21
 
15
- options.keys.each do |key|
16
- matcher = matchers(key)
17
- next if matcher.nil?
18
- unless matchers(key).call(value, options[key])
19
- errors.add(attribute, message, key => options[key])
22
+ message = options[:message] || :numeric
23
+
24
+ validate_numeric_options(options.keys)
25
+
26
+ options.each do |k, v|
27
+ unless VALID_OPTION_VALUES.include?(v.class)
28
+ raise ::PoroValidator::InvalidValidator.new(
29
+ "Invalid option: #{k} => #{v}"
30
+ )
31
+ end
32
+
33
+ unless matchers(k).call(value, v)
34
+ errors.add(attribute, message, k => v)
20
35
  end
21
36
  end
22
37
  end
23
38
 
24
39
  private
25
40
 
41
+ def validate_numeric_options(options_keys)
42
+ opts = options_keys - VALID_OPTIONS
43
+
44
+ return if opts.length <= 0
45
+
46
+ unless VALID_OPTIONS.include?(opts)
47
+ raise ::PoroValidator::InvalidValidator.new(
48
+ "Invalid options: #{opts.inspect} passed for numeric validator."
49
+ )
50
+ end
51
+ end
52
+
26
53
  def matchers(key)
27
- m = {
28
- extremum: lambda do |value, length|
54
+ {
55
+ extremum: proc do |value, length|
29
56
  value == length
30
57
  end,
31
58
 
32
- max: lambda do |value, length|
59
+ max: proc do |value, length|
33
60
  value <= length
34
61
  end,
35
62
 
36
- min: lambda do |value, length|
63
+ min: proc do |value, length|
37
64
  value >= length
38
65
  end,
39
66
 
40
- in: lambda do |value, length|
67
+ in: proc do |value, length|
41
68
  length.cover?(value)
42
69
  end
43
- }
44
- m.fetch(key, nil)
70
+ }[key] || nil
71
+ end
72
+
73
+ def is_numeric?(v, regex_match)
74
+ v =~ regex_match
45
75
  end
46
76
  end
47
77
  end
@@ -1,3 +1,3 @@
1
1
  module PoroValidator
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -13,30 +13,36 @@ RSpec.describe PoroValidator::Validators::FloatValidator do
13
13
  end.new
14
14
  end
15
15
 
16
- expect_validator_to_be_invalid do
17
- let(:entity) do
18
- OpenStruct.new(
19
- amount: 'aaa',
20
- rate: 'aaa'
21
- )
22
- end
16
+ [5, "500", -1, "+3,000", ".5",
17
+ "-300", "+400", "aaa"].each do |value|
18
+ expect_validator_to_be_invalid("value invalid: #{value}") do
19
+ let(:entity) do
20
+ OpenStruct.new(
21
+ amount: value,
22
+ rate: value
23
+ )
24
+ end
23
25
 
24
- let(:expected_errors) do
25
- {
26
- "amount" => ["is not a float"]
27
- }
28
- end
26
+ let(:expected_errors) do
27
+ {
28
+ "amount" => ["is not a float"]
29
+ }
30
+ end
29
31
 
30
- skip_attr_unmet_condition do
31
- let(:attr) { :rate }
32
+ skip_attr_unmet_condition do
33
+ let(:attr) { :rate }
34
+ end
32
35
  end
33
36
  end
34
37
 
35
- expect_validator_to_be_valid do
36
- let(:entity) do
37
- OpenStruct.new(
38
- amount: 5
39
- )
38
+ [5.00, "500.00", -1.00, "+3,000.000", 0.5, "0.5",
39
+ "-300.00", "+400.00"].each do |value|
40
+ expect_validator_to_be_valid("value value: #{value}") do
41
+ let(:entity) do
42
+ OpenStruct.new(
43
+ amount: value
44
+ )
45
+ end
40
46
  end
41
47
  end
42
48
  end
@@ -13,30 +13,34 @@ RSpec.describe PoroValidator::Validators::IntegerValidator do
13
13
  end.new
14
14
  end
15
15
 
16
- expect_validator_to_be_invalid do
17
- let(:entity) do
18
- OpenStruct.new(
19
- amount: "aaa",
20
- rate: "aaa"
21
- )
22
- end
16
+ ["aa", 5.00, "+500.00", -1.0, +30.00, "-300.00", "+400.00"].each do |value|
17
+ expect_validator_to_be_invalid("invalid value: #{value}") do
18
+ let(:entity) do
19
+ OpenStruct.new(
20
+ amount: value,
21
+ rate: value
22
+ )
23
+ end
23
24
 
24
- let(:expected_errors) do
25
- {
26
- "amount" => ["is not an integer"]
27
- }
28
- end
25
+ let(:expected_errors) do
26
+ {
27
+ "amount" => ["is not an integer"]
28
+ }
29
+ end
29
30
 
30
- skip_attr_unmet_condition do
31
- let(:attr) { :rate }
31
+ skip_attr_unmet_condition do
32
+ let(:attr) { :rate }
33
+ end
32
34
  end
33
35
  end
34
36
 
35
- expect_validator_to_be_valid do
36
- let(:entity) do
37
- OpenStruct.new(
38
- amount: 5
39
- )
37
+ [5, "500", -1, +3000000, "-300", "+400"].each do |value|
38
+ expect_validator_to_be_valid("valid value: #{value}") do
39
+ let(:entity) do
40
+ OpenStruct.new(
41
+ amount: value
42
+ )
43
+ end
40
44
  end
41
45
  end
42
46
  end
@@ -33,7 +33,7 @@ RSpec.describe PoroValidator::Validators::NumericValidator do
33
33
 
34
34
  let(:expected_errors) do
35
35
  {
36
- "section" => ["is not an integer"],
36
+ "section" => ["is not an integer or float type"],
37
37
  "amount" => ["does not match the numeric options: {:in=>1..10}"],
38
38
  "cap" => ["does not match the numeric options: {:extremum=>5}"],
39
39
  "bonus" => ["does not match the numeric options: {:max=>20}"],
@@ -64,5 +64,37 @@ RSpec.describe PoroValidator::Validators::NumericValidator do
64
64
  )
65
65
  end
66
66
  end
67
+
68
+ context "invalid option" do
69
+ subject(:validator) do
70
+ Class.new do
71
+ include PoroValidator.validator
72
+
73
+ validates :section, numeric: { mama: 1..10, in: 5..10 }
74
+ end.new
75
+ end
76
+
77
+ it "raises a ::PoroValidator::Exceptions" do
78
+ expect { subject.valid?({ section: 12 }) }.to raise_error(
79
+ ::PoroValidator::InvalidValidator
80
+ )
81
+ end
82
+ end
83
+
84
+ context "invalid option value" do
85
+ subject(:validator) do
86
+ Class.new do
87
+ include PoroValidator.validator
88
+
89
+ validates :section, numeric: { in: "a" }
90
+ end.new
91
+ end
92
+
93
+ it "raises a ::PoroValidator::Exceptions" do
94
+ expect { subject.valid?({ section: 12 }) }.to raise_error(
95
+ ::PoroValidator::InvalidValidator
96
+ )
97
+ end
98
+ end
67
99
  end
68
100
  end
@@ -12,8 +12,8 @@ module SpecHelpers
12
12
  end
13
13
 
14
14
  module ClassMethods
15
- def expect_validator_to_be_valid(&block)
16
- example_group = context("valid entity", caller: caller) do
15
+ def expect_validator_to_be_valid(desc = "valid entity", &block)
16
+ example_group = context(desc, caller: caller) do
17
17
  it "returns false for #valid?" do
18
18
  expect(validator.valid?(entity)).to be_truthy
19
19
  end
@@ -32,8 +32,8 @@ module SpecHelpers
32
32
  example_group.class_eval(&block) if block_given?
33
33
  end
34
34
 
35
- def expect_validator_to_be_invalid(&block)
36
- example_group = context("invalid entity", caller: caller) do
35
+ def expect_validator_to_be_invalid(desc = "invalid entity", &block)
36
+ example_group = context(desc, caller: caller) do
37
37
  it "returns false for #valid?" do
38
38
  expect(validator.valid?(entity)).to be_falsey
39
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: poro_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kareem Gan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-01-22 00:00:00.000000000 Z
11
+ date: 2016-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -198,7 +198,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
198
198
  version: '0'
199
199
  requirements: []
200
200
  rubyforge_project:
201
- rubygems_version: 2.4.5
201
+ rubygems_version: 2.4.5.1
202
202
  signing_key:
203
203
  specification_version: 4
204
204
  summary: A PORO (Plain Old Ruby Object) validator.