strong_json 0.3.0 → 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 +4 -4
- data/README.md +21 -1
- data/lib/strong_json/type.rb +8 -8
- data/lib/strong_json/version.rb +1 -1
- data/spec/object_spec.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 11c5dbfb77d22e1c192653aec8a65425fc89037a
|
4
|
+
data.tar.gz: 4c0a5335c72ee7dbe8197f88d00bf695d8175255
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6ad64a761127eafdae6dc349a6afa3940525fac50e7e6300ef8ecb1346b3fef065e9c84646b95853398e137a7c4db183a07bd3ea1c1c28abbd2916ef324144fd
|
7
|
+
data.tar.gz: 937d1ef703bc65838c4d02c357ff5d96ee963f40b1fc22f3f3add66d61b127646df023b8700c504de3630385ac08fe19faa572fdf627fca40a3e300f1447704b
|
data/README.md
CHANGED
@@ -30,7 +30,7 @@ s = StrongJSON.new do
|
|
30
30
|
let :order, object(customer: customer, items: array(item))
|
31
31
|
end
|
32
32
|
|
33
|
-
json = s.order.coerce(JSON.parse(input
|
33
|
+
json = s.order.coerce(JSON.parse(input, symbolize_names: true))
|
34
34
|
s.order =~ JSON.parse(input, symbolize_names: true)
|
35
35
|
|
36
36
|
case JSON.parse(input2, symbolize_names: true)
|
@@ -57,6 +57,26 @@ If an attribute has a value which does not match with given type, the `coerce` m
|
|
57
57
|
* Fields, `f1`, `f2`, and ..., must be present and its values must be of `type1`, `type2`, ..., respectively
|
58
58
|
* Objects with other fields will be rejected
|
59
59
|
|
60
|
+
#### Performance hint
|
61
|
+
|
62
|
+
Object attributes test is done in order of the keys.
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
slower_object = enum(
|
66
|
+
object(id: numeric, created_at: string, updated_at: string, type: literal("person"), name: string),
|
67
|
+
object(id: numeric, created_at: string, updated_at: string, type: literal("food"), object: any)
|
68
|
+
)
|
69
|
+
|
70
|
+
faster_object = enum(
|
71
|
+
object(type: literal("person"), id: numeric, created_at: string, updated_at: string, name: string),
|
72
|
+
object(type: literal("food"), id: numeric, created_at: string, updated_at: string, object: any)
|
73
|
+
)
|
74
|
+
```
|
75
|
+
|
76
|
+
The two enums represents same object, but testing runs faster with `faster_object`.
|
77
|
+
Objects in `faster_object` have `type` attribute as their first keys.
|
78
|
+
Testing `type` is done first, and it soon determines if the object is `"person"` or `"food"`.
|
79
|
+
|
60
80
|
### array(type)
|
61
81
|
|
62
82
|
* The value must be an array
|
data/lib/strong_json/type.rb
CHANGED
@@ -139,10 +139,14 @@ class StrongJSON
|
|
139
139
|
|
140
140
|
result = {}
|
141
141
|
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
142
|
+
object.each do |key, value|
|
143
|
+
unless @fields.key?(key)
|
144
|
+
raise UnexpectedFieldError.new(path: path + [key], value: value)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
@fields.each do |key, type|
|
149
|
+
value = object.key?(key) ? object[key] : NONE
|
146
150
|
|
147
151
|
test_value_type(path + [key], type, value) do |v|
|
148
152
|
result[key] = v
|
@@ -153,10 +157,6 @@ class StrongJSON
|
|
153
157
|
end
|
154
158
|
|
155
159
|
def test_value_type(path, type, value)
|
156
|
-
if NONE.equal?(type) && !NONE.equal?(value)
|
157
|
-
raise UnexpectedFieldError.new(path: path, value: value)
|
158
|
-
end
|
159
|
-
|
160
160
|
v = type.coerce(value, path: path)
|
161
161
|
|
162
162
|
return if NONE.equal?(v) || NONE.equal?(type)
|
data/lib/strong_json/version.rb
CHANGED
data/spec/object_spec.rb
CHANGED
@@ -30,7 +30,7 @@ describe StrongJSON::Type::Object do
|
|
30
30
|
it "rejects objects with missing fields" do
|
31
31
|
type = StrongJSON::Type::Object.new(a: StrongJSON::Type::Base.new(:numeric))
|
32
32
|
|
33
|
-
expect{ type.coerce(b: "test") }.to raise_error(StrongJSON::Type::
|
33
|
+
expect{ type.coerce(b: "test") }.to raise_error(StrongJSON::Type::UnexpectedFieldError)
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
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: 0.4.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: 2017-05-
|
11
|
+
date: 2017-05-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|