hash_validator 2.0.0 → 2.0.1
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/.github/workflows/ruby.yml +6 -1
- data/.rubocop.yml +340 -0
- data/Gemfile +3 -1
- data/README.md +2 -1
- data/Rakefile +6 -4
- data/hash_validator.gemspec +11 -5
- data/lib/hash_validator/base.rb +2 -0
- data/lib/hash_validator/configuration.rb +5 -3
- data/lib/hash_validator/validations/many.rb +2 -0
- data/lib/hash_validator/validations/multiple.rb +2 -0
- data/lib/hash_validator/validations/optional.rb +2 -0
- data/lib/hash_validator/validations.rb +5 -3
- data/lib/hash_validator/validators/alpha_validator.rb +5 -3
- data/lib/hash_validator/validators/alphanumeric_validator.rb +5 -3
- data/lib/hash_validator/validators/array_validator.rb +44 -48
- data/lib/hash_validator/validators/base.rb +6 -4
- data/lib/hash_validator/validators/boolean_validator.rb +3 -1
- data/lib/hash_validator/validators/class_validator.rb +3 -1
- data/lib/hash_validator/validators/digits_validator.rb +5 -3
- data/lib/hash_validator/validators/dynamic_func_validator.rb +8 -8
- data/lib/hash_validator/validators/dynamic_pattern_validator.rb +5 -3
- data/lib/hash_validator/validators/email_validator.rb +4 -2
- data/lib/hash_validator/validators/enumerable_validator.rb +4 -2
- data/lib/hash_validator/validators/hash_validator.rb +5 -3
- data/lib/hash_validator/validators/hex_color_validator.rb +5 -3
- data/lib/hash_validator/validators/ip_validator.rb +6 -4
- data/lib/hash_validator/validators/ipv4_validator.rb +5 -3
- data/lib/hash_validator/validators/ipv6_validator.rb +6 -4
- data/lib/hash_validator/validators/json_validator.rb +6 -4
- data/lib/hash_validator/validators/lambda_validator.rb +4 -2
- data/lib/hash_validator/validators/many_validator.rb +3 -1
- data/lib/hash_validator/validators/multiple_validator.rb +4 -2
- data/lib/hash_validator/validators/optional_validator.rb +3 -1
- data/lib/hash_validator/validators/presence_validator.rb +4 -2
- data/lib/hash_validator/validators/regex_validator.rb +4 -2
- data/lib/hash_validator/validators/simple_type_validators.rb +3 -1
- data/lib/hash_validator/validators/simple_validator.rb +2 -0
- data/lib/hash_validator/validators/url_validator.rb +6 -4
- data/lib/hash_validator/validators.rb +35 -33
- data/lib/hash_validator/version.rb +3 -1
- data/lib/hash_validator.rb +7 -5
- data/spec/configuration_spec.rb +76 -74
- data/spec/hash_validator_spec.rb +132 -113
- data/spec/hash_validator_spec_helper.rb +2 -0
- data/spec/spec_helper.rb +14 -4
- data/spec/validators/alpha_validator_spec.rb +43 -41
- data/spec/validators/alphanumeric_validator_spec.rb +44 -42
- data/spec/validators/array_spec.rb +102 -47
- data/spec/validators/base_spec.rb +25 -10
- data/spec/validators/boolean_spec.rb +15 -13
- data/spec/validators/class_spec.rb +20 -18
- data/spec/validators/digits_validator_spec.rb +46 -44
- data/spec/validators/dynamic_func_validator_spec.rb +90 -88
- data/spec/validators/dynamic_pattern_validator_spec.rb +65 -63
- data/spec/validators/email_spec.rb +15 -13
- data/spec/validators/hash_validator_spec.rb +39 -37
- data/spec/validators/hex_color_validator_spec.rb +49 -47
- data/spec/validators/in_enumerable_spec.rb +32 -30
- data/spec/validators/ip_validator_spec.rb +46 -44
- data/spec/validators/ipv4_validator_spec.rb +45 -43
- data/spec/validators/ipv6_validator_spec.rb +44 -42
- data/spec/validators/json_validator_spec.rb +38 -36
- data/spec/validators/lambda_spec.rb +20 -18
- data/spec/validators/many_spec.rb +25 -23
- data/spec/validators/multiple_spec.rb +13 -11
- data/spec/validators/optional_spec.rb +22 -20
- data/spec/validators/presence_spec.rb +16 -14
- data/spec/validators/regexp_spec.rb +14 -12
- data/spec/validators/simple_spec.rb +17 -15
- data/spec/validators/simple_types_spec.rb +21 -19
- data/spec/validators/url_validator_spec.rb +34 -32
- data/spec/validators/user_defined_spec.rb +29 -27
- metadata +59 -2
@@ -1,57 +1,59 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe "Simple validator types" do
|
4
6
|
let(:errors) { Hash.new }
|
5
7
|
|
6
8
|
# Simple types
|
7
9
|
{
|
8
10
|
array: {
|
9
|
-
valid: [ [], [1], [
|
10
|
-
invalid: [ nil,
|
11
|
+
valid: [ [], [1], ["foo"], [1, ["foo"], Time.now] ],
|
12
|
+
invalid: [ nil, "", 123, "123", Time.now, "[1]" ]
|
11
13
|
},
|
12
14
|
complex: {
|
13
|
-
valid: [ Complex(1), Complex(2, 3), Complex(
|
14
|
-
invalid: [ nil,
|
15
|
+
valid: [ Complex(1), Complex(2, 3), Complex("2/3+3/4i"), 0.3.to_c ],
|
16
|
+
invalid: [ nil, "", 123, "123", Time.now, "[1]", [1], "2/3+3/4i", Rational(2, 3) ]
|
15
17
|
},
|
16
18
|
enumerable: {
|
17
19
|
valid: [ [], [ 1, 2, 3 ], 1..Float::INFINITY ],
|
18
|
-
invalid: [ nil,
|
20
|
+
invalid: [ nil, "", 123, "123", Time.now, Float::INFINITY ]
|
19
21
|
},
|
20
22
|
float: {
|
21
23
|
valid: [ 0.0, 1.1, 1.23, Float::INFINITY, Float::EPSILON ],
|
22
|
-
invalid: [ nil,
|
24
|
+
invalid: [ nil, "", 0, 123, "123", Time.now, "[1]", "2013-03-04" ]
|
23
25
|
},
|
24
26
|
integer: {
|
25
27
|
valid: [ 0, -1000000, 1000000 ],
|
26
|
-
invalid: [ nil,
|
28
|
+
invalid: [ nil, "", 1.1, "123", Time.now, "[1]", "2013-03-04" ]
|
27
29
|
},
|
28
30
|
numeric: {
|
29
31
|
valid: [ 0, 123, 123.45 ],
|
30
|
-
invalid: [ nil,
|
32
|
+
invalid: [ nil, "", "123", Time.now ]
|
31
33
|
},
|
32
34
|
range: {
|
33
|
-
valid: [ 0..10,
|
34
|
-
invalid: [ nil,
|
35
|
+
valid: [ 0..10, "a".."z", 5..0 ],
|
36
|
+
invalid: [ nil, "", "123", Time.now ]
|
35
37
|
},
|
36
38
|
rational: {
|
37
39
|
valid: [ Rational(1), Rational(2, 3), 3.to_r ],
|
38
|
-
invalid: [ nil,
|
40
|
+
invalid: [ nil, "", 123, "123", Time.now, "[1]", [1], Complex(2, 3) ]
|
39
41
|
},
|
40
42
|
regexp: {
|
41
|
-
valid: [ /[a-z]+/, //, //i, Regexp.new(
|
42
|
-
invalid: [ nil,
|
43
|
+
valid: [ /[a-z]+/, //, //i, Regexp.new(".*") ],
|
44
|
+
invalid: [ nil, "", 123, "123", Time.now, ".*" ]
|
43
45
|
},
|
44
46
|
string: {
|
45
|
-
valid: [
|
47
|
+
valid: [ "", "Hello World", "12345" ],
|
46
48
|
invalid: [ nil, 12345, Time.now ]
|
47
49
|
},
|
48
50
|
symbol: {
|
49
|
-
valid: [ :foo, :'',
|
50
|
-
invalid: [ nil,
|
51
|
+
valid: [ :foo, :'', "bar".to_sym ],
|
52
|
+
invalid: [ nil, "", 1.1, "123", Time.now, "[1]", "2013-03-04" ]
|
51
53
|
},
|
52
54
|
time: {
|
53
55
|
valid: [ Time.now ],
|
54
|
-
invalid: [ nil,
|
56
|
+
invalid: [ nil, "", 123, "123", "#{Time.now}" ]
|
55
57
|
}
|
56
58
|
}.each do |type, data|
|
57
59
|
describe type do
|
@@ -1,75 +1,77 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spec_helper"
|
2
4
|
|
3
5
|
describe HashValidator::Validator::UrlValidator do
|
4
6
|
let(:validator) { HashValidator::Validator::UrlValidator.new }
|
5
7
|
|
6
|
-
context
|
7
|
-
it
|
8
|
+
context "valid URLs" do
|
9
|
+
it "validates http URLs" do
|
8
10
|
errors = {}
|
9
|
-
validator.validate(
|
11
|
+
validator.validate("key", "http://example.com", {}, errors)
|
10
12
|
expect(errors).to be_empty
|
11
13
|
end
|
12
14
|
|
13
|
-
it
|
15
|
+
it "validates https URLs" do
|
14
16
|
errors = {}
|
15
|
-
validator.validate(
|
17
|
+
validator.validate("key", "https://example.com", {}, errors)
|
16
18
|
expect(errors).to be_empty
|
17
19
|
end
|
18
20
|
|
19
|
-
it
|
21
|
+
it "validates ftp URLs" do
|
20
22
|
errors = {}
|
21
|
-
validator.validate(
|
23
|
+
validator.validate("key", "ftp://ftp.example.com", {}, errors)
|
22
24
|
expect(errors).to be_empty
|
23
25
|
end
|
24
26
|
|
25
|
-
it
|
27
|
+
it "validates URLs with paths" do
|
26
28
|
errors = {}
|
27
|
-
validator.validate(
|
29
|
+
validator.validate("key", "https://example.com/path/to/resource", {}, errors)
|
28
30
|
expect(errors).to be_empty
|
29
31
|
end
|
30
32
|
|
31
|
-
it
|
33
|
+
it "validates URLs with query parameters" do
|
32
34
|
errors = {}
|
33
|
-
validator.validate(
|
35
|
+
validator.validate("key", "https://example.com/search?q=test&page=1", {}, errors)
|
34
36
|
expect(errors).to be_empty
|
35
37
|
end
|
36
38
|
end
|
37
39
|
|
38
|
-
context
|
39
|
-
it
|
40
|
+
context "invalid URLs" do
|
41
|
+
it "rejects non-string values" do
|
40
42
|
errors = {}
|
41
|
-
validator.validate(
|
42
|
-
expect(errors[
|
43
|
+
validator.validate("key", 123, {}, errors)
|
44
|
+
expect(errors["key"]).to eq("is not a valid URL")
|
43
45
|
end
|
44
46
|
|
45
|
-
it
|
47
|
+
it "rejects nil values" do
|
46
48
|
errors = {}
|
47
|
-
validator.validate(
|
48
|
-
expect(errors[
|
49
|
+
validator.validate("key", nil, {}, errors)
|
50
|
+
expect(errors["key"]).to eq("is not a valid URL")
|
49
51
|
end
|
50
52
|
|
51
|
-
it
|
53
|
+
it "rejects malformed URLs" do
|
52
54
|
errors = {}
|
53
|
-
validator.validate(
|
54
|
-
expect(errors[
|
55
|
+
validator.validate("key", "not a url", {}, errors)
|
56
|
+
expect(errors["key"]).to eq("is not a valid URL")
|
55
57
|
end
|
56
58
|
|
57
|
-
it
|
59
|
+
it "rejects URLs without schemes" do
|
58
60
|
errors = {}
|
59
|
-
validator.validate(
|
60
|
-
expect(errors[
|
61
|
+
validator.validate("key", "example.com", {}, errors)
|
62
|
+
expect(errors["key"]).to eq("is not a valid URL")
|
61
63
|
end
|
62
64
|
|
63
|
-
it
|
65
|
+
it "rejects unsupported schemes" do
|
64
66
|
errors = {}
|
65
|
-
validator.validate(
|
66
|
-
expect(errors[
|
67
|
+
validator.validate("key", "mailto:test@example.com", {}, errors)
|
68
|
+
expect(errors["key"]).to eq("is not a valid URL")
|
67
69
|
end
|
68
70
|
|
69
|
-
it
|
71
|
+
it "rejects empty strings" do
|
70
72
|
errors = {}
|
71
|
-
validator.validate(
|
72
|
-
expect(errors[
|
73
|
+
validator.validate("key", "", {}, errors)
|
74
|
+
expect(errors["key"]).to eq("is not a valid URL")
|
73
75
|
end
|
74
76
|
end
|
75
|
-
end
|
77
|
+
end
|
@@ -1,12 +1,14 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe "User-defined validator" do
|
4
6
|
let(:validator) do
|
5
7
|
unless defined?(HashValidator::Validator::OddValidator)
|
6
8
|
# Define our custom validator
|
7
9
|
class HashValidator::Validator::OddValidator < HashValidator::Validator::Base
|
8
10
|
def initialize
|
9
|
-
super(
|
11
|
+
super("odd")
|
10
12
|
end
|
11
13
|
|
12
14
|
def validate(key, value, validations, errors)
|
@@ -23,73 +25,73 @@ describe 'User-defined validator' do
|
|
23
25
|
let(:errors) { Hash.new }
|
24
26
|
|
25
27
|
|
26
|
-
describe
|
28
|
+
describe "#should_validate?" do
|
27
29
|
it 'validates the name "odd"' do
|
28
|
-
expect(validator.should_validate?(
|
30
|
+
expect(validator.should_validate?("odd")).to eq true
|
29
31
|
end
|
30
32
|
|
31
|
-
it
|
32
|
-
expect(validator.should_validate?(
|
33
|
-
expect(validator.should_validate?(
|
33
|
+
it "does not validate other names" do
|
34
|
+
expect(validator.should_validate?("string")).to eq false
|
35
|
+
expect(validator.should_validate?("array")).to eq false
|
34
36
|
expect(validator.should_validate?(nil)).to eq false
|
35
37
|
end
|
36
38
|
end
|
37
39
|
|
38
|
-
describe
|
39
|
-
it
|
40
|
+
describe "#validate" do
|
41
|
+
it "validates odd integers with true" do
|
40
42
|
validator.validate(:key, 1, {}, errors)
|
41
43
|
|
42
44
|
expect(errors).to be_empty
|
43
45
|
end
|
44
46
|
|
45
|
-
it
|
47
|
+
it "validates even integers with errrors" do
|
46
48
|
validator.validate(:key, 2, {}, errors)
|
47
49
|
|
48
50
|
expect(errors).not_to be_empty
|
49
|
-
expect(errors).to eq({ key:
|
51
|
+
expect(errors).to eq({ key: "odd required" })
|
50
52
|
end
|
51
53
|
|
52
|
-
it
|
54
|
+
it "validates even floats with errrors" do
|
53
55
|
validator.validate(:key, 2.0, {}, errors)
|
54
56
|
|
55
57
|
expect(errors).not_to be_empty
|
56
|
-
expect(errors).to eq({ key:
|
58
|
+
expect(errors).to eq({ key: "odd required" })
|
57
59
|
end
|
58
60
|
|
59
|
-
it
|
61
|
+
it "validates odd floats with errrors" do
|
60
62
|
validator.validate(:key, 1.0, {}, errors)
|
61
63
|
|
62
64
|
expect(errors).not_to be_empty
|
63
|
-
expect(errors).to eq({ key:
|
65
|
+
expect(errors).to eq({ key: "odd required" })
|
64
66
|
end
|
65
67
|
|
66
|
-
it
|
67
|
-
validator.validate(:key,
|
68
|
+
it "validates an odd integer string with errrors" do
|
69
|
+
validator.validate(:key, "1", {}, errors)
|
68
70
|
|
69
71
|
expect(errors).not_to be_empty
|
70
|
-
expect(errors).to eq({ key:
|
72
|
+
expect(errors).to eq({ key: "odd required" })
|
71
73
|
end
|
72
74
|
end
|
73
75
|
|
74
|
-
describe
|
76
|
+
describe "Integrating with the entire validation system" do
|
75
77
|
before { HashValidator.add_validator(validator) rescue nil } # rescue to prevent: validators need to have unique names
|
76
78
|
|
77
|
-
it
|
78
|
-
expect(HashValidator.validator_for(
|
79
|
+
it "can be looked up using #validator_for" do
|
80
|
+
expect(HashValidator.validator_for("odd")).to be_a_kind_of(HashValidator::Validator::OddValidator)
|
79
81
|
end
|
80
82
|
|
81
|
-
it
|
82
|
-
validator = HashValidator.validate({ age: 27 }, { age:
|
83
|
+
it "can be used in validations - test 1" do
|
84
|
+
validator = HashValidator.validate({ age: 27 }, { age: "odd" })
|
83
85
|
|
84
86
|
expect(validator.valid?).to eq true
|
85
87
|
expect(validator.errors).to be_empty
|
86
88
|
end
|
87
89
|
|
88
|
-
it
|
89
|
-
validator = HashValidator.validate({ age: 40 }, { age:
|
90
|
+
it "can be used in validations - test 2" do
|
91
|
+
validator = HashValidator.validate({ age: 40 }, { age: "odd" })
|
90
92
|
|
91
93
|
expect(validator.valid?).to eq false
|
92
|
-
expect(validator.errors).to eq({ age:
|
94
|
+
expect(validator.errors).to eq({ age: "odd required" })
|
93
95
|
end
|
94
96
|
end
|
95
97
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Brooks
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-08-
|
11
|
+
date: 2025-08-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,62 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.10'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.22.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.22.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.21'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.21'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop-performance
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1.25'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.25'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop-rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '3.6'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '3.6'
|
55
111
|
description: Ruby library to validate hashes (Hash) against user-defined requirements
|
56
112
|
email:
|
57
113
|
- james@gooddogdesign.com
|
@@ -62,6 +118,7 @@ files:
|
|
62
118
|
- ".github/workflows/ruby.yml"
|
63
119
|
- ".gitignore"
|
64
120
|
- ".rspec"
|
121
|
+
- ".rubocop.yml"
|
65
122
|
- ".ruby-version"
|
66
123
|
- Gemfile
|
67
124
|
- LICENSE.txt
|