easy_json_matcher 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/easy_json_matcher/json_coercer.rb +0 -10
- data/lib/easy_json_matcher/version.rb +1 -1
- data/test/custom_validations_test.rb +1 -1
- data/test/global_validation_options_test.rb +4 -4
- data/test/managing_schemas_test.rb +8 -8
- data/test/node_test.rb +5 -5
- data/test/primitives_boolean_test.rb +1 -1
- data/test/primitives_date_test.rb +1 -1
- data/test/primitives_number_test.rb +1 -1
- data/test/primitives_object_test.rb +1 -1
- data/test/primitives_string_test.rb +2 -2
- data/test/required_validation_test.rb +4 -4
- data/test/strict_mode_test.rb +3 -3
- data/test/validating_arrays_test.rb +4 -4
- data/test/validator_set_test.rb +8 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2fc40b15589acbb444020f137e9e079241f73ee9
|
4
|
+
data.tar.gz: 05197c5eac34a9b66d83405debaf0d17ce1b0602
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 448e3d74f73b2abbe9dd43256c06bbd3bee7ce29c29789b95672b32a314af43af6878b5d49b7a17780f96fdfe2d26d3609243cae4e8a1b2e66fb3b829b54ea9b
|
7
|
+
data.tar.gz: 51c81f2acd450002ce9f50c703613cefb43390aad04b8802b411d75c883d23bfca153b9d78420edb73116a3c7939c748569c9a5896e5b90a798a0fb6e39f98cf
|
@@ -7,21 +7,11 @@ module EasyJSONMatcher
|
|
7
7
|
def coerce(json:)
|
8
8
|
begin
|
9
9
|
coerced = JSON.parse(json)
|
10
|
-
symbolize_keys(hash: coerced)
|
11
10
|
rescue JSON::ParserError, TypeError
|
12
11
|
raise CoercionError.new invalid_string: json
|
13
12
|
end
|
14
13
|
end
|
15
14
|
|
16
|
-
def symbolize_keys(hash:)
|
17
|
-
hash.keys.each do |key|
|
18
|
-
value = hash.delete(key)
|
19
|
-
convert_value(value)
|
20
|
-
hash[key.to_sym] = value
|
21
|
-
end
|
22
|
-
hash
|
23
|
-
end
|
24
|
-
|
25
15
|
def convert_value(value)
|
26
16
|
case value
|
27
17
|
when Hash
|
@@ -7,10 +7,10 @@ module EasyJSONMatcher
|
|
7
7
|
|
8
8
|
subject {
|
9
9
|
SchemaGenerator.new(global_opts: [ :required ]) { |schema|
|
10
|
-
schema.has_boolean key:
|
11
|
-
schema.contains_node key:
|
12
|
-
n.has_boolean key:
|
13
|
-
n.has_boolean key:
|
10
|
+
schema.has_boolean key: "implicitly_required"
|
11
|
+
schema.contains_node key: "also_implicitly_required" do |n|
|
12
|
+
n.has_boolean key: "nested_implicitly_required"
|
13
|
+
n.has_boolean key: "not_required", opts: [:not_required]
|
14
14
|
end
|
15
15
|
}.generate_schema
|
16
16
|
}
|
@@ -7,7 +7,7 @@ module EasyJSONMatcher
|
|
7
7
|
setup do
|
8
8
|
@name = :test
|
9
9
|
SchemaGenerator.new { |schema|
|
10
|
-
schema.has_attribute(key:
|
10
|
+
schema.has_attribute(key: "name", opts: [ :string, :required ])
|
11
11
|
}.register(as: @name)
|
12
12
|
end
|
13
13
|
|
@@ -31,7 +31,7 @@ module EasyJSONMatcher
|
|
31
31
|
|
32
32
|
test "As a user I want to reuse a schema within another schema" do
|
33
33
|
test_schema = SchemaGenerator.new { |s|
|
34
|
-
s.has_boolean key:
|
34
|
+
s.has_boolean key: "is_present", opts: [ :required ]
|
35
35
|
s.has_schema key: @name, name: @name
|
36
36
|
}.generate_schema
|
37
37
|
|
@@ -49,16 +49,16 @@ module EasyJSONMatcher
|
|
49
49
|
|
50
50
|
test "It can validate JSON Schema payloads" do
|
51
51
|
SchemaGenerator.new { |country|
|
52
|
-
country.has_attribute key:
|
53
|
-
country.contains_node(key:
|
54
|
-
atts.has_attribute key:
|
55
|
-
atts.has_attribute key:
|
56
|
-
atts.has_attribute key:
|
52
|
+
country.has_attribute key: "id", opts: [:number, :required]
|
53
|
+
country.contains_node(key: "attributes") do |atts|
|
54
|
+
atts.has_attribute key: "alpha_2", opts: [ :string, :required ]
|
55
|
+
atts.has_attribute key: "alpha_3", opts: [ :string, :required ]
|
56
|
+
atts.has_attribute key: "name", opts: [ :string, :required ]
|
57
57
|
end
|
58
58
|
}.register(as: :country)
|
59
59
|
|
60
60
|
country_payload = SchemaGenerator.new {|country_payload|
|
61
|
-
country_payload.has_schema(key:
|
61
|
+
country_payload.has_schema(key: "data", name: :country)
|
62
62
|
}.register(as: :country_payload)
|
63
63
|
|
64
64
|
valid_json = "{\"data\":{\"id\":\"4376\",\"type\":\"countries\",\"attributes\":{\"alpha_2\":\"GB\",\"alpha_3\":\"GBR\",\"name\":\"United Kingdom of Great Britain and Northern Ireland\"}}}"
|
data/test/node_test.rb
CHANGED
@@ -12,14 +12,14 @@ module EasyJSONMatcher
|
|
12
12
|
end
|
13
13
|
|
14
14
|
it "should send call to its own validator" do
|
15
|
-
test_value = { a
|
16
|
-
validators = (
|
15
|
+
test_value = { "a" => 1, "b" => 2, "c" => 3 }
|
16
|
+
validators = ("a".."c").each_with_object({}) do |n, h|
|
17
17
|
h[n] = ValidationChainFactory.get_chain(steps: [:string])
|
18
18
|
end
|
19
19
|
node = Node.new(opts: [:required], validators: validators)
|
20
|
-
expected = [{a
|
21
|
-
b
|
22
|
-
c
|
20
|
+
expected = [{"a" => ["1 is not a String"],
|
21
|
+
"b" => ["2 is not a String"],
|
22
|
+
"c" => ["3 is not a String"]
|
23
23
|
}]
|
24
24
|
node.check(value: test_value).must_be :==, expected
|
25
25
|
end
|
@@ -6,11 +6,11 @@ require 'json'
|
|
6
6
|
|
7
7
|
module EasyJSONMatcher
|
8
8
|
|
9
|
-
describe "
|
9
|
+
describe "String Primitives" do
|
10
10
|
|
11
11
|
before do
|
12
12
|
@test_schema = SchemaGenerator.new { |g|
|
13
|
-
g.has_string key:
|
13
|
+
g.has_string key: "string"
|
14
14
|
}.generate_schema
|
15
15
|
end
|
16
16
|
|
@@ -4,8 +4,8 @@ class RequireValidationTest < ActiveSupport::TestCase
|
|
4
4
|
|
5
5
|
test "As a user I want to insist that a value is present" do
|
6
6
|
astronaut_schema = EasyJSONMatcher::SchemaGenerator.new { |s|
|
7
|
-
s.has_attribute key:
|
8
|
-
s.has_attribute key:
|
7
|
+
s.has_attribute key: "has_oxygen", opts: [:boolean, :required]
|
8
|
+
s.has_attribute key: "name", opts: [:string]
|
9
9
|
}.generate_schema
|
10
10
|
|
11
11
|
valid_astronaut = {
|
@@ -24,8 +24,8 @@ class RequireValidationTest < ActiveSupport::TestCase
|
|
24
24
|
|
25
25
|
test "As a user I want validations to pass if the value is not present and I have not required it to be there" do
|
26
26
|
astronaut_schema = EasyJSONMatcher::SchemaGenerator.new { |s|
|
27
|
-
s.has_attribute key:
|
28
|
-
s.has_attribute key:
|
27
|
+
s.has_attribute key: "has_oxygen", opts: [:boolean]
|
28
|
+
s.has_attribute key: "name", opts: [:string]
|
29
29
|
}.generate_schema
|
30
30
|
|
31
31
|
valid_astronaut = {
|
data/test/strict_mode_test.rb
CHANGED
@@ -5,9 +5,9 @@ module EasyJSONMatcher
|
|
5
5
|
describe "Strict Mode" do
|
6
6
|
|
7
7
|
subject {
|
8
|
-
SchemaGenerator.new(global_opts: [
|
9
|
-
s.has_attribute key:
|
10
|
-
s.has_attribute key:
|
8
|
+
SchemaGenerator.new(global_opts: [:strict]) { |s|
|
9
|
+
s.has_attribute key: "a", opts: []
|
10
|
+
s.has_attribute key: "b", opts: []
|
11
11
|
}.generate_schema
|
12
12
|
}
|
13
13
|
|
@@ -7,14 +7,14 @@ module EasyJSONMatcher
|
|
7
7
|
|
8
8
|
before do
|
9
9
|
SchemaGenerator.new {|s|
|
10
|
-
s.has_attribute key:
|
11
|
-
s.has_attribute key:
|
10
|
+
s.has_attribute key: "name", opts: [:string, :required]
|
11
|
+
s.has_attribute key: "spouse", opts: [:string, :required]
|
12
12
|
}.register as: :greek_hero
|
13
13
|
end
|
14
14
|
|
15
15
|
subject{
|
16
16
|
test_schema = SchemaGenerator.new {|s|
|
17
|
-
s.contains_array(key:
|
17
|
+
s.contains_array(key: "data") do |a|
|
18
18
|
a.elements_should be: [:greek_hero]
|
19
19
|
end
|
20
20
|
}.generate_schema
|
@@ -22,7 +22,7 @@ module EasyJSONMatcher
|
|
22
22
|
|
23
23
|
it "should validate each value in the array" do
|
24
24
|
validator = SchemaGenerator.new { |s|
|
25
|
-
s.contains_array(key:
|
25
|
+
s.contains_array(key: "array") do |a|
|
26
26
|
a.elements_should be: [:number]
|
27
27
|
end
|
28
28
|
}.generate_schema
|
data/test/validator_set_test.rb
CHANGED
@@ -11,6 +11,14 @@ module EasyJSONMatcher
|
|
11
11
|
subject.validators.values.include?(test_val).must_be :==, true
|
12
12
|
end
|
13
13
|
|
14
|
+
it "should validate with keys and symbols" do
|
15
|
+
mock_validators = { key_1: mock_validator, "key_2" => mock_validator }
|
16
|
+
subject = ValidatorSet.new validators: mock_validators
|
17
|
+
test_val = { "key_1" => 1, key_2: 2 }
|
18
|
+
subject.check(value: test_val)
|
19
|
+
mock_validators.each_value(&:verify)
|
20
|
+
end
|
21
|
+
|
14
22
|
it "should return true if all its validators validate their candidates" do
|
15
23
|
mock_validators = { key1: mock_validator, key2: mock_validator }
|
16
24
|
subject = ValidatorSet.new validators: mock_validators
|