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,88 +1,90 @@
|
|
1
|
-
|
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
|
7
|
-
it
|
8
|
+
context "valid JSON" do
|
9
|
+
it "validates simple JSON objects" do
|
8
10
|
errors = {}
|
9
|
-
validator.validate(
|
11
|
+
validator.validate("key", '{"name": "test"}', {}, errors)
|
10
12
|
expect(errors).to be_empty
|
11
13
|
end
|
12
14
|
|
13
|
-
it
|
15
|
+
it "validates JSON arrays" do
|
14
16
|
errors = {}
|
15
|
-
validator.validate(
|
17
|
+
validator.validate("key", "[1, 2, 3]", {}, errors)
|
16
18
|
expect(errors).to be_empty
|
17
19
|
end
|
18
20
|
|
19
|
-
it
|
21
|
+
it "validates JSON strings" do
|
20
22
|
errors = {}
|
21
|
-
validator.validate(
|
23
|
+
validator.validate("key", '"hello world"', {}, errors)
|
22
24
|
expect(errors).to be_empty
|
23
25
|
end
|
24
26
|
|
25
|
-
it
|
27
|
+
it "validates JSON numbers" do
|
26
28
|
errors = {}
|
27
|
-
validator.validate(
|
29
|
+
validator.validate("key", "42", {}, errors)
|
28
30
|
expect(errors).to be_empty
|
29
31
|
end
|
30
32
|
|
31
|
-
it
|
33
|
+
it "validates JSON booleans" do
|
32
34
|
errors = {}
|
33
|
-
validator.validate(
|
35
|
+
validator.validate("key", "true", {}, errors)
|
34
36
|
expect(errors).to be_empty
|
35
37
|
end
|
36
38
|
|
37
|
-
it
|
39
|
+
it "validates JSON null" do
|
38
40
|
errors = {}
|
39
|
-
validator.validate(
|
41
|
+
validator.validate("key", "null", {}, errors)
|
40
42
|
expect(errors).to be_empty
|
41
43
|
end
|
42
44
|
|
43
|
-
it
|
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(
|
48
|
+
validator.validate("key", json_string, {}, errors)
|
47
49
|
expect(errors).to be_empty
|
48
50
|
end
|
49
51
|
end
|
50
52
|
|
51
|
-
context
|
52
|
-
it
|
53
|
+
context "invalid JSON" do
|
54
|
+
it "rejects non-string values" do
|
53
55
|
errors = {}
|
54
|
-
validator.validate(
|
55
|
-
expect(errors[
|
56
|
+
validator.validate("key", 123, {}, errors)
|
57
|
+
expect(errors["key"]).to eq("is not valid JSON")
|
56
58
|
end
|
57
59
|
|
58
|
-
it
|
60
|
+
it "rejects nil values" do
|
59
61
|
errors = {}
|
60
|
-
validator.validate(
|
61
|
-
expect(errors[
|
62
|
+
validator.validate("key", nil, {}, errors)
|
63
|
+
expect(errors["key"]).to eq("is not valid JSON")
|
62
64
|
end
|
63
65
|
|
64
|
-
it
|
66
|
+
it "rejects malformed JSON" do
|
65
67
|
errors = {}
|
66
|
-
validator.validate(
|
67
|
-
expect(errors[
|
68
|
+
validator.validate("key", '{"name": "test"', {}, errors)
|
69
|
+
expect(errors["key"]).to eq("is not valid JSON")
|
68
70
|
end
|
69
71
|
|
70
|
-
it
|
72
|
+
it "rejects invalid JSON syntax" do
|
71
73
|
errors = {}
|
72
|
-
validator.validate(
|
73
|
-
expect(errors[
|
74
|
+
validator.validate("key", '{name: "test"}', {}, errors)
|
75
|
+
expect(errors["key"]).to eq("is not valid JSON")
|
74
76
|
end
|
75
77
|
|
76
|
-
it
|
78
|
+
it "rejects empty strings" do
|
77
79
|
errors = {}
|
78
|
-
validator.validate(
|
79
|
-
expect(errors[
|
80
|
+
validator.validate("key", "", {}, errors)
|
81
|
+
expect(errors["key"]).to eq("is not valid JSON")
|
80
82
|
end
|
81
83
|
|
82
|
-
it
|
84
|
+
it "rejects plain text" do
|
83
85
|
errors = {}
|
84
|
-
validator.validate(
|
85
|
-
expect(errors[
|
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
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
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
|
11
|
+
it "should accept a proc" do
|
10
12
|
validate({}, { foo: Proc.new { |arg| } })
|
11
13
|
end
|
12
14
|
end
|
13
15
|
|
14
|
-
describe
|
15
|
-
it
|
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
|
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
|
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
|
29
|
-
let(:validations) {{ number: lambda { |n| n.odd? } }}
|
30
|
+
describe "#validate" do
|
31
|
+
let(:validations) { { number: lambda { |n| n.odd? } } }
|
30
32
|
|
31
|
-
it
|
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
|
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
|
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
|
44
|
-
expect(validate({ number:
|
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
|
-
|
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
|
12
|
-
it
|
13
|
-
expect(validator.should_validate?(many(
|
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
|
17
|
-
expect(validator.should_validate?(
|
18
|
-
expect(validator.should_validate?(
|
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
|
24
|
-
it
|
25
|
-
validator.validate(:key, [], many(
|
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
|
31
|
-
validator.validate(:key, [
|
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
|
37
|
-
validator.validate(:key, [
|
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,
|
41
|
+
expect(errors).to eq({ key: [nil, "string required"] })
|
40
42
|
end
|
41
43
|
|
42
|
-
it
|
43
|
-
validator.validate(:key, [{v:
|
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
|
49
|
-
validator.validate(:key, [{v:
|
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:
|
53
|
+
expect(errors).to eq({ key: [nil, { v: "string required" }] })
|
52
54
|
end
|
53
55
|
|
54
|
-
it
|
55
|
-
validator.validate(:key,
|
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:
|
59
|
+
expect(errors).to eq({ key: "enumerable required" })
|
58
60
|
end
|
59
61
|
end
|
60
62
|
end
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
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
|
12
|
-
it
|
13
|
-
expect(validator.should_validate?(multiple(
|
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
|
17
|
-
expect(validator.should_validate?(
|
18
|
-
expect(validator.should_validate?(
|
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
|
24
|
-
it
|
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
|
31
|
-
validator.validate(:key, 73, multiple(
|
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
|
-
|
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
|
12
|
-
it
|
13
|
-
expect(validator.should_validate?(optional(
|
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
|
17
|
-
expect(validator.should_validate?(
|
18
|
-
expect(validator.should_validate?(
|
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
|
24
|
-
it
|
25
|
-
validator.validate(:key, nil, optional(
|
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
|
31
|
-
validator.validate(:key,
|
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
|
37
|
-
validator.validate(:key, 123, optional(
|
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:
|
42
|
+
expect(errors).to eq({ key: "string required" })
|
41
43
|
end
|
42
44
|
|
43
|
-
it
|
44
|
-
validator.validate(:key, {v:
|
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
|
50
|
-
validator.validate(:key, {}, optional({v:
|
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:
|
55
|
+
expect(errors).to eq({ key: { v: "string required" } })
|
54
56
|
end
|
55
57
|
end
|
56
58
|
end
|
@@ -1,48 +1,50 @@
|
|
1
|
-
|
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
|
9
|
+
describe "#should_validate?" do
|
8
10
|
it 'should validate the name "required"' do
|
9
|
-
expect(validator.should_validate?(
|
11
|
+
expect(validator.should_validate?("required")).to eq true
|
10
12
|
end
|
11
13
|
|
12
|
-
it
|
13
|
-
expect(validator.should_validate?(
|
14
|
-
expect(validator.should_validate?(
|
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
|
20
|
-
it
|
21
|
-
validator.validate(:key,
|
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
|
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
|
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
|
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:
|
44
|
+
expect(errors).to eq({ key: "is required" })
|
43
45
|
end
|
44
46
|
|
45
|
-
it
|
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
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
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
|
11
|
-
let(:validations) {{ string: /^foo$/ }}
|
12
|
+
describe "#validate" do
|
13
|
+
let(:validations) { { string: /^foo$/ } }
|
12
14
|
|
13
|
-
it
|
14
|
-
expect(validate({ string:
|
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
|
18
|
-
expect(validate({ string:
|
19
|
-
expect(validate({ string:
|
20
|
-
expect(validate({ string:
|
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
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spec_helper"
|
2
4
|
|
3
5
|
describe HashValidator::Validator::SimpleValidator do
|
4
|
-
describe
|
5
|
-
it
|
6
|
+
describe "#initialize" do
|
7
|
+
it "accepts blocks with one argument" do
|
6
8
|
expect {
|
7
|
-
HashValidator::Validator::SimpleValidator.new(
|
9
|
+
HashValidator::Validator::SimpleValidator.new("name", lambda { |a| true })
|
8
10
|
}.to_not raise_error
|
9
11
|
end
|
10
12
|
|
11
|
-
it
|
13
|
+
it "does not accept blocks with no arguments" do
|
12
14
|
expect {
|
13
|
-
HashValidator::Validator::SimpleValidator.new(
|
14
|
-
}.to raise_error(StandardError,
|
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
|
19
|
+
it "does not accept blocks with two arguments" do
|
18
20
|
expect {
|
19
|
-
HashValidator::Validator::SimpleValidator.new(
|
20
|
-
}.to raise_error(StandardError,
|
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
|
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(
|
29
|
+
let(:short_string_validator) { HashValidator::Validator::SimpleValidator.new("short_string", short_string_lambda) }
|
28
30
|
let(:errors) { Hash.new }
|
29
31
|
|
30
|
-
[
|
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,
|
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:
|
44
|
+
expect(errors).to eq({ key: "short_string required" })
|
43
45
|
end
|
44
46
|
end
|
45
47
|
end
|