simple_validate 2.0.2 → 2.2.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
  SHA256:
3
- metadata.gz: 633fbb10accca5c33bc717619ef5392f40f53ffea6ccf4dc9e62fa19f934287c
4
- data.tar.gz: 3a93d9d8e9e9fb896dd150f7137893694339c005b87953f5c2c5d6b6e5f03a90
3
+ metadata.gz: 628e641750b3b4b70e0b5c1e9dfe71d96a328f21ecc84120384d49c8e9016e4f
4
+ data.tar.gz: dbbbaf33a3f1e2597a2e621ae571715d7b02c20556aadc38c36330d24d458662
5
5
  SHA512:
6
- metadata.gz: '070947710fb4ceecf88974d5126073b8f02b37af1cd185d02e6019281934836655733b6f622bc2ac47f4dba8f5c68eac1c8f8ba4d27f0f31f25d6a01234bc35c'
7
- data.tar.gz: 81ecd04c13e0ef128b16aa7fbce0c845e86dabcc259f9c61f5ad066058359c5a6ca4867360cfa9e65d86ab699975a1554c01fad7ad31970278ca2ea51a7c7285
6
+ metadata.gz: c27471e667d574519a90093a454a154908298f2a3667bac826122453582e1d3401e466401e10b7308e436f4e47c8b8de408fb69290cf77b2d1acb0658a0c752d
7
+ data.tar.gz: d4613f09b11db76d5c1406a012b8073ac384a9121f6fa99208ef809bffcfb5dabc60589b4fcf17d9d5070867e618eb18c34ad5427ec045d93d2300c76645300b
data/.rspec_status CHANGED
@@ -1,8 +1,28 @@
1
- example_id | status | run_time |
2
- ------------------------------------- | ------ | --------------- |
3
- ./spec/simple_validate_spec.rb[1:1:1] | passed | 0.00127 seconds |
4
- ./spec/simple_validate_spec.rb[1:2:1] | passed | 0.00012 seconds |
5
- ./spec/simple_validate_spec.rb[1:3:1] | passed | 0.00104 seconds |
6
- ./spec/simple_validate_spec.rb[1:4:1] | passed | 0.00009 seconds |
7
- ./spec/simple_validate_spec.rb[1:5:1] | passed | 0.00006 seconds |
8
- ./spec/simple_validate_spec.rb[1:6:1] | passed | 0.00005 seconds |
1
+ example_id | status | run_time |
2
+ -------------------------------------------- | ------ | --------------- |
3
+ ./spec/simple_validate_spec.rb[1:1:1] | passed | 0.00128 seconds |
4
+ ./spec/simple_validate_spec.rb[1:2:1] | passed | 0.00008 seconds |
5
+ ./spec/validators/exclusion_spec.rb[1:1:1] | passed | 0.00005 seconds |
6
+ ./spec/validators/exclusion_spec.rb[1:2:1:1] | passed | 0.00006 seconds |
7
+ ./spec/validators/exclusion_spec.rb[1:2:2:1] | passed | 0.00005 seconds |
8
+ ./spec/validators/inclusion_spec.rb[1:1:1] | passed | 0.00004 seconds |
9
+ ./spec/validators/inclusion_spec.rb[1:2:1:1] | passed | 0.00005 seconds |
10
+ ./spec/validators/inclusion_spec.rb[1:2:2:1] | passed | 0.00005 seconds |
11
+ ./spec/validators/length_spec.rb[1:1:1] | passed | 0.00007 seconds |
12
+ ./spec/validators/length_spec.rb[1:2:1] | passed | 0.00007 seconds |
13
+ ./spec/validators/length_spec.rb[1:3:1] | passed | 0.00007 seconds |
14
+ ./spec/validators/length_spec.rb[1:4:1:1] | passed | 0.00008 seconds |
15
+ ./spec/validators/length_spec.rb[1:4:2:1] | passed | 0.00007 seconds |
16
+ ./spec/validators/length_spec.rb[1:5:1:1] | passed | 0.00005 seconds |
17
+ ./spec/validators/length_spec.rb[1:5:2:1] | passed | 0.00006 seconds |
18
+ ./spec/validators/length_spec.rb[1:6:1] | passed | 0.00005 seconds |
19
+ ./spec/validators/presence_spec.rb[1:1:1] | passed | 0.00006 seconds |
20
+ ./spec/validators/type_spec.rb[1:1:1] | passed | 0.00005 seconds |
21
+ ./spec/validators/type_spec.rb[1:2:1:1] | passed | 0.00006 seconds |
22
+ ./spec/validators/type_spec.rb[1:2:2:1] | passed | 0.00006 seconds |
23
+ ./spec/validators/type_spec.rb[1:3:1:1] | passed | 0.00005 seconds |
24
+ ./spec/validators/type_spec.rb[1:3:2:1] | passed | 0.00006 seconds |
25
+ ./spec/validators/type_spec.rb[1:4:1:1] | passed | 0.00006 seconds |
26
+ ./spec/validators/type_spec.rb[1:4:2:1] | passed | 0.00006 seconds |
27
+ ./spec/validators/type_spec.rb[1:5:1:1] | passed | 0.00005 seconds |
28
+ ./spec/validators/type_spec.rb[1:5:2:1] | passed | 0.00006 seconds |
data/README.md CHANGED
@@ -18,7 +18,7 @@ class Person
18
18
  attr_accessor :name, :age
19
19
 
20
20
  validates_presence_of :name, :age
21
- validates_numericality_of :age
21
+ validates_type_of :age, as: :integer
22
22
  end
23
23
  ```
24
24
 
@@ -30,7 +30,7 @@ end
30
30
  => p.errors
31
31
  => #<SimpleValidate::Errors:0x007f94318b4df0
32
32
  @messages=
33
- {:age=>["can't be empty", "must be a number"],
33
+ {:age=>["can't be empty", "must be an integer"],
34
34
  :name=>["can't be empty"]}>
35
35
  ```
36
36
 
@@ -1,11 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SimpleValidate
4
- class ValidatesExclusionOf < ValidatesSetBase
4
+ class ValidatesExclusionOf < ValidatesBase
5
+ def initialize(attribute, options)
6
+ @set = options[:in]
7
+ @allow_nil = options[:allow_nil]
8
+
9
+ raise ArgumentError unless [Array, Range].include?(@set.class)
10
+
11
+ super(attribute, options[:message] ||
12
+ "breaks exclusion rules", options[:if] || proc { true })
13
+ end
14
+
5
15
  def valid?(instance)
6
- raise ArgumentError if set.empty? || !options.fetch(:in).is_a?(Array)
16
+ val = instance.send(attribute)
17
+
18
+ return true if val.nil? && @allow_nil == true
7
19
 
8
- !set.include?(instance.send(attribute).to_s)
20
+ !@set.to_a.include?(val)
9
21
  end
10
22
  end
11
23
  end
@@ -5,15 +5,22 @@ module SimpleValidate
5
5
  attr_accessor :regex
6
6
 
7
7
  def initialize(attribute, options)
8
+ @allow_nil = options[:allow_nil]
8
9
  @regex = options[:with]
9
- super(attribute, options[:message] ||
10
- "is incorrect format", options[:if] || proc { true })
10
+ super(
11
+ attribute,
12
+ options[:message] || "is incorrect format",
13
+ options[:if] || proc { true }
14
+ )
11
15
  end
12
16
 
13
17
  def valid?(instance)
18
+ val = instance.send(attribute)
19
+
20
+ return true if val.nil? && @allow_nil == true
14
21
  raise ArgumentError if regex.nil? || !regex.is_a?(Regexp)
15
22
 
16
- !!(instance.send(attribute) =~ regex)
23
+ !!(val =~ regex)
17
24
  end
18
25
  end
19
26
  end
@@ -1,11 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SimpleValidate
4
- class ValidatesInclusionOf < ValidatesSetBase
4
+ class ValidatesInclusionOf < ValidatesBase
5
+ def initialize(attribute, options)
6
+ @set = options[:in]
7
+ @allow_nil = options[:allow_nil]
8
+
9
+ raise ArgumentError unless [Array, Range].include?(@set.class)
10
+
11
+ super(attribute, options[:message] ||
12
+ "breaks inclusion rules", options[:if] || proc { true })
13
+ end
14
+
5
15
  def valid?(instance)
6
- raise ArgumentError if set.empty? || !options.fetch(:in).is_a?(Array)
16
+ val = instance.send(attribute)
17
+
18
+ return true if val.nil? && @allow_nil == true
7
19
 
8
- set.include?(instance.send(attribute).to_s)
20
+ @set.to_a.include?(val)
9
21
  end
10
22
  end
11
23
  end
@@ -2,58 +2,49 @@
2
2
 
3
3
  module SimpleValidate
4
4
  class ValidatesLengthOf < ValidatesBase
5
- class InvalidLengthOption < ArgumentError; end
6
- attr_reader :attribute
7
- attr_accessor :options
8
-
9
- VALID_LENGTH_OPTIONS = %i[maximum minimum in is].freeze
5
+ VALIDATORS = %i[maximum minimum in is].freeze
10
6
 
11
7
  def initialize(attribute, options)
12
- super(attribute, options.delete(:message), options.delete(:if) ||
13
- proc { true })
14
- self.options = options
15
- end
8
+ @validator = options.keys.first
16
9
 
17
- def message
18
- @message ||= case validator
19
- when :minimum
20
- "is too short"
21
- when :maximum
22
- "is too long"
23
- else
24
- "is not the correct length"
25
- end
26
- end
10
+ raise ArgumentError, "Invalid length option" unless VALIDATORS.include?(@validator)
27
11
 
28
- def validator
29
- options.keys.first
12
+ @valid_length = options[@validator]
13
+ @allow_nil = options[:allow_nil]
14
+
15
+ super(attribute, options[:message], options[:if] || proc { true })
30
16
  end
31
17
 
32
- def valid_length
33
- options.values.first
18
+ def message
19
+ @message = case @validator
20
+ when :minimum
21
+ "is too short"
22
+ when :maximum
23
+ "is too long"
24
+ else
25
+ "is not valid length"
26
+ end
34
27
  end
35
28
 
36
29
  def valid_length?(actual_length)
37
- case validator
30
+ case @validator
38
31
  when :minimum
39
- actual_length >= valid_length
32
+ actual_length >= @valid_length
40
33
  when :maximum
41
- actual_length <= valid_length
34
+ actual_length <= @valid_length
42
35
  when :in
43
- valid_length.member?(actual_length)
36
+ @valid_length.member?(actual_length)
44
37
  when :is
45
- actual_length == valid_length
38
+ actual_length == @valid_length
46
39
  end
47
40
  end
48
41
 
49
42
  def valid?(instance)
50
- raise ArgumentError, "Only one length argument can be provided" if options.keys.size > 1
43
+ val = instance.send(attribute)
51
44
 
52
- unless VALID_LENGTH_OPTIONS.include?(options.keys.first)
53
- raise InvalidLengthOption, "Invalid length option given #{options.keys}"
54
- end
45
+ return true if val.nil? && @allow_nil == true
55
46
 
56
- actual_length = instance.send(attribute)&.length
47
+ actual_length = val&.length
57
48
  valid_length?(actual_length)
58
49
  end
59
50
  end
@@ -2,21 +2,29 @@
2
2
 
3
3
  module SimpleValidate
4
4
  class ValidatesTypeOf < ValidatesBase
5
- SUPPORTED_TYPES = %i[string integer float].freeze
5
+ SUPPORTED_TYPES = %i[string integer float boolean].freeze
6
6
 
7
7
  def initialize(attribute, options)
8
+ @allow_nil = options[:allow_nil]
8
9
  @type = options[:as]
9
10
 
10
11
  raise ArgumentError unless @type && SUPPORTED_TYPES.include?(@type)
11
12
 
12
- @klass = Utils.classify(options[:as])
13
-
14
13
  super(attribute, options[:message] ||
15
14
  "must be #{Utils.article(@type)} #{@type}", options[:if] || proc { true })
16
15
  end
17
16
 
18
17
  def valid?(instance)
19
- instance.send(attribute).is_a?(@klass)
18
+ val = instance.send(attribute)
19
+
20
+ return true if val.nil? && @allow_nil == true
21
+
22
+ if @type == :boolean
23
+ [true, false].include? val
24
+ else
25
+ klass = Utils.classify(@type)
26
+ val.is_a?(klass)
27
+ end
20
28
  end
21
29
  end
22
30
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SimpleValidate
4
- VERSION = "2.0.2"
4
+ VERSION = "2.2.2"
5
5
  end
@@ -5,7 +5,6 @@
5
5
  format_of
6
6
  type_of
7
7
  length_of
8
- set_base
9
8
  inclusion_of
10
9
  exclusion_of].each do |validation|
11
10
  require "simple_validate/validates_#{validation}"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_validate
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Palaniuk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-28 00:00:00.000000000 Z
11
+ date: 2023-08-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: PORO validation mixin with no deps
14
14
  email:
@@ -36,7 +36,6 @@ files:
36
36
  - lib/simple_validate/validates_inclusion_of.rb
37
37
  - lib/simple_validate/validates_length_of.rb
38
38
  - lib/simple_validate/validates_presence_of.rb
39
- - lib/simple_validate/validates_set_base.rb
40
39
  - lib/simple_validate/validates_type_of.rb
41
40
  - lib/simple_validate/version.rb
42
41
  - simple_validate.gemspec
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SimpleValidate
4
- class ValidatesSetBase < ValidatesBase
5
- attr_accessor :set, :options
6
-
7
- def initialize(attribute, options)
8
- self.options = options
9
- self.set = Set.new(Array(options[:in]).map(&:to_s))
10
- super(attribute, options[:message] ||
11
- "breaks inclusion/exclusion rules", options[:if] || proc { true })
12
- end
13
- end
14
- end