thrift-validator 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 +4 -4
- data/lib/thrift/validator.rb +5 -1
- data/lib/thrift/validator/version.rb +1 -1
- data/test.thrift +5 -0
- data/test/acceptance_test.rb +31 -0
- data/vendor/gen-rb/test_types.rb +29 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7d2072d19fcac9ac4d9e44efc3d1a9b360fde931
|
4
|
+
data.tar.gz: 861205ed86104587b4a8fe319f5771c95f9733a4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b038a575a75faeb0b986f76ce8dfb265d8a31bcdc45d80cd51fed0ff459708096b2ecae3b0ec8cec266b70d441bfff634c4ab141087ff37ade1cae552386f583
|
7
|
+
data.tar.gz: 05ee95200e54ff7a6a027a58d160b2fecc15b341e80f0480a9cae6856f1278b9b1383ea25ed922df99ce6c708a74e3cf31f1ffc8ea134e1925888d80685534e0
|
data/lib/thrift/validator.rb
CHANGED
@@ -10,7 +10,11 @@ module Thrift
|
|
10
10
|
type, name = field.fetch(:type), field.fetch(:name)
|
11
11
|
case type
|
12
12
|
when Types::STRUCT
|
13
|
-
|
13
|
+
next if source.kind_of?(Union) && source.get_set_field.to_s != name
|
14
|
+
|
15
|
+
if source.send(name)
|
16
|
+
validate source.send(name)
|
17
|
+
end
|
14
18
|
when Types::LIST, Types::SET
|
15
19
|
if recurse? field.fetch(:element)
|
16
20
|
Array(source.send(name)).each do |item|
|
data/test.thrift
CHANGED
data/test/acceptance_test.rb
CHANGED
@@ -26,6 +26,12 @@ class AcceptanceTest < MiniTest::Unit::TestCase
|
|
26
26
|
assert_valid struct
|
27
27
|
end
|
28
28
|
|
29
|
+
def test_passes_if_nested_optional_struct_is_omitted
|
30
|
+
struct = NestedExample.new
|
31
|
+
struct.required_struct = SimpleStruct.new required_string: 'foo'
|
32
|
+
assert_valid struct
|
33
|
+
end
|
34
|
+
|
29
35
|
def test_fails_if_a_nested_list_item_is_invalid
|
30
36
|
struct = ListExample.new
|
31
37
|
struct.required_list = [ SimpleStruct.new ]
|
@@ -118,6 +124,31 @@ class AcceptanceTest < MiniTest::Unit::TestCase
|
|
118
124
|
assert_valid struct
|
119
125
|
end
|
120
126
|
|
127
|
+
def test_fails_if_no_union_fields_set
|
128
|
+
union = UnionExample.new
|
129
|
+
assert_raises(StandardError) do
|
130
|
+
Thrift::Validator.new.validate(union)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_fails_if_union_set_field_is_invalid
|
135
|
+
union = UnionExample.new
|
136
|
+
union.primary = SimpleStruct.new
|
137
|
+
refute_valid union
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_passes_if_union_set_field_is_valid
|
141
|
+
union = UnionExample.new
|
142
|
+
union.primary = SimpleStruct.new required_string: 'foo'
|
143
|
+
assert_valid union
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_passes_if_unions_alternate_value_is_provided
|
147
|
+
union = UnionExample.new
|
148
|
+
union.alternate = 'foo'
|
149
|
+
assert_valid union
|
150
|
+
end
|
151
|
+
|
121
152
|
private
|
122
153
|
|
123
154
|
def refute_valid(struct)
|
data/vendor/gen-rb/test_types.rb
CHANGED
@@ -154,3 +154,32 @@ class MapValueExample
|
|
154
154
|
::Thrift::Struct.generate_accessors self
|
155
155
|
end
|
156
156
|
|
157
|
+
class UnionExample < ::Thrift::Union
|
158
|
+
include ::Thrift::Struct_Union
|
159
|
+
class << self
|
160
|
+
def primary(val)
|
161
|
+
UnionExample.new(:primary, val)
|
162
|
+
end
|
163
|
+
|
164
|
+
def alternate(val)
|
165
|
+
UnionExample.new(:alternate, val)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
PRIMARY = 1
|
170
|
+
ALTERNATE = 2
|
171
|
+
|
172
|
+
FIELDS = {
|
173
|
+
PRIMARY => {:type => ::Thrift::Types::STRUCT, :name => 'primary', :class => ::SimpleStruct},
|
174
|
+
ALTERNATE => {:type => ::Thrift::Types::STRING, :name => 'alternate'}
|
175
|
+
}
|
176
|
+
|
177
|
+
def struct_fields; FIELDS; end
|
178
|
+
|
179
|
+
def validate
|
180
|
+
raise(StandardError, 'Union fields are not set.') if get_set_field.nil? || get_value.nil?
|
181
|
+
end
|
182
|
+
|
183
|
+
::Thrift::Union.generate_accessors self
|
184
|
+
end
|
185
|
+
|