membrane 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/.travis.yml +6 -0
- data/LICENSE +58 -6869
- data/NOTICE +10 -0
- data/README.md +130 -30
- data/lib/membrane/schema_parser.rb +25 -24
- data/lib/membrane/schemas/any.rb +8 -0
- data/lib/membrane/{schema → schemas}/base.rb +1 -6
- data/lib/membrane/schemas/bool.rb +31 -0
- data/lib/membrane/schemas/class.rb +35 -0
- data/lib/membrane/schemas/dictionary.rb +69 -0
- data/lib/membrane/schemas/enum.rb +49 -0
- data/lib/membrane/schemas/list.rb +63 -0
- data/lib/membrane/schemas/record.rb +96 -0
- data/lib/membrane/schemas/regexp.rb +60 -0
- data/lib/membrane/schemas/tuple.rb +90 -0
- data/lib/membrane/schemas/value.rb +37 -0
- data/lib/membrane/schemas.rb +5 -0
- data/lib/membrane/version.rb +1 -1
- data/lib/membrane.rb +1 -1
- data/spec/schema_parser_spec.rb +79 -75
- data/spec/{any_schema_spec.rb → schemas/any_spec.rb} +2 -2
- data/spec/{base_schema_spec.rb → schemas/base_spec.rb} +2 -2
- data/spec/{bool_schema_spec.rb → schemas/bool_spec.rb} +2 -3
- data/spec/{class_schema_spec.rb → schemas/class_spec.rb} +2 -2
- data/spec/{dictionary_schema_spec.rb → schemas/dictionary_spec.rb} +11 -11
- data/spec/{enum_schema_spec.rb → schemas/enum_spec.rb} +4 -4
- data/spec/{list_schema_spec.rb → schemas/list_spec.rb} +8 -8
- data/spec/schemas/record_spec.rb +108 -0
- data/spec/{regexp_schema_spec.rb → schemas/regexp_spec.rb} +2 -2
- data/spec/{tuple_schema_spec.rb → schemas/tuple_spec.rb} +4 -4
- data/spec/{value_schema_spec.rb → schemas/value_spec.rb} +2 -2
- metadata +37 -35
- data/lib/membrane/schema/any.rb +0 -13
- data/lib/membrane/schema/bool.rb +0 -20
- data/lib/membrane/schema/class.rb +0 -24
- data/lib/membrane/schema/dictionary.rb +0 -40
- data/lib/membrane/schema/enum.rb +0 -30
- data/lib/membrane/schema/list.rb +0 -37
- data/lib/membrane/schema/record.rb +0 -45
- data/lib/membrane/schema/regexp.rb +0 -29
- data/lib/membrane/schema/tuple.rb +0 -48
- data/lib/membrane/schema/value.rb +0 -22
- data/lib/membrane/schema.rb +0 -17
- data/spec/record_schema_spec.rb +0 -56
@@ -0,0 +1,60 @@
|
|
1
|
+
require "membrane/errors"
|
2
|
+
require "membrane/schemas/base"
|
3
|
+
|
4
|
+
module Membrane
|
5
|
+
module Schema
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class Membrane::Schemas::Regexp < Membrane::Schemas::Base
|
10
|
+
attr_reader :regexp
|
11
|
+
|
12
|
+
def initialize(regexp)
|
13
|
+
@regexp = regexp
|
14
|
+
end
|
15
|
+
|
16
|
+
def validate(object)
|
17
|
+
StringValidator.new(object).validate
|
18
|
+
MatchValidator.new(@regexp, object).validate
|
19
|
+
|
20
|
+
nil
|
21
|
+
end
|
22
|
+
|
23
|
+
class StringValidator
|
24
|
+
|
25
|
+
def initialize(object)
|
26
|
+
@object = object
|
27
|
+
end
|
28
|
+
|
29
|
+
def validate
|
30
|
+
fail! if !@object.kind_of?(String)
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def fail!
|
36
|
+
emsg = "Expected instance of String, given instance of #{@object.class}"
|
37
|
+
raise Membrane::SchemaValidationError.new(emsg)
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
class MatchValidator
|
43
|
+
|
44
|
+
def initialize(regexp, object)
|
45
|
+
@regexp = regexp
|
46
|
+
@object = object
|
47
|
+
end
|
48
|
+
|
49
|
+
def validate
|
50
|
+
fail! if !@regexp.match(@object)
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def fail!
|
56
|
+
emsg = "Value #{@object} doesn't match regexp #{@regexp}"
|
57
|
+
raise Membrane::SchemaValidationError.new(emsg)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require "membrane/errors"
|
2
|
+
require "membrane/schemas/base"
|
3
|
+
|
4
|
+
module Membrane
|
5
|
+
module Schema
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class Membrane::Schemas::Tuple < Membrane::Schemas::Base
|
10
|
+
attr_reader :elem_schemas
|
11
|
+
|
12
|
+
def initialize(*elem_schemas)
|
13
|
+
@elem_schemas = elem_schemas
|
14
|
+
end
|
15
|
+
|
16
|
+
def validate(object)
|
17
|
+
ArrayValidator.new(object).validate
|
18
|
+
LengthValidator.new(@elem_schemas, object).validate
|
19
|
+
MemberValidator.new(@elem_schemas, object).validate
|
20
|
+
|
21
|
+
nil
|
22
|
+
end
|
23
|
+
|
24
|
+
class ArrayValidator
|
25
|
+
def initialize(object)
|
26
|
+
@object = object
|
27
|
+
end
|
28
|
+
|
29
|
+
def validate
|
30
|
+
fail! if !@object.kind_of?(Array)
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def fail!
|
36
|
+
emsg = "Expected instance of Array, given instance of #{@object.class}"
|
37
|
+
raise Membrane::SchemaValidationError.new(emsg)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class LengthValidator
|
42
|
+
def initialize(elem_schemas, object)
|
43
|
+
@elem_schemas = elem_schemas
|
44
|
+
@object = object
|
45
|
+
end
|
46
|
+
|
47
|
+
def validate
|
48
|
+
expected = @elem_schemas.length
|
49
|
+
actual = @object.length
|
50
|
+
|
51
|
+
fail!(expected, actual) if actual != expected
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def fail!(expected, actual)
|
57
|
+
emsg = "Expected #{expected} element(s), given #{actual}"
|
58
|
+
raise Membrane::SchemaValidationError.new(emsg)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
class MemberValidator
|
63
|
+
def initialize(elem_schemas, object)
|
64
|
+
@elem_schemas = elem_schemas
|
65
|
+
@object = object
|
66
|
+
end
|
67
|
+
|
68
|
+
def validate
|
69
|
+
errors = {}
|
70
|
+
|
71
|
+
@elem_schemas.each_with_index do |schema, ii|
|
72
|
+
begin
|
73
|
+
schema.validate(@object[ii])
|
74
|
+
rescue Membrane::SchemaValidationError => e
|
75
|
+
errors[ii] = e
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
fail!(errors) if errors.size > 0
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def fail!(errors)
|
85
|
+
emsg = "There were errors at the following indices: " \
|
86
|
+
+ errors.map { |ii, err| "#{ii} => #{err}" }.join(", ")
|
87
|
+
raise Membrane::SchemaValidationError.new(emsg)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require "membrane/errors"
|
2
|
+
require "membrane/schemas/base"
|
3
|
+
|
4
|
+
module Membrane
|
5
|
+
module Schema
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class Membrane::Schemas::Value < Membrane::Schemas::Base
|
10
|
+
attr_reader :value
|
11
|
+
|
12
|
+
def initialize(value)
|
13
|
+
@value = value
|
14
|
+
end
|
15
|
+
|
16
|
+
def validate(object)
|
17
|
+
ValueValidator.new(@value, object).validate
|
18
|
+
end
|
19
|
+
|
20
|
+
class ValueValidator
|
21
|
+
def initialize(value, object)
|
22
|
+
@value = value
|
23
|
+
@object = object
|
24
|
+
end
|
25
|
+
|
26
|
+
def validate
|
27
|
+
fail!(@value, @object) if @object != @value
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def fail!(expected, given)
|
33
|
+
emsg = "Expected #{expected}, given #{given}"
|
34
|
+
raise Membrane::SchemaValidationError.new(emsg)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/membrane/version.rb
CHANGED
data/lib/membrane.rb
CHANGED
data/spec/schema_parser_spec.rb
CHANGED
@@ -7,100 +7,104 @@ describe Membrane::SchemaParser do
|
|
7
7
|
it "should call inspect on the value of a Value schema" do
|
8
8
|
val = "test"
|
9
9
|
val.should_receive(:inspect).twice
|
10
|
-
schema = Membrane::
|
10
|
+
schema = Membrane::Schemas::Value.new(val)
|
11
11
|
|
12
12
|
parser.deparse(schema).should == val.inspect
|
13
13
|
end
|
14
14
|
|
15
|
-
it "should return 'any' for instance of Membrane::
|
16
|
-
schema = Membrane::
|
15
|
+
it "should return 'any' for instance of Membrane::Schemas::Any" do
|
16
|
+
schema = Membrane::Schemas::Any.new
|
17
17
|
|
18
18
|
parser.deparse(schema).should == "any"
|
19
19
|
end
|
20
20
|
|
21
|
-
it "should return 'bool' for instances of Membrane::
|
22
|
-
schema = Membrane::
|
21
|
+
it "should return 'bool' for instances of Membrane::Schemas::Bool" do
|
22
|
+
schema = Membrane::Schemas::Bool.new
|
23
23
|
|
24
24
|
parser.deparse(schema).should == "bool"
|
25
25
|
end
|
26
26
|
|
27
|
-
it "should call name on the class of a Membrane::
|
27
|
+
it "should call name on the class of a Membrane::Schemas::Class schema" do
|
28
28
|
klass = String
|
29
29
|
klass.should_receive(:name).twice
|
30
|
-
schema = Membrane::
|
30
|
+
schema = Membrane::Schemas::Class.new(klass)
|
31
31
|
|
32
32
|
parser.deparse(schema).should == klass.name
|
33
33
|
end
|
34
34
|
|
35
|
-
it "should deparse the k/v schemas of a Membrane::
|
36
|
-
key_schema = Membrane::
|
37
|
-
val_schema = Membrane::
|
35
|
+
it "should deparse the k/v schemas of a Membrane::Schemas::Dictionary schema" do
|
36
|
+
key_schema = Membrane::Schemas::Class.new(String)
|
37
|
+
val_schema = Membrane::Schemas::Class.new(Integer)
|
38
38
|
|
39
|
-
dict_schema = Membrane::
|
39
|
+
dict_schema = Membrane::Schemas::Dictionary.new(key_schema, val_schema)
|
40
40
|
|
41
41
|
parser.deparse(dict_schema).should == "dict(String, Integer)"
|
42
42
|
end
|
43
43
|
|
44
|
-
it "should deparse the element schemas of a Membrane::
|
44
|
+
it "should deparse the element schemas of a Membrane::Schemas::Enum schema" do
|
45
45
|
schemas =
|
46
|
-
[String, Integer, Float].map { |c| Membrane::
|
46
|
+
[String, Integer, Float].map { |c| Membrane::Schemas::Class.new(c) }
|
47
47
|
|
48
|
-
enum_schema = Membrane::
|
48
|
+
enum_schema = Membrane::Schemas::Enum.new(*schemas)
|
49
49
|
|
50
50
|
parser.deparse(enum_schema).should == "enum(String, Integer, Float)"
|
51
51
|
end
|
52
52
|
|
53
|
-
it "should deparse the element schema of a Membrane::
|
54
|
-
key_schema = Membrane::
|
55
|
-
val_schema = Membrane::
|
56
|
-
item_schema = Membrane::
|
53
|
+
it "should deparse the element schema of a Membrane::Schemas::List schema" do
|
54
|
+
key_schema = Membrane::Schemas::Class.new(String)
|
55
|
+
val_schema = Membrane::Schemas::Class.new(Integer)
|
56
|
+
item_schema = Membrane::Schemas::Dictionary.new(key_schema, val_schema)
|
57
57
|
|
58
|
-
list_schema = Membrane::
|
58
|
+
list_schema = Membrane::Schemas::List.new(item_schema)
|
59
59
|
|
60
60
|
parser.deparse(list_schema).should == "[dict(String, Integer)]"
|
61
61
|
end
|
62
62
|
|
63
|
-
it "should deparse elem schemas of a Membrane::
|
64
|
-
str_schema = Membrane::
|
65
|
-
int_schema = Membrane::
|
66
|
-
dict_schema = Membrane::
|
67
|
-
|
68
|
-
int_rec_schema = Membrane::
|
69
|
-
|
70
|
-
|
71
|
-
|
63
|
+
it "should deparse elem schemas of a Membrane::Schemas::Record schema" do
|
64
|
+
str_schema = Membrane::Schemas::Class.new(String)
|
65
|
+
int_schema = Membrane::Schemas::Class.new(Integer)
|
66
|
+
dict_schema = Membrane::Schemas::Dictionary.new(str_schema, int_schema)
|
67
|
+
|
68
|
+
int_rec_schema = Membrane::Schemas::Record.new({
|
69
|
+
:str => str_schema,
|
70
|
+
:dict => dict_schema
|
71
|
+
})
|
72
|
+
rec_schema = Membrane::Schemas::Record.new({
|
73
|
+
"str" => str_schema,
|
72
74
|
"rec" => int_rec_schema,
|
73
|
-
"int" => int_schema
|
75
|
+
"int" => int_schema
|
76
|
+
})
|
77
|
+
|
74
78
|
exp_deparse =<<EOT
|
75
79
|
{
|
76
|
-
"str" => String,
|
77
|
-
"rec" => {
|
78
|
-
|
79
|
-
|
80
|
+
"str" => String,
|
81
|
+
"rec" => {
|
82
|
+
:str => String,
|
83
|
+
:dict => dict(String, Integer),
|
80
84
|
},
|
81
|
-
"int" => Integer,
|
85
|
+
"int" => Integer,
|
82
86
|
}
|
83
87
|
EOT
|
84
88
|
parser.deparse(rec_schema).should == exp_deparse.strip
|
85
89
|
end
|
86
90
|
|
87
|
-
it "should call inspect on regexps for Membrane::
|
88
|
-
schema = Membrane::
|
91
|
+
it "should call inspect on regexps for Membrane::Schemas::Regexp" do
|
92
|
+
schema = Membrane::Schemas::Regexp.new(/test/)
|
89
93
|
schema.regexp.should_receive(:inspect)
|
90
94
|
parser.deparse(schema)
|
91
95
|
end
|
92
96
|
|
93
|
-
it "should deparse the element schemas of a Membrane::
|
94
|
-
schemas = [String, Integer].map { |c| Membrane::
|
95
|
-
schemas << Membrane::
|
97
|
+
it "should deparse the element schemas of a Membrane::Schemas::Tuple schema" do
|
98
|
+
schemas = [String, Integer].map { |c| Membrane::Schemas::Class.new(c) }
|
99
|
+
schemas << Membrane::Schemas::Value.new("test")
|
96
100
|
|
97
|
-
enum_schema = Membrane::
|
101
|
+
enum_schema = Membrane::Schemas::Tuple.new(*schemas)
|
98
102
|
|
99
103
|
parser.deparse(enum_schema).should == 'tuple(String, Integer, "test")'
|
100
104
|
end
|
101
105
|
|
102
|
-
it "should call inspect on a Membrane::
|
103
|
-
schema = Membrane::
|
106
|
+
it "should call inspect on a Membrane::Schemas::Base schema" do
|
107
|
+
schema = Membrane::Schemas::Base.new
|
104
108
|
parser.deparse(schema).should == schema.inspect
|
105
109
|
end
|
106
110
|
|
@@ -112,85 +116,85 @@ EOT
|
|
112
116
|
end
|
113
117
|
|
114
118
|
describe "#parse" do
|
115
|
-
it "should leave instances derived from Membrane::
|
116
|
-
old_schema = Membrane::
|
119
|
+
it "should leave instances derived from Membrane::Schemas::Base unchanged" do
|
120
|
+
old_schema = Membrane::Schemas::Any.new
|
117
121
|
|
118
122
|
parser.parse { old_schema }.should == old_schema
|
119
123
|
end
|
120
124
|
|
121
|
-
it "should translate 'any' into Membrane::
|
125
|
+
it "should translate 'any' into Membrane::Schemas::Any" do
|
122
126
|
schema = parser.parse { any }
|
123
127
|
|
124
|
-
schema.class.should == Membrane::
|
128
|
+
schema.class.should == Membrane::Schemas::Any
|
125
129
|
end
|
126
130
|
|
127
|
-
it "should translate 'bool' into Membrane::
|
131
|
+
it "should translate 'bool' into Membrane::Schemas::Bool" do
|
128
132
|
schema = parser.parse { bool }
|
129
133
|
|
130
|
-
schema.class.should == Membrane::
|
134
|
+
schema.class.should == Membrane::Schemas::Bool
|
131
135
|
end
|
132
136
|
|
133
|
-
it "should translate 'enum' into Membrane::
|
137
|
+
it "should translate 'enum' into Membrane::Schemas::Enum" do
|
134
138
|
schema = parser.parse { enum(bool, any) }
|
135
139
|
|
136
|
-
schema.class.should == Membrane::
|
140
|
+
schema.class.should == Membrane::Schemas::Enum
|
137
141
|
|
138
142
|
schema.elem_schemas.length.should == 2
|
139
143
|
|
140
144
|
elem_schema_classes = schema.elem_schemas.map { |es| es.class }
|
141
145
|
|
142
|
-
expected_classes = [Membrane::
|
146
|
+
expected_classes = [Membrane::Schemas::Bool, Membrane::Schemas::Any]
|
143
147
|
elem_schema_classes.should == expected_classes
|
144
148
|
end
|
145
149
|
|
146
|
-
it "should translate 'dict' into Membrane::
|
150
|
+
it "should translate 'dict' into Membrane::Schemas::Dictionary" do
|
147
151
|
schema = parser.parse { dict(String, Integer) }
|
148
152
|
|
149
|
-
schema.class.should == Membrane::
|
153
|
+
schema.class.should == Membrane::Schemas::Dictionary
|
150
154
|
|
151
|
-
schema.key_schema.class.should == Membrane::
|
155
|
+
schema.key_schema.class.should == Membrane::Schemas::Class
|
152
156
|
schema.key_schema.klass.should == String
|
153
157
|
|
154
|
-
schema.value_schema.class.should == Membrane::
|
158
|
+
schema.value_schema.class.should == Membrane::Schemas::Class
|
155
159
|
schema.value_schema.klass.should == Integer
|
156
160
|
end
|
157
161
|
|
158
|
-
it "should translate 'tuple' into Membrane::
|
162
|
+
it "should translate 'tuple' into Membrane::Schemas::Tuple" do
|
159
163
|
schema = parser.parse { tuple(String, any, Integer) }
|
160
164
|
|
161
|
-
schema.class.should == Membrane::
|
165
|
+
schema.class.should == Membrane::Schemas::Tuple
|
162
166
|
|
163
|
-
schema.elem_schemas[0].class.should == Membrane::
|
167
|
+
schema.elem_schemas[0].class.should == Membrane::Schemas::Class
|
164
168
|
schema.elem_schemas[0].klass.should == String
|
165
169
|
|
166
|
-
schema.elem_schemas[1].class == Membrane::
|
170
|
+
schema.elem_schemas[1].class == Membrane::Schemas::Any
|
167
171
|
|
168
|
-
schema.elem_schemas[2].class.should == Membrane::
|
172
|
+
schema.elem_schemas[2].class.should == Membrane::Schemas::Class
|
169
173
|
schema.elem_schemas[2].klass.should == Integer
|
170
174
|
end
|
171
175
|
|
172
|
-
it "should translate classes into Membrane::
|
176
|
+
it "should translate classes into Membrane::Schemas::Class" do
|
173
177
|
schema = parser.parse { String }
|
174
178
|
|
175
|
-
schema.class.should == Membrane::
|
179
|
+
schema.class.should == Membrane::Schemas::Class
|
176
180
|
|
177
181
|
schema.klass.should == String
|
178
182
|
end
|
179
183
|
|
180
|
-
it "should translate regexps into Membrane::
|
184
|
+
it "should translate regexps into Membrane::Schemas::Regexp" do
|
181
185
|
regexp = /foo/
|
182
186
|
|
183
187
|
schema = parser.parse { regexp }
|
184
188
|
|
185
|
-
schema.class.should == Membrane::
|
189
|
+
schema.class.should == Membrane::Schemas::Regexp
|
186
190
|
|
187
191
|
schema.regexp.should == regexp
|
188
192
|
end
|
189
193
|
|
190
|
-
it "should fall back to Membrane::
|
194
|
+
it "should fall back to Membrane::Schemas::Value" do
|
191
195
|
schema = parser.parse { 5 }
|
192
196
|
|
193
|
-
schema.class.should == Membrane::
|
197
|
+
schema.class.should == Membrane::Schemas::Value
|
194
198
|
schema.value.should == 5
|
195
199
|
end
|
196
200
|
|
@@ -207,12 +211,12 @@ EOT
|
|
207
211
|
end.to raise_error(ArgumentError, /single schema/)
|
208
212
|
end
|
209
213
|
|
210
|
-
it "should parse '[<expr>]' into Membrane::
|
214
|
+
it "should parse '[<expr>]' into Membrane::Schemas::List" do
|
211
215
|
schema = parser.parse { [String] }
|
212
216
|
|
213
|
-
schema.class.should == Membrane::
|
217
|
+
schema.class.should == Membrane::Schemas::List
|
214
218
|
|
215
|
-
schema.elem_schema.class.should == Membrane::
|
219
|
+
schema.elem_schema.class.should == Membrane::Schemas::Class
|
216
220
|
schema.elem_schema.klass.should == String
|
217
221
|
end
|
218
222
|
end
|
@@ -224,29 +228,29 @@ EOT
|
|
224
228
|
end.to raise_error(ArgumentError, /must supply/)
|
225
229
|
end
|
226
230
|
|
227
|
-
it "should parse '{ <key> => <schema> }' into Membrane::
|
231
|
+
it "should parse '{ <key> => <schema> }' into Membrane::Schemas::Record" do
|
228
232
|
schema = parser.parse do
|
229
233
|
{ "string" => String,
|
230
234
|
"ints" => [Integer],
|
231
235
|
}
|
232
236
|
end
|
233
237
|
|
234
|
-
schema.class.should == Membrane::
|
238
|
+
schema.class.should == Membrane::Schemas::Record
|
235
239
|
|
236
240
|
str_schema = schema.schemas["string"]
|
237
|
-
str_schema.class.should == Membrane::
|
241
|
+
str_schema.class.should == Membrane::Schemas::Class
|
238
242
|
str_schema.klass.should == String
|
239
243
|
|
240
244
|
ints_schema = schema.schemas["ints"]
|
241
|
-
ints_schema.class.should == Membrane::
|
242
|
-
ints_schema.elem_schema.class.should == Membrane::
|
245
|
+
ints_schema.class.should == Membrane::Schemas::List
|
246
|
+
ints_schema.elem_schema.class.should == Membrane::Schemas::Class
|
243
247
|
ints_schema.elem_schema.klass.should == Integer
|
244
248
|
end
|
245
249
|
|
246
250
|
it "should handle keys marked with 'optional()'" do
|
247
251
|
schema = parser.parse { { optional("test") => Integer } }
|
248
252
|
|
249
|
-
schema.class.should == Membrane::
|
253
|
+
schema.class.should == Membrane::Schemas::Record
|
250
254
|
schema.optional_keys.to_a.should == ["test"]
|
251
255
|
end
|
252
256
|
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
describe Membrane::
|
3
|
+
describe Membrane::Schemas::Any do
|
4
4
|
describe "#validate" do
|
5
5
|
it "should always return nil" do
|
6
|
-
schema = Membrane::
|
6
|
+
schema = Membrane::Schemas::Any.new
|
7
7
|
# Smoke test more than anything. Cannot validate this with 100%
|
8
8
|
# certainty.
|
9
9
|
[1, "hi", :test, {}, []].each do |o|
|
@@ -1,9 +1,9 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
|
4
|
-
describe Membrane::
|
4
|
+
describe Membrane::Schemas::Base do
|
5
5
|
describe "#validate" do
|
6
|
-
let(:schema) { Membrane::
|
6
|
+
let(:schema) { Membrane::Schemas::Base.new }
|
7
7
|
|
8
8
|
it "should raise error" do
|
9
9
|
expect { schema.validate }.to raise_error
|
@@ -1,9 +1,8 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
|
4
|
-
describe Membrane::Schema::Bool do
|
3
|
+
describe Membrane::Schemas::Bool do
|
5
4
|
describe "#validate" do
|
6
|
-
let(:schema) { Membrane::
|
5
|
+
let(:schema) { Membrane::Schemas::Bool.new }
|
7
6
|
|
8
7
|
it "should return nil for {true, false}" do
|
9
8
|
[true, false].each { |v| schema.validate(v).should be_nil }
|
@@ -1,9 +1,9 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
|
4
|
-
describe Membrane::
|
4
|
+
describe Membrane::Schemas::Class do
|
5
5
|
describe "#validate" do
|
6
|
-
let(:schema) { Membrane::
|
6
|
+
let(:schema) { Membrane::Schemas::Class.new(String) }
|
7
7
|
|
8
8
|
it "should return nil for instances of the supplied class" do
|
9
9
|
schema.validate("test").should be_nil
|
@@ -1,33 +1,33 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
describe Membrane::
|
3
|
+
describe Membrane::Schemas::Dictionary do
|
4
4
|
describe "#validate" do
|
5
5
|
let (:data) { { "foo" => 1, "bar" => 2 } }
|
6
6
|
|
7
7
|
it "should return an error if supplied with a non-hash" do
|
8
|
-
schema = Membrane::
|
8
|
+
schema = Membrane::Schemas::Dictionary.new(nil, nil)
|
9
9
|
|
10
10
|
expect_validation_failure(schema, "test", /instance of Hash/)
|
11
11
|
end
|
12
12
|
|
13
13
|
it "should validate each key against the supplied key schema" do
|
14
|
-
key_schema =
|
14
|
+
key_schema = double("key_schema")
|
15
15
|
|
16
16
|
data.keys.each { |k| key_schema.should_receive(:validate).with(k) }
|
17
17
|
|
18
|
-
dict_schema = Membrane::
|
19
|
-
Membrane::
|
18
|
+
dict_schema = Membrane::Schemas::Dictionary.new(key_schema,
|
19
|
+
Membrane::Schemas::Any.new)
|
20
20
|
|
21
21
|
dict_schema.validate(data)
|
22
22
|
end
|
23
23
|
|
24
24
|
it "should validate the value for each valid key" do
|
25
|
-
key_schema = Membrane::
|
26
|
-
val_schema =
|
25
|
+
key_schema = Membrane::Schemas::Class.new(String)
|
26
|
+
val_schema = double("val_schema")
|
27
27
|
|
28
28
|
data.values.each { |v| val_schema.should_receive(:validate).with(v) }
|
29
29
|
|
30
|
-
dict_schema = Membrane::
|
30
|
+
dict_schema = Membrane::Schemas::Dictionary.new(key_schema, val_schema)
|
31
31
|
|
32
32
|
dict_schema.validate(data)
|
33
33
|
end
|
@@ -38,9 +38,9 @@ describe Membrane::Schema::Dictionary do
|
|
38
38
|
:bar => 2,
|
39
39
|
}
|
40
40
|
|
41
|
-
key_schema = Membrane::
|
42
|
-
val_schema = Membrane::
|
43
|
-
dict_schema = Membrane::
|
41
|
+
key_schema = Membrane::Schemas::Class.new(String)
|
42
|
+
val_schema = Membrane::Schemas::Class.new(Integer)
|
43
|
+
dict_schema = Membrane::Schemas::Dictionary.new(key_schema, val_schema)
|
44
44
|
|
45
45
|
errors = nil
|
46
46
|
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
describe Membrane::
|
3
|
+
describe Membrane::Schemas::Enum do
|
4
4
|
describe "#validate" do
|
5
|
-
let (:int_schema) { Membrane::
|
6
|
-
let (:str_schema) { Membrane::
|
7
|
-
let (:enum_schema) { Membrane::
|
5
|
+
let (:int_schema) { Membrane::Schemas::Class.new(Integer) }
|
6
|
+
let (:str_schema) { Membrane::Schemas::Class.new(String) }
|
7
|
+
let (:enum_schema) { Membrane::Schemas::Enum.new(int_schema, str_schema) }
|
8
8
|
|
9
9
|
it "should return an error if none of the schemas validate" do
|
10
10
|
expect_validation_failure(enum_schema, :sym, /doesn't validate/)
|