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.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +6 -1
  3. data/.rubocop.yml +340 -0
  4. data/Gemfile +3 -1
  5. data/README.md +2 -1
  6. data/Rakefile +6 -4
  7. data/hash_validator.gemspec +11 -5
  8. data/lib/hash_validator/base.rb +2 -0
  9. data/lib/hash_validator/configuration.rb +5 -3
  10. data/lib/hash_validator/validations/many.rb +2 -0
  11. data/lib/hash_validator/validations/multiple.rb +2 -0
  12. data/lib/hash_validator/validations/optional.rb +2 -0
  13. data/lib/hash_validator/validations.rb +5 -3
  14. data/lib/hash_validator/validators/alpha_validator.rb +5 -3
  15. data/lib/hash_validator/validators/alphanumeric_validator.rb +5 -3
  16. data/lib/hash_validator/validators/array_validator.rb +44 -48
  17. data/lib/hash_validator/validators/base.rb +6 -4
  18. data/lib/hash_validator/validators/boolean_validator.rb +3 -1
  19. data/lib/hash_validator/validators/class_validator.rb +3 -1
  20. data/lib/hash_validator/validators/digits_validator.rb +5 -3
  21. data/lib/hash_validator/validators/dynamic_func_validator.rb +8 -8
  22. data/lib/hash_validator/validators/dynamic_pattern_validator.rb +5 -3
  23. data/lib/hash_validator/validators/email_validator.rb +4 -2
  24. data/lib/hash_validator/validators/enumerable_validator.rb +4 -2
  25. data/lib/hash_validator/validators/hash_validator.rb +5 -3
  26. data/lib/hash_validator/validators/hex_color_validator.rb +5 -3
  27. data/lib/hash_validator/validators/ip_validator.rb +6 -4
  28. data/lib/hash_validator/validators/ipv4_validator.rb +5 -3
  29. data/lib/hash_validator/validators/ipv6_validator.rb +6 -4
  30. data/lib/hash_validator/validators/json_validator.rb +6 -4
  31. data/lib/hash_validator/validators/lambda_validator.rb +4 -2
  32. data/lib/hash_validator/validators/many_validator.rb +3 -1
  33. data/lib/hash_validator/validators/multiple_validator.rb +4 -2
  34. data/lib/hash_validator/validators/optional_validator.rb +3 -1
  35. data/lib/hash_validator/validators/presence_validator.rb +4 -2
  36. data/lib/hash_validator/validators/regex_validator.rb +4 -2
  37. data/lib/hash_validator/validators/simple_type_validators.rb +3 -1
  38. data/lib/hash_validator/validators/simple_validator.rb +2 -0
  39. data/lib/hash_validator/validators/url_validator.rb +6 -4
  40. data/lib/hash_validator/validators.rb +35 -33
  41. data/lib/hash_validator/version.rb +3 -1
  42. data/lib/hash_validator.rb +7 -5
  43. data/spec/configuration_spec.rb +76 -74
  44. data/spec/hash_validator_spec.rb +132 -113
  45. data/spec/hash_validator_spec_helper.rb +2 -0
  46. data/spec/spec_helper.rb +14 -4
  47. data/spec/validators/alpha_validator_spec.rb +43 -41
  48. data/spec/validators/alphanumeric_validator_spec.rb +44 -42
  49. data/spec/validators/array_spec.rb +102 -47
  50. data/spec/validators/base_spec.rb +25 -10
  51. data/spec/validators/boolean_spec.rb +15 -13
  52. data/spec/validators/class_spec.rb +20 -18
  53. data/spec/validators/digits_validator_spec.rb +46 -44
  54. data/spec/validators/dynamic_func_validator_spec.rb +90 -88
  55. data/spec/validators/dynamic_pattern_validator_spec.rb +65 -63
  56. data/spec/validators/email_spec.rb +15 -13
  57. data/spec/validators/hash_validator_spec.rb +39 -37
  58. data/spec/validators/hex_color_validator_spec.rb +49 -47
  59. data/spec/validators/in_enumerable_spec.rb +32 -30
  60. data/spec/validators/ip_validator_spec.rb +46 -44
  61. data/spec/validators/ipv4_validator_spec.rb +45 -43
  62. data/spec/validators/ipv6_validator_spec.rb +44 -42
  63. data/spec/validators/json_validator_spec.rb +38 -36
  64. data/spec/validators/lambda_spec.rb +20 -18
  65. data/spec/validators/many_spec.rb +25 -23
  66. data/spec/validators/multiple_spec.rb +13 -11
  67. data/spec/validators/optional_spec.rb +22 -20
  68. data/spec/validators/presence_spec.rb +16 -14
  69. data/spec/validators/regexp_spec.rb +14 -12
  70. data/spec/validators/simple_spec.rb +17 -15
  71. data/spec/validators/simple_types_spec.rb +21 -19
  72. data/spec/validators/url_validator_spec.rb +34 -32
  73. data/spec/validators/user_defined_spec.rb +29 -27
  74. metadata +59 -2
@@ -1,88 +1,90 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
2
4
 
3
5
  describe HashValidator::Validator::JsonValidator do
4
6
  let(:validator) { HashValidator::Validator::JsonValidator.new }
5
7
 
6
- context 'valid JSON' do
7
- it 'validates simple JSON objects' do
8
+ context "valid JSON" do
9
+ it "validates simple JSON objects" do
8
10
  errors = {}
9
- validator.validate('key', '{"name": "test"}', {}, errors)
11
+ validator.validate("key", '{"name": "test"}', {}, errors)
10
12
  expect(errors).to be_empty
11
13
  end
12
14
 
13
- it 'validates JSON arrays' do
15
+ it "validates JSON arrays" do
14
16
  errors = {}
15
- validator.validate('key', '[1, 2, 3]', {}, errors)
17
+ validator.validate("key", "[1, 2, 3]", {}, errors)
16
18
  expect(errors).to be_empty
17
19
  end
18
20
 
19
- it 'validates JSON strings' do
21
+ it "validates JSON strings" do
20
22
  errors = {}
21
- validator.validate('key', '"hello world"', {}, errors)
23
+ validator.validate("key", '"hello world"', {}, errors)
22
24
  expect(errors).to be_empty
23
25
  end
24
26
 
25
- it 'validates JSON numbers' do
27
+ it "validates JSON numbers" do
26
28
  errors = {}
27
- validator.validate('key', '42', {}, errors)
29
+ validator.validate("key", "42", {}, errors)
28
30
  expect(errors).to be_empty
29
31
  end
30
32
 
31
- it 'validates JSON booleans' do
33
+ it "validates JSON booleans" do
32
34
  errors = {}
33
- validator.validate('key', 'true', {}, errors)
35
+ validator.validate("key", "true", {}, errors)
34
36
  expect(errors).to be_empty
35
37
  end
36
38
 
37
- it 'validates JSON null' do
39
+ it "validates JSON null" do
38
40
  errors = {}
39
- validator.validate('key', 'null', {}, errors)
41
+ validator.validate("key", "null", {}, errors)
40
42
  expect(errors).to be_empty
41
43
  end
42
44
 
43
- it 'validates complex nested JSON' do
45
+ it "validates complex nested JSON" do
44
46
  errors = {}
45
47
  json_string = '{"users": [{"name": "John", "age": 30}, {"name": "Jane", "age": 25}]}'
46
- validator.validate('key', json_string, {}, errors)
48
+ validator.validate("key", json_string, {}, errors)
47
49
  expect(errors).to be_empty
48
50
  end
49
51
  end
50
52
 
51
- context 'invalid JSON' do
52
- it 'rejects non-string values' do
53
+ context "invalid JSON" do
54
+ it "rejects non-string values" do
53
55
  errors = {}
54
- validator.validate('key', 123, {}, errors)
55
- expect(errors['key']).to eq('is not valid JSON')
56
+ validator.validate("key", 123, {}, errors)
57
+ expect(errors["key"]).to eq("is not valid JSON")
56
58
  end
57
59
 
58
- it 'rejects nil values' do
60
+ it "rejects nil values" do
59
61
  errors = {}
60
- validator.validate('key', nil, {}, errors)
61
- expect(errors['key']).to eq('is not valid JSON')
62
+ validator.validate("key", nil, {}, errors)
63
+ expect(errors["key"]).to eq("is not valid JSON")
62
64
  end
63
65
 
64
- it 'rejects malformed JSON' do
66
+ it "rejects malformed JSON" do
65
67
  errors = {}
66
- validator.validate('key', '{"name": "test"', {}, errors)
67
- expect(errors['key']).to eq('is not valid JSON')
68
+ validator.validate("key", '{"name": "test"', {}, errors)
69
+ expect(errors["key"]).to eq("is not valid JSON")
68
70
  end
69
71
 
70
- it 'rejects invalid JSON syntax' do
72
+ it "rejects invalid JSON syntax" do
71
73
  errors = {}
72
- validator.validate('key', '{name: "test"}', {}, errors)
73
- expect(errors['key']).to eq('is not valid JSON')
74
+ validator.validate("key", '{name: "test"}', {}, errors)
75
+ expect(errors["key"]).to eq("is not valid JSON")
74
76
  end
75
77
 
76
- it 'rejects empty strings' do
78
+ it "rejects empty strings" do
77
79
  errors = {}
78
- validator.validate('key', '', {}, errors)
79
- expect(errors['key']).to eq('is not valid JSON')
80
+ validator.validate("key", "", {}, errors)
81
+ expect(errors["key"]).to eq("is not valid JSON")
80
82
  end
81
83
 
82
- it 'rejects plain text' do
84
+ it "rejects plain text" do
83
85
  errors = {}
84
- validator.validate('key', 'hello world', {}, errors)
85
- expect(errors['key']).to eq('is not valid JSON')
86
+ validator.validate("key", "hello world", {}, errors)
87
+ expect(errors["key"]).to eq("is not valid JSON")
86
88
  end
87
89
  end
88
- end
90
+ end
@@ -1,47 +1,49 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
- describe 'Functional validator' do
4
- describe 'Accepting Lambdas in validations' do
5
- it 'should accept a lambda' do
3
+ require "spec_helper"
4
+
5
+ describe "Functional validator" do
6
+ describe "Accepting Lambdas in validations" do
7
+ it "should accept a lambda" do
6
8
  validate({}, { foo: lambda { |arg| } })
7
9
  end
8
10
 
9
- it 'should accept a proc' do
11
+ it "should accept a proc" do
10
12
  validate({}, { foo: Proc.new { |arg| } })
11
13
  end
12
14
  end
13
15
 
14
- describe 'Correct number of arguments for lambads in validations' do
15
- it 'should accept a lambda with one argument' do
16
+ describe "Correct number of arguments for lambads in validations" do
17
+ it "should accept a lambda with one argument" do
16
18
  expect { validate({}, { foo: lambda { |a| } }) }.to_not raise_error
17
19
  end
18
20
 
19
- it 'should not accept a lambda with no arguments' do
21
+ it "should not accept a lambda with no arguments" do
20
22
  expect { validate({}, { foo: lambda { } }) }.to raise_error(HashValidator::Validator::LambdaValidator::InvalidArgumentCount)
21
23
  end
22
24
 
23
- it 'should not accept a lambda with two arguments' do
24
- expect { validate({}, { foo: lambda { |a,b| } }) }.to raise_error(HashValidator::Validator::LambdaValidator::InvalidArgumentCount)
25
+ it "should not accept a lambda with two arguments" do
26
+ expect { validate({}, { foo: lambda { |a, b| } }) }.to raise_error(HashValidator::Validator::LambdaValidator::InvalidArgumentCount)
25
27
  end
26
28
  end
27
29
 
28
- describe '#validate' do
29
- let(:validations) {{ number: lambda { |n| n.odd? } }}
30
+ describe "#validate" do
31
+ let(:validations) { { number: lambda { |n| n.odd? } } }
30
32
 
31
- it 'should validate true when the number is odd' do
33
+ it "should validate true when the number is odd" do
32
34
  expect(validate({ number: 1 }, validations).valid?).to eq true
33
35
  end
34
36
 
35
- it 'should validate false when the number is even' do
37
+ it "should validate false when the number is even" do
36
38
  expect(validate({ number: 2 }, validations).valid?).to eq false
37
39
  end
38
40
  end
39
41
 
40
- describe 'Thrown exceptions from within the lambda' do
41
- let(:validations) {{ number: lambda { |n| n.odd? } }}
42
+ describe "Thrown exceptions from within the lambda" do
43
+ let(:validations) { { number: lambda { |n| n.odd? } } }
42
44
 
43
- it 'should validate false when an exception occurs within the lambda' do
44
- expect(validate({ number: '2' }, validations).valid?).to eq false
45
+ it "should validate false when an exception occurs within the lambda" do
46
+ expect(validate({ number: "2" }, validations).valid?).to eq false
45
47
  end
46
48
  end
47
49
  end
@@ -1,4 +1,6 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
2
4
 
3
5
  describe HashValidator::Validator::Base do
4
6
  let(:validator) { HashValidator::Validator::ManyValidator.new }
@@ -8,53 +10,53 @@ describe HashValidator::Validator::Base do
8
10
  HashValidator::Validations::Many.new(validation)
9
11
  end
10
12
 
11
- describe '#should_validate?' do
12
- it 'should validate an Many validation' do
13
- expect(validator.should_validate?(many('string'))).to eq true
13
+ describe "#should_validate?" do
14
+ it "should validate an Many validation" do
15
+ expect(validator.should_validate?(many("string"))).to eq true
14
16
  end
15
17
 
16
- it 'should not validate other things' do
17
- expect(validator.should_validate?('string')).to eq false
18
- expect(validator.should_validate?('array')).to eq false
18
+ it "should not validate other things" do
19
+ expect(validator.should_validate?("string")).to eq false
20
+ expect(validator.should_validate?("array")).to eq false
19
21
  expect(validator.should_validate?(nil)).to eq false
20
22
  end
21
23
  end
22
24
 
23
- describe '#validate' do
24
- it 'should accept an empty array' do
25
- validator.validate(:key, [], many('string'), errors)
25
+ describe "#validate" do
26
+ it "should accept an empty array" do
27
+ validator.validate(:key, [], many("string"), errors)
26
28
 
27
29
  expect(errors).to be_empty
28
30
  end
29
31
 
30
- it 'should accept an array of matching elements' do
31
- validator.validate(:key, ['a', 'b'], many('string'), errors)
32
+ it "should accept an array of matching elements" do
33
+ validator.validate(:key, ["a", "b"], many("string"), errors)
32
34
 
33
35
  expect(errors).to be_empty
34
36
  end
35
37
 
36
- it 'should not accept an array including a non-matching element' do
37
- validator.validate(:key, ['a', 2], many('string'), errors)
38
+ it "should not accept an array including a non-matching element" do
39
+ validator.validate(:key, ["a", 2], many("string"), errors)
38
40
 
39
- expect(errors).to eq({ key: [nil, 'string required'] })
41
+ expect(errors).to eq({ key: [nil, "string required"] })
40
42
  end
41
43
 
42
- it 'should accept an array of matching hashes' do
43
- validator.validate(:key, [{v: 'a'}, {v: 'b'}], many({v: 'string'}), errors)
44
+ it "should accept an array of matching hashes" do
45
+ validator.validate(:key, [{ v: "a" }, { v: "b" }], many({ v: "string" }), errors)
44
46
 
45
47
  expect(errors).to be_empty
46
48
  end
47
49
 
48
- it 'should not accept an array including a non-matching element' do
49
- validator.validate(:key, [{v: 'a'}, {v: 2}], many({v: 'string'}), errors)
50
+ it "should not accept an array including a non-matching element" do
51
+ validator.validate(:key, [{ v: "a" }, { v: 2 }], many({ v: "string" }), errors)
50
52
 
51
- expect(errors).to eq({ key: [nil, {v: 'string required'}] })
53
+ expect(errors).to eq({ key: [nil, { v: "string required" }] })
52
54
  end
53
55
 
54
- it 'should not accept a non-enumerable' do
55
- validator.validate(:key, 'a', many({v: 'string'}), errors)
56
+ it "should not accept a non-enumerable" do
57
+ validator.validate(:key, "a", many({ v: "string" }), errors)
56
58
 
57
- expect(errors).to eq({ key: 'enumerable required' })
59
+ expect(errors).to eq({ key: "enumerable required" })
58
60
  end
59
61
  end
60
62
  end
@@ -1,4 +1,6 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
2
4
 
3
5
  describe HashValidator::Validator::Base do
4
6
  let(:validator) { HashValidator::Validator::MultipleValidator.new }
@@ -8,27 +10,27 @@ describe HashValidator::Validator::Base do
8
10
  HashValidator::Validations::Multiple.new(validations)
9
11
  end
10
12
 
11
- describe '#should_validate?' do
12
- it 'should validate an Multiple validation' do
13
- expect(validator.should_validate?(multiple('numeric', 1..10))).to eq true
13
+ describe "#should_validate?" do
14
+ it "should validate an Multiple validation" do
15
+ expect(validator.should_validate?(multiple("numeric", 1..10))).to eq true
14
16
  end
15
17
 
16
- it 'should not validate other things' do
17
- expect(validator.should_validate?('string')).to eq false
18
- expect(validator.should_validate?('array')).to eq false
18
+ it "should not validate other things" do
19
+ expect(validator.should_validate?("string")).to eq false
20
+ expect(validator.should_validate?("array")).to eq false
19
21
  expect(validator.should_validate?(nil)).to eq false
20
22
  end
21
23
  end
22
24
 
23
- describe '#validate' do
24
- it 'should accept an empty collection of validators' do
25
+ describe "#validate" do
26
+ it "should accept an empty collection of validators" do
25
27
  validator.validate(:key, 73, multiple(), errors)
26
28
 
27
29
  expect(errors).to be_empty
28
30
  end
29
31
 
30
- it 'should accept an collection of validators' do
31
- validator.validate(:key, 73, multiple('numeric', 1..100), errors)
32
+ it "should accept an collection of validators" do
33
+ validator.validate(:key, 73, multiple("numeric", 1..100), errors)
32
34
 
33
35
  expect(errors).to be_empty
34
36
  end
@@ -1,4 +1,6 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
2
4
 
3
5
  describe HashValidator::Validator::Base do
4
6
  let(:validator) { HashValidator::Validator::OptionalValidator.new }
@@ -8,49 +10,49 @@ describe HashValidator::Validator::Base do
8
10
  HashValidator::Validations::Optional.new(validation)
9
11
  end
10
12
 
11
- describe '#should_validate?' do
12
- it 'should validate an Optional validation' do
13
- expect(validator.should_validate?(optional('string'))).to eq true
13
+ describe "#should_validate?" do
14
+ it "should validate an Optional validation" do
15
+ expect(validator.should_validate?(optional("string"))).to eq true
14
16
  end
15
17
 
16
- it 'should not validate other things' do
17
- expect(validator.should_validate?('string')).to eq false
18
- expect(validator.should_validate?('array')).to eq false
18
+ it "should not validate other things" do
19
+ expect(validator.should_validate?("string")).to eq false
20
+ expect(validator.should_validate?("array")).to eq false
19
21
  expect(validator.should_validate?(nil)).to eq false
20
22
  end
21
23
  end
22
24
 
23
- describe '#validate' do
24
- it 'should accept a missing value' do
25
- validator.validate(:key, nil, optional('string'), errors)
25
+ describe "#validate" do
26
+ it "should accept a missing value" do
27
+ validator.validate(:key, nil, optional("string"), errors)
26
28
 
27
29
  expect(errors).to be_empty
28
30
  end
29
31
 
30
- it 'should accept a present, matching value' do
31
- validator.validate(:key, 'foo', optional('string'), errors)
32
+ it "should accept a present, matching value" do
33
+ validator.validate(:key, "foo", optional("string"), errors)
32
34
 
33
35
  expect(errors).to be_empty
34
36
  end
35
37
 
36
- it 'should reject a present, non-matching value' do
37
- validator.validate(:key, 123, optional('string'), errors)
38
+ it "should reject a present, non-matching value" do
39
+ validator.validate(:key, 123, optional("string"), errors)
38
40
 
39
41
  expect(errors).not_to be_empty
40
- expect(errors).to eq({ key: 'string required' })
42
+ expect(errors).to eq({ key: "string required" })
41
43
  end
42
44
 
43
- it 'should accept a present, matching hash' do
44
- validator.validate(:key, {v: 'foo'}, optional({v: 'string'}), errors)
45
+ it "should accept a present, matching hash" do
46
+ validator.validate(:key, { v: "foo" }, optional({ v: "string" }), errors)
45
47
 
46
48
  expect(errors).to be_empty
47
49
  end
48
50
 
49
- it 'should reject a present, non-matching hash' do
50
- validator.validate(:key, {}, optional({v: 'string'}), errors)
51
+ it "should reject a present, non-matching hash" do
52
+ validator.validate(:key, {}, optional({ v: "string" }), errors)
51
53
 
52
54
  expect(errors).not_to be_empty
53
- expect(errors).to eq({ key: {v: 'string required'} })
55
+ expect(errors).to eq({ key: { v: "string required" } })
54
56
  end
55
57
  end
56
58
  end
@@ -1,48 +1,50 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
2
4
 
3
5
  describe HashValidator::Validator::Base do
4
6
  let(:validator) { HashValidator::Validator::PresenceValidator.new }
5
7
  let(:errors) { Hash.new }
6
8
 
7
- describe '#should_validate?' do
9
+ describe "#should_validate?" do
8
10
  it 'should validate the name "required"' do
9
- expect(validator.should_validate?('required')).to eq true
11
+ expect(validator.should_validate?("required")).to eq true
10
12
  end
11
13
 
12
- it 'should not validate other names' do
13
- expect(validator.should_validate?('string')).to eq false
14
- expect(validator.should_validate?('array')).to eq false
14
+ it "should not validate other names" do
15
+ expect(validator.should_validate?("string")).to eq false
16
+ expect(validator.should_validate?("array")).to eq false
15
17
  expect(validator.should_validate?(nil)).to eq false
16
18
  end
17
19
  end
18
20
 
19
- describe '#validate' do
20
- it 'should validate a string with true' do
21
- validator.validate(:key, 'test', {}, errors)
21
+ describe "#validate" do
22
+ it "should validate a string with true" do
23
+ validator.validate(:key, "test", {}, errors)
22
24
 
23
25
  expect(errors).to be_empty
24
26
  end
25
27
 
26
- it 'should validate a number with true' do
28
+ it "should validate a number with true" do
27
29
  validator.validate(:key, 123, {}, errors)
28
30
 
29
31
  expect(errors).to be_empty
30
32
  end
31
33
 
32
- it 'should validate a time with true' do
34
+ it "should validate a time with true" do
33
35
  validator.validate(:key, Time.now, {}, errors)
34
36
 
35
37
  expect(errors).to be_empty
36
38
  end
37
39
 
38
- it 'should validate nil with false' do
40
+ it "should validate nil with false" do
39
41
  validator.validate(:key, nil, {}, errors)
40
42
 
41
43
  expect(errors).not_to be_empty
42
- expect(errors).to eq({ key: 'is required' })
44
+ expect(errors).to eq({ key: "is required" })
43
45
  end
44
46
 
45
- it 'should validate false with true' do
47
+ it "should validate false with true" do
46
48
  validator.validate(:key, false, {}, errors)
47
49
 
48
50
  expect(errors).to be_empty
@@ -1,23 +1,25 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
- describe 'Regular expression validator' do
4
- describe 'Accepting RegExps in validations' do
5
- it 'should accept a regexp' do
3
+ require "spec_helper"
4
+
5
+ describe "Regular expression validator" do
6
+ describe "Accepting RegExps in validations" do
7
+ it "should accept a regexp" do
6
8
  validate({}, { foo: // })
7
9
  end
8
10
  end
9
11
 
10
- describe '#validate' do
11
- let(:validations) {{ string: /^foo$/ }}
12
+ describe "#validate" do
13
+ let(:validations) { { string: /^foo$/ } }
12
14
 
13
- it 'should validate true when the value is foo' do
14
- expect(validate({ string: 'foo' }, validations).valid?).to eq true
15
+ it "should validate true when the value is foo" do
16
+ expect(validate({ string: "foo" }, validations).valid?).to eq true
15
17
  end
16
18
 
17
- it 'should validate false when the value is not foo' do
18
- expect(validate({ string: 'bar' }, validations).valid?).to eq false
19
- expect(validate({ string: ' foo' }, validations).valid?).to eq false
20
- expect(validate({ string: 'foo ' }, validations).valid?).to eq false
19
+ it "should validate false when the value is not foo" do
20
+ expect(validate({ string: "bar" }, validations).valid?).to eq false
21
+ expect(validate({ string: " foo" }, validations).valid?).to eq false
22
+ expect(validate({ string: "foo " }, validations).valid?).to eq false
21
23
  expect(validate({ string: nil }, validations).valid?).to eq false
22
24
  expect(validate({ string: 0 }, validations).valid?).to eq false
23
25
  expect(validate({ string: true }, validations).valid?).to eq false
@@ -1,33 +1,35 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
2
4
 
3
5
  describe HashValidator::Validator::SimpleValidator do
4
- describe '#initialize' do
5
- it 'accepts blocks with one argument' do
6
+ describe "#initialize" do
7
+ it "accepts blocks with one argument" do
6
8
  expect {
7
- HashValidator::Validator::SimpleValidator.new('name', lambda { |a| true })
9
+ HashValidator::Validator::SimpleValidator.new("name", lambda { |a| true })
8
10
  }.to_not raise_error
9
11
  end
10
12
 
11
- it 'does not accept blocks with no arguments' do
13
+ it "does not accept blocks with no arguments" do
12
14
  expect {
13
- HashValidator::Validator::SimpleValidator.new('name', lambda { true })
14
- }.to raise_error(StandardError, 'lambda should take only one argument - passed lambda takes 0')
15
+ HashValidator::Validator::SimpleValidator.new("name", lambda { true })
16
+ }.to raise_error(StandardError, "lambda should take only one argument - passed lambda takes 0")
15
17
  end
16
18
 
17
- it 'does not accept blocks with two arguments' do
19
+ it "does not accept blocks with two arguments" do
18
20
  expect {
19
- HashValidator::Validator::SimpleValidator.new('name', lambda { |a,b| true })
20
- }.to raise_error(StandardError, 'lambda should take only one argument - passed lambda takes 2')
21
+ HashValidator::Validator::SimpleValidator.new("name", lambda { |a, b| true })
22
+ }.to raise_error(StandardError, "lambda should take only one argument - passed lambda takes 2")
21
23
  end
22
24
  end
23
25
 
24
- describe '#validate' do
26
+ describe "#validate" do
25
27
  # Lambda that accepts strings that are 4 characters or shorter
26
28
  let(:short_string_lambda) { lambda { |v| v.is_a?(String) && v.size < 5 } }
27
- let(:short_string_validator) { HashValidator::Validator::SimpleValidator.new('short_string', short_string_lambda) }
29
+ let(:short_string_validator) { HashValidator::Validator::SimpleValidator.new("short_string", short_string_lambda) }
28
30
  let(:errors) { Hash.new }
29
31
 
30
- [ '', '1', '12', '123', '1234' ].each do |value|
32
+ [ "", "1", "12", "123", "1234" ].each do |value|
31
33
  it "validates the string '#{value}'" do
32
34
  short_string_validator.validate(:key, value, {}, errors)
33
35
 
@@ -35,11 +37,11 @@ describe HashValidator::Validator::SimpleValidator do
35
37
  end
36
38
  end
37
39
 
38
- [ nil, '12345', '123456', 123, 123456, Time.now, (1..5) ].each do |value|
40
+ [ nil, "12345", "123456", 123, 123456, Time.now, (1..5) ].each do |value|
39
41
  it "does not validate bad value '#{value}'" do
40
42
  short_string_validator.validate(:key, value, {}, errors)
41
43
 
42
- expect(errors).to eq({ key: 'short_string required' })
44
+ expect(errors).to eq({ key: "short_string required" })
43
45
  end
44
46
  end
45
47
  end