strong_json 0.0.3 → 0.0.4

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: bf88492fb72b27293db582e0c533f523ebc9847e
4
- data.tar.gz: 6d1fb792714bd945e8d8a587df0ce8f08a7b59a6
3
+ metadata.gz: e4ebb2da5c4e3d195b5141cff335676d87868bf7
4
+ data.tar.gz: 8effc10c56e7208a846bbde39a144d2bb03bb91a
5
5
  SHA512:
6
- metadata.gz: 04dce37728688528ef296cf732d87d4d17785d7d217536298a1ec0788002bd602c4c2ee4ae27621b70ccb197e4c725acf714b5ecdfab58a6bfdc189ab35a455d
7
- data.tar.gz: a26a4cb4f300733f2136183fe4044171761fbe15d9611cf6f92ca6ed6b5c4d445a27b2f61212d53ebb115800ca73f463c0fcf2962398ecc677f9c54012d23f61
6
+ metadata.gz: 377ee5533793e2523b521721dfbb567e63f10077521905a19209b0e71201951fd7a2c9c2a2249fe10bac148df661a696d103ec252f3286cd8b1f672cb9b546a7
7
+ data.tar.gz: de56ca69657851d241233562be8ec5cc95296639e6991be9121fd49826282b8cc485dcd184c0e6c038236a2c7eac584d8bc029df0464a4528a79f36511cf656e
data/README.md CHANGED
@@ -67,6 +67,15 @@ If the input JSON contains `prohibited` attributes, `id` of `item` in the exampl
67
67
  * `any` Any value except `nil` is accepted
68
68
  * `prohibited` Any value will be rejected
69
69
 
70
+ ### Shortcuts
71
+
72
+ There are some alias for `optional(base)`, where base is base types, as the following:
73
+
74
+ * `number?`
75
+ * `string?`
76
+ * `boolean?`
77
+ * `numeric?`
78
+
70
79
  ## Contributing
71
80
 
72
81
  1. Fork it ( https://github.com/soutaro/strong_json/fork )
@@ -1,14 +1,16 @@
1
1
  class StrongJSON
2
2
  module Type
3
+ NONE = ::Object.new
4
+
3
5
  class Base
4
6
  def initialize(type)
5
7
  @type = type
6
8
  end
7
9
 
8
- def test(value)
10
+ def test(value, absent = false)
9
11
  case @type
10
12
  when :prohibited
11
- false
13
+ NONE.equal?(value)
12
14
  when :any
13
15
  true
14
16
  when :number
@@ -40,7 +42,7 @@ class StrongJSON
40
42
  end
41
43
 
42
44
  def coerce(value, path: [])
43
- if value != nil
45
+ unless value == nil || NONE.equal?(value)
44
46
  @type.coerce(value, path: path)
45
47
  else
46
48
  nil
@@ -85,7 +87,7 @@ class StrongJSON
85
87
  result = {}
86
88
 
87
89
  @fields.each do |name, ty|
88
- value = ty.coerce(object[name], path: path + [name])
90
+ value = ty.coerce(object.has_key?(name) ? object[name] : NONE, path: path + [name])
89
91
  result[name] = value if object.has_key?(name)
90
92
  end
91
93
 
@@ -100,6 +102,12 @@ class StrongJSON
100
102
  Object.new(@fields.merge(fields))
101
103
  end
102
104
 
105
+ def except(*keys)
106
+ Object.new(keys.each.with_object(@fields.dup) do |key, hash|
107
+ hash.delete key
108
+ end)
109
+ end
110
+
103
111
  def to_s
104
112
  fields = []
105
113
 
@@ -121,7 +129,8 @@ class StrongJSON
121
129
  end
122
130
 
123
131
  def to_s
124
- "Expected type of value at #{path.join('.')} (#{value}) is #{type}"
132
+ position = " at #{path.join('.')}"
133
+ "Expected type of value #{value}#{position} is #{type}"
125
134
  end
126
135
  end
127
136
  end
@@ -1,3 +1,3 @@
1
1
  class StrongJSON
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -15,10 +15,18 @@ describe StrongJSON::Type::Object do
15
15
  expect(type.coerce(a: 123, b: true)).to eq(a: 123)
16
16
  end
17
17
 
18
- it "rejects prohibited fields" do
19
- type = StrongJSON::Type::Object.new(a: StrongJSON::Type::Base.new(:prohibited))
18
+ describe "prohibited" do
19
+ it "rejects field with any value" do
20
+ type = StrongJSON::Type::Object.new(a: StrongJSON::Type::Base.new(:prohibited))
20
21
 
21
- expect{ type.coerce(a: 123, b: true) }.to raise_error(StrongJSON::Type::Error)
22
+ expect{ type.coerce(a: 123, b: true) }.to raise_error(StrongJSON::Type::Error)
23
+ end
24
+
25
+ it "accepts if it does not contains the field" do
26
+ type = StrongJSON::Type::Object.new(a: StrongJSON::Type::Base.new(:prohibited))
27
+
28
+ expect(type.coerce(b: true)).to eq({})
29
+ end
22
30
  end
23
31
 
24
32
  it "rejects objects with missing fields" do
@@ -49,4 +57,14 @@ describe StrongJSON::Type::Object do
49
57
  expect{ ty2.coerce(a: 123) }.to raise_error(StrongJSON::Type::Error)
50
58
  end
51
59
  end
60
+
61
+ describe "#except" do
62
+ let (:type) { StrongJSON::Type::Object.new(a: StrongJSON::Type::Base.new(:numeric), b: StrongJSON::Type::Base.new(:string)) }
63
+
64
+ it "return object which ignores given fields but preserve others" do
65
+ ty2 = type.except(:a)
66
+
67
+ expect(ty2.coerce(a: 123, b: "test")).to eq({ b: "test" })
68
+ end
69
+ end
52
70
  end
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.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Soutaro Matsumoto