easy_json_matcher 0.3.4 → 0.4.0
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/array_generator.rb +0 -2
- data/lib/easy_json_matcher/attribute_type_methods.rb +8 -1
- data/lib/easy_json_matcher/node.rb +0 -2
- data/lib/easy_json_matcher/node_generator.rb +4 -10
- data/lib/easy_json_matcher/schema_generator.rb +2 -37
- data/lib/easy_json_matcher/validation_rules.rb +1 -1
- data/lib/easy_json_matcher/validator.rb +0 -2
- data/lib/easy_json_matcher/version.rb +1 -1
- data/lib/easy_json_matcher.rb +1 -0
- data/test/custom_validations_test.rb +2 -2
- data/test/global_validation_options_test.rb +5 -5
- data/test/managing_schemas_test.rb +19 -18
- data/test/primitives_boolean_test.rb +2 -2
- data/test/primitives_date_test.rb +2 -2
- data/test/primitives_number_test.rb +2 -2
- data/test/primitives_object_test.rb +2 -2
- data/test/primitives_string_test.rb +2 -2
- data/test/primtives_value_test.rb +2 -2
- data/test/required_validation_test.rb +6 -6
- data/test/shortened_names_test.rb +29 -0
- data/test/strict_mode_test.rb +3 -3
- data/test/validating_arrays_test.rb +9 -9
- metadata +9 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f5594d2bf04fe24083c5ffe568e88f22accbee84
|
4
|
+
data.tar.gz: 19de26378a180c35d80ffc5c21fa4cff5064ceb5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9934c3a54200cc1b4ceb0de8a25ef45509445df8330820db4324270967437038ec48964a7bc91fb9be4c0e2a86bf96ead52e0209d1e0fbb15185632202073713
|
7
|
+
data.tar.gz: 44fb3187668459fd709e3a916668411f1f7749ebcad47d32d717ec870fa983bfd800557d5be2e1b2d98343788d44ede157fce4317a7387e40bcee7922bc188ba
|
@@ -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,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
|
-
|
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
|
-
|
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
|
data/lib/easy_json_matcher.rb
CHANGED
@@ -5,8 +5,8 @@ module EasyJSONMatcher
|
|
5
5
|
describe "Custom Validations" do
|
6
6
|
|
7
7
|
subject {
|
8
|
-
SchemaGenerator.new {
|
9
|
-
|
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 ]) {
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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 {
|
10
|
-
|
11
|
-
}
|
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 {
|
20
|
-
|
20
|
+
test_schema = SchemaGenerator.new {
|
21
|
+
has_schema key: "lazy", name: :lazily_evaluated
|
21
22
|
}.register as: :outer
|
22
23
|
|
23
|
-
|
24
|
-
|
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 {
|
62
|
-
|
63
|
-
|
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 {
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
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 {
|
89
|
-
|
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\"}}}"
|
@@ -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 {
|
7
|
-
|
8
|
-
|
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 {
|
27
|
-
|
28
|
-
|
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
|
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: [ :strict ]) {
|
9
|
-
|
10
|
-
|
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 {
|
10
|
-
|
11
|
-
|
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
|
-
|
17
|
-
|
18
|
-
|
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 {
|
25
|
-
|
26
|
-
|
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.
|
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:
|
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:
|
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:
|
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:
|
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:
|
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.
|
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:
|