strong_json 0.1.1 → 0.1.2

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: d8c22cee2e96c616585aab8e4c116daa4e64944a
4
- data.tar.gz: 0b52394883010d69ffa7cfc65107f2b7bd381987
3
+ metadata.gz: 4a009e9385e3e0d6dceed251b6951e533d8d9cae
4
+ data.tar.gz: 26c8e89826ebe9b88b325c647c662d7b89013ddf
5
5
  SHA512:
6
- metadata.gz: 553c80f3d3c1941cf154b852662cbc462b523e77e11a2d49c12d98847ec412bf17a200c3756bb31f165c3e1c84759a278e4e65de90bff9f0db1998c693796561
7
- data.tar.gz: 55fcd6bbb18191de401fc6aa9d29b23d0c0a07a5d217aebe3db7b477959602d22ae7670f744ae75c686b1ac3eaf25168c957caefc58ad08fcd4ea2f0ce4182da
6
+ metadata.gz: d597908375f4112ee71f931f60a4c489ead56ae2c56c31fac8f185375aad38539ded3b846e8ce4207d32641f4641cccb244dc28ee8dcf20487f0dfb12108de5b
7
+ data.tar.gz: 643ca3880954ba6fd3f5ac9654313d604744c50f79142af703364281d1aab35d406b3610fced4b1ef0a79fc5a711d95fb03887c22cb7f77294d687b9d6898c79
data/README.md CHANGED
@@ -75,6 +75,11 @@ There are some alias for `optional(base)`, where base is base types, as the foll
75
75
  * `boolean?`
76
76
  * `numeric?`
77
77
 
78
+ Shorthands for `optional(array(ty))` and `optional(object(fields))` are also defined as the following:
79
+
80
+ * `array?(ty)`
81
+ * `object?(fields)`
82
+
78
83
  ## Contributing
79
84
 
80
85
  1. Fork it ( https://github.com/soutaro/strong_json/fork )
@@ -110,7 +110,7 @@ class StrongJSON
110
110
  v = type.coerce(value, path: path)
111
111
 
112
112
  return if NONE.equal?(v) || NONE.equal?(type)
113
- return if type.is_a?(Optional) && v == nil
113
+ return if type.is_a?(Optional) && NONE.equal?(value)
114
114
 
115
115
  yield(v)
116
116
  end
@@ -55,5 +55,13 @@ class StrongJSON
55
55
  def ignored
56
56
  StrongJSON::Type::Base.new(:ignored)
57
57
  end
58
+
59
+ def array?(ty)
60
+ optional(array(ty))
61
+ end
62
+
63
+ def object?(fields)
64
+ optional(object(fields))
65
+ end
58
66
  end
59
67
  end
@@ -1,3 +1,3 @@
1
1
  class StrongJSON
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/spec/object_spec.rb CHANGED
@@ -32,12 +32,23 @@ describe StrongJSON::Type::Object do
32
32
 
33
33
  expect{ type.coerce(b: "test") }.to raise_error(StrongJSON::Type::Error)
34
34
  end
35
+ end
35
36
 
37
+ describe "optional" do
36
38
  it "accepts missing field if optional" do
37
39
  type = StrongJSON::Type::Object.new(a: StrongJSON::Type::Optional.new(StrongJSON::Type::Base.new(:numeric)))
38
-
39
40
  expect(type.coerce({})).to eq({})
40
41
  end
42
+
43
+ it "preserves if present" do
44
+ type = StrongJSON::Type::Object.new(a: StrongJSON::Type::Optional.new(StrongJSON::Type::Base.new(:numeric)))
45
+ expect(type.coerce({ a: "-123" })).to eq({ a: "-123" })
46
+ end
47
+
48
+ it "preserves nil if present" do
49
+ type = StrongJSON::Type::Object.new(a: StrongJSON::Type::Optional.new(StrongJSON::Type::Base.new(:numeric)))
50
+ expect(type.coerce({ a: nil })).to eq({ a: nil })
51
+ end
41
52
  end
42
53
 
43
54
  describe "#merge" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strong_json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Soutaro Matsumoto
@@ -96,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
96
  version: '0'
97
97
  requirements: []
98
98
  rubyforge_project:
99
- rubygems_version: 2.4.5
99
+ rubygems_version: 2.2.2
100
100
  signing_key:
101
101
  specification_version: 4
102
102
  summary: Type check JSON objects