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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9d89a8713abb0a107d389588fc6ab8276b97a73e
4
- data.tar.gz: 1a984ae9ebf4154fe92ed59b4d551aed3d31949d
3
+ metadata.gz: 2fc40b15589acbb444020f137e9e079241f73ee9
4
+ data.tar.gz: 05197c5eac34a9b66d83405debaf0d17ce1b0602
5
5
  SHA512:
6
- metadata.gz: 3859e9e26ccd582d6cd76438b7dfb27f852211dcce7e2863050e1f48981de0539ff3835571b02fe1de45ade149f11cc46c31ec776d403d80e315d0dbd81a758c
7
- data.tar.gz: 4f9486b504b527d7ab1a081538cdfa5e401c7a69ebf3b3ecbe3ed187a9dd0616828fe5958679f2e3d4c91031c2c6846f79a5f745dd4a1b4a3406d4c121048f3a
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
@@ -1,3 +1,3 @@
1
1
  module EasyJSONMatcher
2
- VERSION = "0.3.2".freeze
2
+ VERSION = "0.3.3".freeze
3
3
  end
@@ -6,7 +6,7 @@ module EasyJSONMatcher
6
6
 
7
7
  subject {
8
8
  SchemaGenerator.new { |sc|
9
- sc.has_attribute key: :val,
9
+ sc.has_attribute key: "val",
10
10
  opts: [
11
11
  :required,
12
12
  ->(value, errors) { errors << "value was false" unless value === true }
@@ -7,10 +7,10 @@ module EasyJSONMatcher
7
7
 
8
8
  subject {
9
9
  SchemaGenerator.new(global_opts: [ :required ]) { |schema|
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]
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: :name, opts: [ :string, :required ])
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: :is_present, opts: [ :required ]
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: :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 ]
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: :data, name: :country)
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: 1, b: 2, c: 3 }
16
- validators = (:a..:c).each_with_object({}) do |n, h|
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: ["1 is not a String"],
21
- b: ["2 is not a String"],
22
- c: ["3 is not a String"]
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
@@ -10,7 +10,7 @@ module EasyJSONMatcher
10
10
 
11
11
  before do
12
12
  @test_schema = SchemaGenerator.new { |g|
13
- g.has_boolean key: :boolean
13
+ g.has_boolean key: "boolean"
14
14
  }.generate_schema
15
15
  end
16
16
 
@@ -11,7 +11,7 @@ module EasyJSONMatcher
11
11
 
12
12
  before do
13
13
  @test_schema = SchemaGenerator.new { |g|
14
- g.has_date key: :date
14
+ g.has_date key: "date"
15
15
  }.generate_schema
16
16
  end
17
17
 
@@ -10,7 +10,7 @@ module EasyJSONMatcher
10
10
 
11
11
  before do
12
12
  @test_schema = SchemaGenerator.new { |g|
13
- g.has_number key: :number
13
+ g.has_number key: "number"
14
14
  }.generate_schema
15
15
  end
16
16
 
@@ -10,7 +10,7 @@ module EasyJSONMatcher
10
10
 
11
11
  before do
12
12
  @test_schema = SchemaGenerator.new { |g|
13
- g.has_object key: :object
13
+ g.has_object key: "object"
14
14
  }.generate_schema
15
15
  end
16
16
 
@@ -6,11 +6,11 @@ require 'json'
6
6
 
7
7
  module EasyJSONMatcher
8
8
 
9
- describe "Basic Type Validations" do
9
+ describe "String Primitives" do
10
10
 
11
11
  before do
12
12
  @test_schema = SchemaGenerator.new { |g|
13
- g.has_string key: :string
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: :has_oxygen, opts: [:boolean, :required]
8
- s.has_attribute key: :name, opts: [:string]
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: :has_oxygen, opts: [:boolean]
28
- s.has_attribute key: :name, opts: [:string]
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 = {
@@ -5,9 +5,9 @@ module EasyJSONMatcher
5
5
  describe "Strict Mode" do
6
6
 
7
7
  subject {
8
- SchemaGenerator.new(global_opts: [ :strict ]) { |s|
9
- s.has_attribute key: :a, opts: []
10
- s.has_attribute key: :b, opts: []
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: :name, opts: [:string, :required]
11
- s.has_attribute key: :spouse, opts: [:string, :required]
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: :data) do |a|
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: :array) do |a|
25
+ s.contains_array(key: "array") do |a|
26
26
  a.elements_should be: [:number]
27
27
  end
28
28
  }.generate_schema
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_json_matcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - WJD Hamilton