strong_json 0.9.0 → 1.0.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 +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +68 -16
- data/lib/strong_json/error_reporter.rb +130 -0
- data/lib/strong_json/type.rb +232 -86
- data/lib/strong_json/types.rb +11 -14
- data/lib/strong_json/version.rb +1 -1
- data/lib/strong_json.rb +3 -1
- data/sig/strong_json.rbi +25 -6
- data/sig/type.rbi +55 -28
- data/spec/array_spec.rb +3 -3
- data/spec/basetype_spec.rb +16 -8
- data/spec/enum_spec.rb +87 -9
- data/spec/error_spec.rb +62 -4
- data/spec/json_spec.rb +133 -7
- data/spec/object_spec.rb +122 -57
- data/spec/optional_spec.rb +4 -1
- metadata +3 -2
data/spec/object_spec.rb
CHANGED
@@ -3,93 +3,158 @@ require "strong_json"
|
|
3
3
|
describe StrongJSON::Type::Object do
|
4
4
|
describe "#coerce" do
|
5
5
|
it "accepts value" do
|
6
|
-
type = StrongJSON::Type::Object.new(
|
7
|
-
|
6
|
+
type = StrongJSON::Type::Object.new(
|
7
|
+
{
|
8
|
+
a: StrongJSON::Type::Base.new(:numeric),
|
9
|
+
b: StrongJSON::Type::Base.new(:string)
|
10
|
+
},
|
11
|
+
ignored_attributes: nil,
|
12
|
+
prohibited_attributes: Set.new
|
13
|
+
)
|
8
14
|
|
9
15
|
expect(type.coerce(a: 123, b: "test")).to eq(a: 123, b: "test")
|
10
16
|
end
|
11
17
|
|
12
18
|
it "rejects unspecified fields" do
|
13
|
-
type = StrongJSON::Type::Object.new(
|
19
|
+
type = StrongJSON::Type::Object.new(
|
20
|
+
{
|
21
|
+
a: StrongJSON::Type::Base.new(:numeric)
|
22
|
+
},
|
23
|
+
ignored_attributes: nil,
|
24
|
+
prohibited_attributes: Set.new
|
25
|
+
)
|
26
|
+
|
27
|
+
expect { type.coerce(a:123, b:true) }.to raise_error(StrongJSON::Type::UnexpectedAttributeError) {|e|
|
28
|
+
expect(e.path.to_s).to eq("$")
|
29
|
+
expect(e.attribute).to eq(:b)
|
30
|
+
}
|
31
|
+
end
|
14
32
|
|
15
|
-
|
33
|
+
it "rejects objects with missing fields" do
|
34
|
+
type = StrongJSON::Type::Object.new(
|
35
|
+
{
|
36
|
+
a: StrongJSON::Type::Base.new(:numeric)
|
37
|
+
},
|
38
|
+
ignored_attributes: nil,
|
39
|
+
prohibited_attributes: Set.new
|
40
|
+
)
|
41
|
+
|
42
|
+
expect{ type.coerce(b: "test") }.to raise_error(StrongJSON::Type::UnexpectedAttributeError) {|e|
|
43
|
+
expect(e.path.to_s).to eq("$")
|
44
|
+
expect(e.attribute).to eq(:b)
|
45
|
+
}
|
16
46
|
end
|
17
47
|
|
18
|
-
describe "
|
19
|
-
|
20
|
-
|
21
|
-
|
48
|
+
describe "ignored_attributes" do
|
49
|
+
context "when ignored_attributes are given as Set" do
|
50
|
+
let(:type) {
|
51
|
+
StrongJSON::Type::Object.new(
|
52
|
+
{
|
53
|
+
a: StrongJSON::Type::Base.new(:numeric)
|
54
|
+
},
|
55
|
+
ignored_attributes: Set.new([:b]),
|
56
|
+
prohibited_attributes: Set.new
|
57
|
+
)
|
58
|
+
}
|
59
|
+
|
60
|
+
it "ignores field with any value" do
|
61
|
+
expect(type.coerce(a: 123, b: true)).to eq(a: 123)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "accepts if it does not contains the field" do
|
65
|
+
expect(type.coerce(a: 123)).to eq(a: 123)
|
66
|
+
end
|
22
67
|
end
|
23
68
|
|
24
|
-
|
25
|
-
|
26
|
-
|
69
|
+
context "when ignored_attributes is nil" do
|
70
|
+
let(:type) {
|
71
|
+
StrongJSON::Type::Object.new(
|
72
|
+
{
|
73
|
+
a: StrongJSON::Type::Base.new(:numeric)
|
74
|
+
},
|
75
|
+
ignored_attributes: nil,
|
76
|
+
prohibited_attributes: Set.new
|
77
|
+
)
|
78
|
+
}
|
79
|
+
|
80
|
+
it "ignores field with any value" do
|
81
|
+
expect {
|
82
|
+
type.coerce(a: 123, b: true)
|
83
|
+
}.to raise_error(StrongJSON::Type::UnexpectedAttributeError)
|
84
|
+
end
|
27
85
|
end
|
28
|
-
end
|
29
86
|
|
30
|
-
|
31
|
-
|
87
|
+
context "when ignored_attributes is :any" do
|
88
|
+
let(:type) {
|
89
|
+
StrongJSON::Type::Object.new(
|
90
|
+
{
|
91
|
+
a: StrongJSON::Type::Base.new(:numeric)
|
92
|
+
},
|
93
|
+
ignored_attributes: :any,
|
94
|
+
prohibited_attributes: Set.new
|
95
|
+
)
|
96
|
+
}
|
97
|
+
|
98
|
+
it "ignores field with any value" do
|
99
|
+
expect(type.coerce(a: 123, b: true)).to eq(a: 123)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
32
103
|
|
33
|
-
|
104
|
+
describe "prohibited_attributes" do
|
105
|
+
let(:type) {
|
106
|
+
StrongJSON::Type::Object.new(
|
107
|
+
{
|
108
|
+
a: StrongJSON::Type::Base.new(:numeric)
|
109
|
+
},
|
110
|
+
ignored_attributes: :any,
|
111
|
+
prohibited_attributes: Set.new([:x])
|
112
|
+
)
|
113
|
+
}
|
114
|
+
|
115
|
+
it "raises error if the attribute is given" do
|
116
|
+
expect {
|
117
|
+
type.coerce(a:123, b:true, x: [])
|
118
|
+
}.to raise_error(StrongJSON::Type::UnexpectedAttributeError)
|
119
|
+
end
|
34
120
|
end
|
35
121
|
end
|
36
122
|
|
37
123
|
describe "optional" do
|
124
|
+
let(:type) {
|
125
|
+
StrongJSON::Type::Object.new(
|
126
|
+
{
|
127
|
+
a: StrongJSON::Type::Optional.new(StrongJSON::Type::Base.new(:numeric))
|
128
|
+
},
|
129
|
+
ignored_attributes: nil,
|
130
|
+
prohibited_attributes: Set.new
|
131
|
+
)
|
132
|
+
}
|
133
|
+
|
38
134
|
it "accepts missing field if optional" do
|
39
|
-
type
|
40
|
-
expect(type.coerce({})).to eq({})
|
135
|
+
expect(type.coerce({})).to eq(a: nil)
|
41
136
|
end
|
42
137
|
|
43
138
|
it "preserves if present" do
|
44
|
-
type = StrongJSON::Type::Object.new(a: StrongJSON::Type::Optional.new(StrongJSON::Type::Base.new(:numeric)))
|
45
139
|
expect(type.coerce({ a: "-123" })).to eq({ a: "-123" })
|
46
140
|
end
|
47
141
|
|
48
142
|
it "preserves nil if present" do
|
49
|
-
type = StrongJSON::Type::Object.new(a: StrongJSON::Type::Optional.new(StrongJSON::Type::Base.new(:numeric)))
|
50
143
|
expect(type.coerce({ a: nil })).to eq({ a: nil })
|
51
144
|
end
|
52
145
|
end
|
53
146
|
|
54
|
-
describe "#merge" do
|
55
|
-
let (:type) { StrongJSON::Type::Object.new(a: StrongJSON::Type::Base.new(:numeric)) }
|
56
|
-
|
57
|
-
it "adds field" do
|
58
|
-
ty2 = type.merge(b: StrongJSON::Type::Base.new(:string))
|
59
|
-
|
60
|
-
expect(ty2.coerce(a: 123, b: "test")).to eq(a: 123, b: "test")
|
61
|
-
end
|
62
|
-
|
63
|
-
it "overrides field" do
|
64
|
-
ty2 = type.merge(a: StrongJSON::Type::Base.new(:prohibited))
|
65
|
-
|
66
|
-
expect{ ty2.coerce(a: 123) }.to raise_error(StrongJSON::Type::Error)
|
67
|
-
end
|
68
|
-
|
69
|
-
it "adds field via object" do
|
70
|
-
ty2 = type.merge(StrongJSON::Type::Object.new(b: StrongJSON::Type::Base.new(:string)))
|
71
|
-
|
72
|
-
expect(ty2.coerce(a: 123, b: "test")).to eq(a: 123, b: "test")
|
73
|
-
end
|
74
|
-
|
75
|
-
it "overrides field via object" do
|
76
|
-
ty2 = type.merge(StrongJSON::Type::Object.new(a: StrongJSON::Type::Base.new(:prohibited)))
|
77
|
-
|
78
|
-
expect{ ty2.coerce(a: 123) }.to raise_error(StrongJSON::Type::Error)
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
describe "#except" do
|
83
|
-
let (:type) { StrongJSON::Type::Object.new(a: StrongJSON::Type::Base.new(:numeric), b: StrongJSON::Type::Base.new(:string)) }
|
84
|
-
|
85
|
-
it "return object which ignores given fields but preserve others" do
|
86
|
-
ty2 = type.except(:a)
|
87
|
-
expect(ty2.coerce(b: "test")).to eq({ b: "test" })
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
147
|
describe "=~" do
|
92
|
-
let (:type) {
|
148
|
+
let (:type) {
|
149
|
+
StrongJSON::Type::Object.new(
|
150
|
+
{
|
151
|
+
a: StrongJSON::Type::Base.new(:numeric),
|
152
|
+
b: StrongJSON::Type::Base.new(:string)
|
153
|
+
},
|
154
|
+
ignored_attributes: nil,
|
155
|
+
prohibited_attributes: Set.new
|
156
|
+
)
|
157
|
+
}
|
93
158
|
|
94
159
|
it "returns true for valid object" do
|
95
160
|
expect(type =~ { a: 3, b: "foo" }).to be_truthy
|
data/spec/optional_spec.rb
CHANGED
@@ -13,7 +13,10 @@ describe StrongJSON::Type::Optional, "#coerce" do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
it "rejects string" do
|
16
|
-
expect { type.coerce("a") }.to raise_error(StrongJSON::Type::
|
16
|
+
expect { type.coerce("a") }.to raise_error(StrongJSON::Type::TypeError) {|e|
|
17
|
+
expect(e.path.to_s).to eq("$")
|
18
|
+
expect(e.type).to be_a(StrongJSON::Type::Base)
|
19
|
+
}
|
17
20
|
end
|
18
21
|
end
|
19
22
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: strong_json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Soutaro Matsumoto
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-05-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -84,6 +84,7 @@ files:
|
|
84
84
|
- example/example.rb
|
85
85
|
- example/example.rbi
|
86
86
|
- lib/strong_json.rb
|
87
|
+
- lib/strong_json/error_reporter.rb
|
87
88
|
- lib/strong_json/type.rb
|
88
89
|
- lib/strong_json/types.rb
|
89
90
|
- lib/strong_json/version.rb
|