simple_validate 2.1.2 → 2.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 +4 -4
- data/.rspec_status +28 -8
- data/lib/simple_validate/validates_exclusion_of.rb +15 -3
- data/lib/simple_validate/validates_format_of.rb +10 -3
- data/lib/simple_validate/validates_inclusion_of.rb +15 -3
- data/lib/simple_validate/validates_length_of.rb +24 -33
- data/lib/simple_validate/validates_type_of.rb +7 -2
- data/lib/simple_validate/version.rb +1 -1
- data/lib/simple_validate.rb +0 -1
- metadata +1 -2
- data/lib/simple_validate/validates_set_base.rb +0 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 628e641750b3b4b70e0b5c1e9dfe71d96a328f21ecc84120384d49c8e9016e4f
|
4
|
+
data.tar.gz: dbbbaf33a3f1e2597a2e621ae571715d7b02c20556aadc38c36330d24d458662
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c27471e667d574519a90093a454a154908298f2a3667bac826122453582e1d3401e466401e10b7308e436f4e47c8b8de408fb69290cf77b2d1acb0658a0c752d
|
7
|
+
data.tar.gz: d4613f09b11db76d5c1406a012b8073ac384a9121f6fa99208ef809bffcfb5dabc60589b4fcf17d9d5070867e618eb18c34ad5427ec045d93d2300c76645300b
|
data/.rspec_status
CHANGED
@@ -1,8 +1,28 @@
|
|
1
|
-
example_id
|
2
|
-
|
3
|
-
./spec/simple_validate_spec.rb[1:1:1]
|
4
|
-
./spec/simple_validate_spec.rb[1:2:1]
|
5
|
-
./spec/
|
6
|
-
./spec/
|
7
|
-
./spec/
|
8
|
-
./spec/
|
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 |
|
@@ -1,11 +1,23 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module SimpleValidate
|
4
|
-
class ValidatesExclusionOf <
|
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
|
-
|
16
|
+
val = instance.send(attribute)
|
17
|
+
|
18
|
+
return true if val.nil? && @allow_nil == true
|
7
19
|
|
8
|
-
|
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(
|
10
|
-
|
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
|
-
!!(
|
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 <
|
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
|
-
|
16
|
+
val = instance.send(attribute)
|
17
|
+
|
18
|
+
return true if val.nil? && @allow_nil == true
|
7
19
|
|
8
|
-
set.include?(
|
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
|
-
|
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
|
-
|
13
|
-
proc { true })
|
14
|
-
self.options = options
|
15
|
-
end
|
8
|
+
@validator = options.keys.first
|
16
9
|
|
17
|
-
|
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
|
-
|
29
|
-
options
|
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
|
33
|
-
|
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
|
-
|
43
|
+
val = instance.send(attribute)
|
51
44
|
|
52
|
-
|
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 =
|
47
|
+
actual_length = val&.length
|
57
48
|
valid_length?(actual_length)
|
58
49
|
end
|
59
50
|
end
|
@@ -5,6 +5,7 @@ module SimpleValidate
|
|
5
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)
|
@@ -14,11 +15,15 @@ module SimpleValidate
|
|
14
15
|
end
|
15
16
|
|
16
17
|
def valid?(instance)
|
18
|
+
val = instance.send(attribute)
|
19
|
+
|
20
|
+
return true if val.nil? && @allow_nil == true
|
21
|
+
|
17
22
|
if @type == :boolean
|
18
|
-
[true, false].include?
|
23
|
+
[true, false].include? val
|
19
24
|
else
|
20
25
|
klass = Utils.classify(@type)
|
21
|
-
|
26
|
+
val.is_a?(klass)
|
22
27
|
end
|
23
28
|
end
|
24
29
|
end
|
data/lib/simple_validate.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_validate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Palaniuk
|
@@ -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
|