easy_json_matcher 0.3.4 → 0.4.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 83a0d1798ca42d453ea4ec8997c8a66c482c6d8f
4
- data.tar.gz: 306a4d081f0dc1f6886bab938e121b5a43428c7b
3
+ metadata.gz: f5594d2bf04fe24083c5ffe568e88f22accbee84
4
+ data.tar.gz: 19de26378a180c35d80ffc5c21fa4cff5064ceb5
5
5
  SHA512:
6
- metadata.gz: 9cef9865bdd10d6e3c7127743f682ee7918a3ab4a98e695f5b012728e74edfd8fcb59c249a4f2f47671243c96a52d47fcc39672c3cbb70e76cf7277a1aabab69
7
- data.tar.gz: a5a1888234ab415e6f2a09da2ff720d5913fa50302904d80abc86d346f5495fee5acfd1d81ca825fbc48a628ac30a5421873ffbdf907367562a2366e1c665fd6
6
+ metadata.gz: 9934c3a54200cc1b4ceb0de8a25ef45509445df8330820db4324270967437038ec48964a7bc91fb9be4c0e2a86bf96ead52e0209d1e0fbb15185632202073713
7
+ data.tar.gz: 44fb3187668459fd709e3a916668411f1f7749ebcad47d32d717ec870fa983bfd800557d5be2e1b2d98343788d44ede157fce4317a7387e40bcee7922bc188ba
@@ -1,5 +1,3 @@
1
- require "easy_json_matcher/array_validator"
2
-
3
1
  module EasyJSONMatcher
4
2
  class ArrayGenerator
5
3
  include AutoInject.kwargs[:array_validator]
@@ -1,5 +1,6 @@
1
1
  module AttributeTypeMethods
2
2
 
3
+
3
4
  def has_boolean(key:, opts: [])
4
5
  has_attribute(key: key, opts: opts + [:boolean])
5
6
  end
@@ -48,8 +49,14 @@ module AttributeTypeMethods
48
49
  mass_assign(keys: keys, opts: opts, meth: :has_date)
49
50
  end
50
51
 
51
-
52
52
  def mass_assign(keys:, opts: [], meth:)
53
53
  keys.each { |k| self.send(meth, { key: k, opts: opts }) }
54
54
  end
55
+
56
+ alias_method :boolean, :has_boolean
57
+ alias_method :number, :has_number
58
+ alias_method :date, :has_date
59
+ alias_method :object, :has_object
60
+ alias_method :value, :has_value
61
+ alias_method :string, :has_string
55
62
  end
@@ -1,5 +1,3 @@
1
- require "easy_json_matcher/validation_step"
2
-
3
1
  module EasyJSONMatcher
4
2
  class Node
5
3
  include AutoInject.kwargs[:node_content_validator, :chain_factory]
@@ -1,9 +1,4 @@
1
- require "easy_json_matcher/validation_chain_factory"
2
- require "easy_json_matcher/node"
3
- require "easy_json_matcher/schema_library"
4
1
  require "easy_json_matcher/attribute_type_methods"
5
- require "easy_json_matcher/attribute_generator"
6
- require "easy_json_matcher/array_generator"
7
2
 
8
3
  module EasyJSONMatcher
9
4
  class NodeGenerator
@@ -29,15 +24,14 @@ module EasyJSONMatcher
29
24
  validators[key] = validator.generate_attribute
30
25
  end
31
26
 
32
- def contains_node(key:, opts: [])
33
- generator = self.class.new(opts: opts, global_opts: global_opts)
34
- yield generator if block_given?
27
+ def contains_node(key:, opts: [], &block)
28
+ generator = self.class.new(opts: opts, global_opts: global_opts, &block)
35
29
  validators[key] = generator.generate_node
36
30
  end
37
31
 
38
- def contains_array(key:, opts: [])
32
+ def contains_array(key:, opts: [], &block)
39
33
  validator = array_generator.new(local_opts: opts, global_opts: global_opts)
40
- yield validator if block_given?
34
+ validator.instance_eval &block if block_given?
41
35
  validators[key] = validator.generate_array
42
36
  end
43
37
 
@@ -12,54 +12,19 @@ module EasyJSONMatcher
12
12
 
13
13
  attr_reader :node, :glob_opts, :att_glob_opts
14
14
 
15
- def initialize(opts: [], global_opts: [], **args)
15
+ def initialize(opts: [], global_opts: [], **args, &block)
16
16
  super(**args)
17
17
  @glob_opts = global_opts
18
18
  @att_glob_opts = glob_opts.dup
19
19
  @att_glob_opts.delete(:strict)
20
20
  @node = new_node_generator(opts: opts, globals: global_opts)
21
- yield node if block_given?
21
+ node.instance_eval &block if block_given?
22
22
  end
23
23
 
24
24
  def create_node(opts:)
25
25
  node.new(opts: opts)
26
26
  end
27
27
 
28
- def has_attribute(key:, opts:)
29
- opts = override_globals(local_opts: opts)
30
- opts = opts - [:strict]
31
- validator = validation_chain_factory.get_chain(steps: opts)
32
- node.add_validator key: key, validator: validator
33
- end
34
-
35
- # TODO pretty hacky but can be cleaned later
36
- def override_globals(local_opts:)
37
- if local_opts.include?(:not_required) && glob_opts.include?(:required)
38
- (local_opts + (glob_opts - [:required]))
39
- else
40
- local_opts + glob_opts
41
- end
42
- end
43
-
44
- ################ Methods for adding specific attribute types ##############
45
-
46
- def contains_node(key:, opts: [])
47
- generator = new_node_generator(opts: opts)
48
- yield generator if block_given?
49
- node.add_validator key: key, validator: generator.node
50
- end
51
-
52
- def contains_array(key:, opts: [], with_content:)
53
- array_validator = array_validator.new opts: opts, verify_content_as: with_content
54
- yield array_validator if block_given?
55
- node.add_validator key: key, validator: array_validator
56
- end
57
-
58
- def has_schema(key:, name:)
59
- schema = schema_library.get_schema(name: name)
60
- node.add_validator key: key, validator: schema
61
- end
62
-
63
28
  ################ Methods for generating the schema #########################
64
29
 
65
30
  def generate_node
@@ -33,7 +33,7 @@ module EasyJSONMatcher
33
33
  },
34
34
  boolean: ->(value, errors){
35
35
  clazz = value.class
36
- unless (clazz == TrueClass) || (clazz == FalseClass)
36
+ unless [ TrueClass, FalseClass].include? clazz
37
37
  errors << "#{value} is not a Boolean"
38
38
  false
39
39
  end
@@ -1,5 +1,3 @@
1
- require 'easy_json_matcher/json_coercer'
2
-
3
1
  module EasyJSONMatcher
4
2
  class Validator
5
3
  include AutoInject.kwargs[:coercer]
@@ -1,3 +1,3 @@
1
1
  module EasyJSONMatcher
2
- VERSION = "0.3.4".freeze
2
+ VERSION = "0.4.0".freeze
3
3
  end
@@ -7,4 +7,5 @@ module EasyJSONMatcher
7
7
 
8
8
  TYPES = [:number, :object, :value, :string, :boolean, :date, :array]
9
9
 
10
+ IMPORT = Dry::AutoInject(Container)
10
11
  end
@@ -5,8 +5,8 @@ module EasyJSONMatcher
5
5
  describe "Custom Validations" do
6
6
 
7
7
  subject {
8
- SchemaGenerator.new { |sc|
9
- sc.has_attribute key: "val",
8
+ SchemaGenerator.new {
9
+ has_attribute key: "val",
10
10
  opts: [
11
11
  :required,
12
12
  ->(value, errors) { errors << "value was false" unless value === true }
@@ -6,11 +6,11 @@ module EasyJSONMatcher
6
6
 
7
7
 
8
8
  subject {
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]
9
+ SchemaGenerator.new(global_opts: [ :required ]) {
10
+ has_boolean key: "implicitly_required"
11
+ contains_node key: "also_implicitly_required" do
12
+ has_boolean key: "nested_implicitly_required"
13
+ has_boolean key: "not_required", opts: [:not_required]
14
14
  end
15
15
  }.generate_schema
16
16
  }
@@ -6,9 +6,10 @@ module EasyJSONMatcher
6
6
 
7
7
  setup do
8
8
  @name = :test
9
- SchemaGenerator.new { |schema|
10
- schema.has_attribute(key: "name", opts: [ :string, :required ])
11
- }.register(as: @name)
9
+ sc = SchemaGenerator.new {
10
+ has_attribute(key: "name", opts: [ :string, :required ])
11
+ }
12
+ sc.register(as: @name)
12
13
  end
13
14
 
14
15
  test "As a user I want to be able to register a Schema so I can reuse it later" do
@@ -16,12 +17,12 @@ module EasyJSONMatcher
16
17
  end
17
18
 
18
19
  test "The order in which schemas are defined should not matter" do
19
- test_schema = SchemaGenerator.new { |sc|
20
- sc.has_schema key: "lazy", name: :lazily_evaluated
20
+ test_schema = SchemaGenerator.new {
21
+ has_schema key: "lazy", name: :lazily_evaluated
21
22
  }.register as: :outer
22
23
 
23
- lazy = SchemaGenerator.new { |sc|
24
- sc.has_attribute key: "val"
24
+ SchemaGenerator.new {
25
+ has_attribute key: "val"
25
26
  }.register as: :lazily_evaluated
26
27
 
27
28
  valid_json = {
@@ -58,9 +59,9 @@ module EasyJSONMatcher
58
59
  end
59
60
 
60
61
  test "As a user I want to reuse a schema within another schema" do
61
- test_schema = SchemaGenerator.new { |s|
62
- s.has_boolean key: "is_present", opts: [ :required ]
63
- s.has_schema key: @name, name: @name
62
+ test_schema = SchemaGenerator.new {
63
+ has_boolean key: "is_present", opts: [ :required ]
64
+ has_schema key: :test, name: :test
64
65
  }.generate_schema
65
66
 
66
67
  invalid_json = {
@@ -76,17 +77,17 @@ module EasyJSONMatcher
76
77
  end
77
78
 
78
79
  test "It can validate JSON Schema payloads" do
79
- SchemaGenerator.new { |country|
80
- country.has_attribute key: "id", opts: [:number, :required]
81
- country.contains_node(key: "attributes") do |atts|
82
- atts.has_attribute key: "alpha_2", opts: [ :string, :required ]
83
- atts.has_attribute key: "alpha_3", opts: [ :string, :required ]
84
- atts.has_attribute key: "name", opts: [ :string, :required ]
80
+ SchemaGenerator.new {
81
+ has_attribute key: "id", opts: [:number, :required]
82
+ contains_node(key: "attributes") do
83
+ has_attribute key: "alpha_2", opts: [ :string, :required ]
84
+ has_attribute key: "alpha_3", opts: [ :string, :required ]
85
+ has_attribute key: "name", opts: [ :string, :required ]
85
86
  end
86
87
  }.register(as: :country)
87
88
 
88
- country_payload = SchemaGenerator.new {|country_payload|
89
- country_payload.has_schema(key: "data", name: :country)
89
+ country_payload = SchemaGenerator.new {
90
+ has_schema(key: "data", name: :country)
90
91
  }.register(as: :country_payload)
91
92
 
92
93
  valid_json = "{\"data\":{\"id\":\"4376\",\"type\":\"countries\",\"attributes\":{\"alpha_2\":\"GB\",\"alpha_3\":\"GBR\",\"name\":\"United Kingdom of Great Britain and Northern Ireland\"}}}"
@@ -9,8 +9,8 @@ module EasyJSONMatcher
9
9
  describe "Boolean Primitive Test" do
10
10
 
11
11
  before do
12
- @test_schema = SchemaGenerator.new { |g|
13
- g.has_boolean key: "boolean"
12
+ @test_schema = SchemaGenerator.new {
13
+ has_boolean key: "boolean"
14
14
  }.generate_schema
15
15
  end
16
16
 
@@ -10,8 +10,8 @@ module EasyJSONMatcher
10
10
  describe "Date primitive" do
11
11
 
12
12
  before do
13
- @test_schema = SchemaGenerator.new { |g|
14
- g.has_date key: "date"
13
+ @test_schema = SchemaGenerator.new {
14
+ has_date key: "date"
15
15
  }.generate_schema
16
16
  end
17
17
 
@@ -9,8 +9,8 @@ module EasyJSONMatcher
9
9
  describe "Number Validations" do
10
10
 
11
11
  before do
12
- @test_schema = SchemaGenerator.new { |g|
13
- g.has_number key: "number"
12
+ @test_schema = SchemaGenerator.new {
13
+ has_number key: "number"
14
14
  }.generate_schema
15
15
  end
16
16
 
@@ -9,8 +9,8 @@ module EasyJSONMatcher
9
9
  describe "Object primitive test" do
10
10
 
11
11
  before do
12
- @test_schema = SchemaGenerator.new { |g|
13
- g.has_object key: "object"
12
+ @test_schema = SchemaGenerator.new {
13
+ has_object key: "object"
14
14
  }.generate_schema
15
15
  end
16
16
 
@@ -9,8 +9,8 @@ module EasyJSONMatcher
9
9
  describe "Primitive String Validation" do
10
10
 
11
11
  before do
12
- @test_schema = SchemaGenerator.new { |g|
13
- g.has_string key: "string"
12
+ @test_schema = SchemaGenerator.new {
13
+ has_string key: "string"
14
14
  }.generate_schema
15
15
  end
16
16
 
@@ -9,8 +9,8 @@ module EasyJSONMatcher
9
9
  describe "Value primitive test" do
10
10
 
11
11
  before do
12
- @test_schema = SchemaGenerator.new { |g|
13
- g.has_boolean key: :value
12
+ @test_schema = SchemaGenerator.new {
13
+ has_boolean key: :value
14
14
  }.generate_schema
15
15
  end
16
16
 
@@ -3,9 +3,9 @@ require 'test_helper'
3
3
  class RequireValidationTest < ActiveSupport::TestCase
4
4
 
5
5
  test "As a user I want to insist that a value is present" do
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]
6
+ astronaut_schema = EasyJSONMatcher::SchemaGenerator.new {
7
+ has_attribute key: "has_oxygen", opts: [:boolean, :required]
8
+ has_attribute key: "name", opts: [:string]
9
9
  }.generate_schema
10
10
 
11
11
  valid_astronaut = {
@@ -23,9 +23,9 @@ class RequireValidationTest < ActiveSupport::TestCase
23
23
  end
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
- astronaut_schema = EasyJSONMatcher::SchemaGenerator.new { |s|
27
- s.has_attribute key: "has_oxygen", opts: [:boolean]
28
- s.has_attribute key: "name", opts: [:string]
26
+ astronaut_schema = EasyJSONMatcher::SchemaGenerator.new {
27
+ has_attribute key: "has_oxygen", opts: [:boolean]
28
+ has_attribute key: "name", opts: [:string]
29
29
  }.generate_schema
30
30
 
31
31
  valid_astronaut = {
@@ -0,0 +1,29 @@
1
+ require "test_helper"
2
+
3
+ module EasyJSONMatcher
4
+
5
+ describe "Shortened Names" do
6
+
7
+ it "should allow the user to require attributes wihout has_" do
8
+ schema = EasyJSONMatcher::SchemaGenerator.new do |sc|
9
+ sc.boolean key: "bool"
10
+ sc.number key: "num"
11
+ sc.date key: "date"
12
+ sc.object key: "object"
13
+ sc.value key: "val"
14
+ sc.string key: "string"
15
+ end.generate_schema
16
+
17
+ valid = {
18
+ bool: true,
19
+ num: 1,
20
+ date: "2015-01-15",
21
+ object: {},
22
+ value: nil,
23
+ string: "hello"
24
+ }.to_json
25
+
26
+ schema.valid?(candidate: valid).must_be :==, true
27
+ end
28
+ end
29
+ end
@@ -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 ]) {
9
+ has_attribute key: "a", opts: []
10
+ has_attribute key: "b", opts: []
11
11
  }.generate_schema
12
12
  }
13
13
 
@@ -6,24 +6,24 @@ module EasyJSONMatcher
6
6
  describe ArrayValidator do
7
7
 
8
8
  before do
9
- SchemaGenerator.new {|s|
10
- s.has_attribute key: "name", opts: [:string, :required]
11
- s.has_attribute key: "spouse", opts: [:string, :required]
9
+ SchemaGenerator.new {
10
+ has_attribute key: "name", opts: [:string, :required]
11
+ has_attribute key: "spouse", opts: [:string, :required]
12
12
  }.register as: :greek_hero
13
13
  end
14
14
 
15
15
  subject{
16
- test_schema = SchemaGenerator.new {|s|
17
- s.contains_array(key: "data") do |a|
18
- a.elements_should be: [:greek_hero]
16
+ SchemaGenerator.new {
17
+ contains_array(key: "data") do
18
+ elements_should be: [:greek_hero]
19
19
  end
20
20
  }.generate_schema
21
21
  }
22
22
 
23
23
  it "should validate each value in the array" do
24
- validator = SchemaGenerator.new { |s|
25
- s.contains_array(key: "array") do |a|
26
- a.elements_should be: [:number]
24
+ validator = SchemaGenerator.new {
25
+ contains_array(key: "array") do
26
+ elements_should be: [:number]
27
27
  end
28
28
  }.generate_schema
29
29
  candidate = { array: [1,2,3,4,"oops"] }.to_json
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_json_matcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - WJD Hamilton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-22 00:00:00.000000000 Z
11
+ date: 2017-07-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-auto_inject
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.3'
19
+ version: 0.3.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.3'
26
+ version: 0.3.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: dry-container
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.3'
33
+ version: 0.3.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0.3'
40
+ version: 0.3.0
41
41
  description: Test your JSON output in Ruby, with a DSL that makes reasoning about
42
42
  your JSON very straightforward. See the Homepage for docs.
43
43
  email:
@@ -89,6 +89,7 @@ files:
89
89
  - test/primtives_value_test.rb
90
90
  - test/required_validation_test.rb
91
91
  - test/schema_generator_test.rb
92
+ - test/shortened_names_test.rb
92
93
  - test/strict_mode_test.rb
93
94
  - test/test_helper.rb
94
95
  - test/validating_arrays_test.rb
@@ -126,7 +127,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
126
127
  version: '0'
127
128
  requirements: []
128
129
  rubyforge_project:
129
- rubygems_version: 2.5.1
130
+ rubygems_version: 2.6.8
130
131
  signing_key:
131
132
  specification_version: 4
132
133
  summary: Easily test your JSON output with templates and Schemas
@@ -147,6 +148,7 @@ test_files:
147
148
  - test/primtives_value_test.rb
148
149
  - test/required_validation_test.rb
149
150
  - test/schema_generator_test.rb
151
+ - test/shortened_names_test.rb
150
152
  - test/strict_mode_test.rb
151
153
  - test/test_helper.rb
152
154
  - test/validating_arrays_test.rb
@@ -164,4 +166,3 @@ test_files:
164
166
  - test/validation_step_value_test.rb
165
167
  - test/validator_set_test.rb
166
168
  - test/validator_test.rb
167
- has_rdoc: