respect 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.md +289 -0
- data/RELATED_WORK.md +40 -0
- data/RELEASE_NOTES.md +23 -0
- data/Rakefile +31 -0
- data/STATUS_MATRIX.html +137 -0
- data/lib/respect.rb +231 -0
- data/lib/respect/any_schema.rb +22 -0
- data/lib/respect/array_def.rb +28 -0
- data/lib/respect/array_schema.rb +203 -0
- data/lib/respect/boolean_schema.rb +32 -0
- data/lib/respect/composite_schema.rb +86 -0
- data/lib/respect/core_statements.rb +206 -0
- data/lib/respect/datetime_schema.rb +27 -0
- data/lib/respect/def_without_name.rb +6 -0
- data/lib/respect/divisible_by_validator.rb +20 -0
- data/lib/respect/doc_helper.rb +24 -0
- data/lib/respect/doc_parser.rb +37 -0
- data/lib/respect/dsl_dumper.rb +181 -0
- data/lib/respect/equal_to_validator.rb +20 -0
- data/lib/respect/fake_name_proxy.rb +116 -0
- data/lib/respect/float_schema.rb +27 -0
- data/lib/respect/format_validator.rb +136 -0
- data/lib/respect/global_def.rb +79 -0
- data/lib/respect/greater_than_or_equal_to_validator.rb +19 -0
- data/lib/respect/greater_than_validator.rb +19 -0
- data/lib/respect/has_constraints.rb +34 -0
- data/lib/respect/hash_def.rb +40 -0
- data/lib/respect/hash_schema.rb +218 -0
- data/lib/respect/in_validator.rb +19 -0
- data/lib/respect/integer_schema.rb +27 -0
- data/lib/respect/ip_addr_schema.rb +23 -0
- data/lib/respect/ipv4_addr_schema.rb +27 -0
- data/lib/respect/ipv6_addr_schema.rb +27 -0
- data/lib/respect/items_def.rb +21 -0
- data/lib/respect/json_schema_html_formatter.rb +143 -0
- data/lib/respect/less_than_or_equal_to_validator.rb +19 -0
- data/lib/respect/less_than_validator.rb +19 -0
- data/lib/respect/match_validator.rb +19 -0
- data/lib/respect/max_length_validator.rb +20 -0
- data/lib/respect/min_length_validator.rb +20 -0
- data/lib/respect/multiple_of_validator.rb +10 -0
- data/lib/respect/null_schema.rb +26 -0
- data/lib/respect/numeric_schema.rb +33 -0
- data/lib/respect/org3_dumper.rb +213 -0
- data/lib/respect/regexp_schema.rb +19 -0
- data/lib/respect/schema.rb +285 -0
- data/lib/respect/schema_def.rb +16 -0
- data/lib/respect/string_schema.rb +21 -0
- data/lib/respect/unit_test_helper.rb +37 -0
- data/lib/respect/uri_schema.rb +23 -0
- data/lib/respect/utc_time_schema.rb +17 -0
- data/lib/respect/validator.rb +51 -0
- data/lib/respect/version.rb +3 -0
- data/test/any_schema_test.rb +79 -0
- data/test/array_def_test.rb +113 -0
- data/test/array_schema_test.rb +487 -0
- data/test/boolean_schema_test.rb +89 -0
- data/test/composite_schema_test.rb +30 -0
- data/test/datetime_schema_test.rb +83 -0
- data/test/doc_helper_test.rb +34 -0
- data/test/doc_parser_test.rb +109 -0
- data/test/dsl_dumper_test.rb +395 -0
- data/test/fake_name_proxy_test.rb +138 -0
- data/test/float_schema_test.rb +146 -0
- data/test/format_validator_test.rb +224 -0
- data/test/hash_def_test.rb +126 -0
- data/test/hash_schema_test.rb +613 -0
- data/test/integer_schema_test.rb +142 -0
- data/test/ip_addr_schema_test.rb +78 -0
- data/test/ipv4_addr_schema_test.rb +71 -0
- data/test/ipv6_addr_schema_test.rb +71 -0
- data/test/json_schema_html_formatter_test.rb +214 -0
- data/test/null_schema_test.rb +46 -0
- data/test/numeric_schema_test.rb +294 -0
- data/test/org3_dumper_test.rb +784 -0
- data/test/regexp_schema_test.rb +54 -0
- data/test/respect_test.rb +108 -0
- data/test/schema_def_test.rb +405 -0
- data/test/schema_test.rb +290 -0
- data/test/string_schema_test.rb +209 -0
- data/test/support/circle.rb +11 -0
- data/test/support/color.rb +24 -0
- data/test/support/point.rb +11 -0
- data/test/support/respect/circle_schema.rb +16 -0
- data/test/support/respect/color_def.rb +19 -0
- data/test/support/respect/color_schema.rb +33 -0
- data/test/support/respect/point_schema.rb +19 -0
- data/test/support/respect/rgba_schema.rb +20 -0
- data/test/support/respect/universal_validator.rb +25 -0
- data/test/support/respect/user_macros.rb +12 -0
- data/test/support/rgba.rb +11 -0
- data/test/test_helper.rb +90 -0
- data/test/uri_schema_test.rb +54 -0
- data/test/utc_time_schema_test.rb +63 -0
- data/test/validator_test.rb +22 -0
- metadata +288 -0
data/test/schema_test.rb
ADDED
@@ -0,0 +1,290 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class SchemaTest < Test::Unit::TestCase
|
4
|
+
def test_validate_value_return_true_on_success
|
5
|
+
s = Respect::HashSchema.define do |s|
|
6
|
+
s.string "test", equal_to: "value"
|
7
|
+
end
|
8
|
+
assert s.validate({ "test" => "value" })
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_no_exception_raised_for_invalid_constraint
|
12
|
+
s = Respect::HashSchema.define do |s|
|
13
|
+
s.string "test", invalid_constraint: "value"
|
14
|
+
end
|
15
|
+
assert_nothing_raised do
|
16
|
+
assert s.validate({ "test" => "value" })
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_access_to_sanitized_value
|
21
|
+
s = Respect::IntegerSchema.new(equal_to: 42)
|
22
|
+
doc = "42"
|
23
|
+
assert s.validate(doc)
|
24
|
+
assert_equal 42, s.sanitized_object
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_validate_query_does_not_raise_exception_when_invalid
|
28
|
+
s = Respect::HashSchema.define do |s|
|
29
|
+
s.string "test", equal_to: "value"
|
30
|
+
end
|
31
|
+
assert_schema_validate s, { "test" => "value" }
|
32
|
+
assert_schema_invalidate s, { "test" => "invalid" }
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_non_collection_schema_validation_error_have_context
|
36
|
+
s = Respect::Schema.define do |s|
|
37
|
+
s.string equal_to: "value"
|
38
|
+
end
|
39
|
+
assert_schema_invalidate s, "invalid"
|
40
|
+
assert_equal 1, s.last_error.context.size
|
41
|
+
assert (s.last_error.message == s.last_error.context.first)
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_hash_schema_validation_error_have_context
|
45
|
+
s = Respect::HashSchema.define do |s|
|
46
|
+
s.string "test", equal_to: "value"
|
47
|
+
end
|
48
|
+
assert_schema_invalidate s, { "test" => "invalid" }
|
49
|
+
assert_equal 2, s.last_error.context.size
|
50
|
+
assert_hash_context_error_message("test", s.last_error.context.last)
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_array_schema_validation_error_have_context
|
54
|
+
s = Respect::ArraySchema.define do |s|
|
55
|
+
s.numeric
|
56
|
+
end
|
57
|
+
assert_schema_invalidate s, [1, "invalid", 3]
|
58
|
+
assert_equal 2, s.last_error.context.size
|
59
|
+
assert_array_context_error_message(1, s.last_error.context.last)
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_error_context_can_be_nested
|
63
|
+
s = Respect::HashSchema.define do |s|
|
64
|
+
s.hash "level_1" do |s|
|
65
|
+
s.array "level_2" do |s|
|
66
|
+
s.hash do |s|
|
67
|
+
s.numeric "level_4", equal_to: 51
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
doc = {
|
73
|
+
"level_1" => {
|
74
|
+
"level_2" => [
|
75
|
+
{ "level_4" => 42 }
|
76
|
+
]
|
77
|
+
}
|
78
|
+
}
|
79
|
+
assert_schema_invalidate s, doc
|
80
|
+
assert_equal 5, s.last_error.context.size
|
81
|
+
assert_hash_context_error_message("level_4", s.last_error.context[1])
|
82
|
+
assert_array_context_error_message("0", s.last_error.context[2])
|
83
|
+
assert_hash_context_error_message("level_2", s.last_error.context[3])
|
84
|
+
assert_hash_context_error_message("level_1", s.last_error.context[4])
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_schema_to_s_as_dsl
|
88
|
+
s = Respect::HashSchema.new
|
89
|
+
d = Respect::DslDumper.new(s)
|
90
|
+
Respect::DslDumper.expects(:new).with(s).returns(d)
|
91
|
+
d.stubs(:dump).at_least_once
|
92
|
+
s.to_s
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_schema_to_json_org3
|
96
|
+
h = {}
|
97
|
+
h.stubs(:to_json).returns('json string').at_least_once
|
98
|
+
|
99
|
+
s = Respect::HashSchema.new
|
100
|
+
s.stubs(:to_h).with(:org3).returns(h).at_least_once
|
101
|
+
|
102
|
+
assert_equal("json string", s.to_json(:org3))
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_schema_to_h_as_org3
|
106
|
+
s = Respect::HashSchema.new
|
107
|
+
d = Respect::Org3Dumper.new(s)
|
108
|
+
Respect::Org3Dumper.expects(:new).with(s).returns(d)
|
109
|
+
d.stubs(:dump).returns("json hash").at_least_once
|
110
|
+
assert_equal("json hash", s.to_h(:org3))
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_def_class_name
|
114
|
+
assert_equal "Respect::SchemaDef", Respect::Schema.def_class_name
|
115
|
+
assert_equal "Respect::ArrayDef", Respect::ArraySchema.def_class_name
|
116
|
+
assert_equal "Respect::HashDef", Respect::HashSchema.def_class_name
|
117
|
+
assert_equal "Respect::StringDef", Respect::StringSchema.def_class_name
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_def_class
|
121
|
+
assert_equal Respect::SchemaDef, Respect::Schema.def_class
|
122
|
+
assert_equal Respect::ArrayDef, Respect::ArraySchema.def_class
|
123
|
+
assert_equal Respect::HashDef, Respect::HashSchema.def_class
|
124
|
+
assert_equal nil, Respect::StringSchema.def_class
|
125
|
+
end
|
126
|
+
|
127
|
+
def test_statement_name
|
128
|
+
assert_equal "string", Respect::StringSchema.statement_name
|
129
|
+
assert_equal "integer", Respect::IntegerSchema.statement_name
|
130
|
+
assert_equal "schema", Respect::Schema.statement_name
|
131
|
+
assert_equal "circle", Respect::CircleSchema.statement_name
|
132
|
+
assert_equal "point", Respect::PointSchema.statement_name
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_in_place_validation_always_return_boolean
|
136
|
+
s = Respect::Schema.define do |s|
|
137
|
+
s.boolean
|
138
|
+
end
|
139
|
+
assert_equal true, s.validate!(false)
|
140
|
+
assert_equal true, s.validate!(true)
|
141
|
+
assert_equal false, s.validate!(nil)
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_sanitize_always_return_new_object
|
145
|
+
s = Respect::Schema.define do |s|
|
146
|
+
s.boolean
|
147
|
+
end
|
148
|
+
assert_equal false, s.sanitize!(false)
|
149
|
+
assert_equal true, s.sanitize!(true)
|
150
|
+
assert_raise(Respect::ValidationError) do
|
151
|
+
s.sanitize!(nil)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
def test_schema_accept_doc_option
|
156
|
+
s = Respect::IntegerSchema.new(doc: "Hey!")
|
157
|
+
assert_equal "Hey!", s.doc
|
158
|
+
assert_equal "Hey!", s.options[:doc]
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_duplicata_are_equal
|
162
|
+
s1 = Respect::IntegerSchema.new
|
163
|
+
assert_equal s1, s1.dup
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_schema_differs_from_options
|
167
|
+
s1 = Respect::IntegerSchema.new(required: true)
|
168
|
+
s2 = Respect::IntegerSchema.new(required: false)
|
169
|
+
assert(s1 != s2)
|
170
|
+
end
|
171
|
+
|
172
|
+
def test_schema_differs_from_doc
|
173
|
+
s1 = Respect::Schema.define do |s|
|
174
|
+
s.doc "hey"
|
175
|
+
s.integer
|
176
|
+
end
|
177
|
+
s2 = Respect::Schema.define do |s|
|
178
|
+
s.doc "ho"
|
179
|
+
s.integer
|
180
|
+
end
|
181
|
+
assert(s1 != s2)
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_schema_differs_from_type
|
185
|
+
s1 = Respect::IntegerSchema.new
|
186
|
+
s2 = Respect::StringSchema.new
|
187
|
+
assert(s1 != s2)
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_schema_differs_from_validator
|
191
|
+
s1 = Respect::IntegerSchema.new(equal_to: 42)
|
192
|
+
s2 = Respect::IntegerSchema.new(equal_to: 51)
|
193
|
+
assert(s1 != s2)
|
194
|
+
end
|
195
|
+
|
196
|
+
def test_schema_dont_differs_from_sanitized_object
|
197
|
+
s1 = Respect::IntegerSchema.new
|
198
|
+
s1.send(:sanitized_object=, 42)
|
199
|
+
s2 = Respect::IntegerSchema.new
|
200
|
+
s2.send(:sanitized_object=, 51)
|
201
|
+
assert(s1 == s2)
|
202
|
+
end
|
203
|
+
|
204
|
+
def test_dup_duplicate_options
|
205
|
+
s1 = Respect::IntegerSchema.define equal_to: 42
|
206
|
+
s2 = s1.dup
|
207
|
+
assert(s2.object_id != s1.object_id)
|
208
|
+
assert_equal(42, s2.options[:equal_to])
|
209
|
+
end
|
210
|
+
|
211
|
+
def test_validate_query_returns_true_when_no_error
|
212
|
+
s = Respect::Schema.send(:new)
|
213
|
+
doc = {}
|
214
|
+
s.stubs(:validate).with(doc).returns(nil).once
|
215
|
+
assert_equal true, s.validate?(doc)
|
216
|
+
end
|
217
|
+
|
218
|
+
def test_validate_query_returns_false_on_error_and_store_last_error
|
219
|
+
s = Respect::Schema.send(:new)
|
220
|
+
doc = {}
|
221
|
+
error = Respect::ValidationError.new("message")
|
222
|
+
s.stubs(:validate).with(doc).raises(error).once
|
223
|
+
assert_equal false, s.validate?(doc)
|
224
|
+
assert_equal error, s.last_error
|
225
|
+
end
|
226
|
+
|
227
|
+
def test_validate_shebang_returns_true_on_success_and_sanitize
|
228
|
+
s = Respect::Schema.send(:new)
|
229
|
+
doc = {}
|
230
|
+
s.stubs(:validate?).with(doc).returns(true).once
|
231
|
+
s.stubs(:sanitize_object!).with(doc).once
|
232
|
+
assert_equal true, s.validate!(doc)
|
233
|
+
end
|
234
|
+
|
235
|
+
def test_validate_shebang_returns_false_on_failure
|
236
|
+
s = Respect::Schema.send(:new)
|
237
|
+
doc = {}
|
238
|
+
s.stubs(:validate?).with(doc).returns(false).once
|
239
|
+
assert_equal false, s.validate!(doc)
|
240
|
+
end
|
241
|
+
|
242
|
+
def test_sanitize_shebang_raises_exception_on_error
|
243
|
+
s = Respect::Schema.send(:new)
|
244
|
+
doc = {}
|
245
|
+
error = Respect::ValidationError.new("message")
|
246
|
+
s.stubs(:validate).with(doc).raises(error).once
|
247
|
+
begin
|
248
|
+
s.sanitize!(doc)
|
249
|
+
assert false, "nothing raised"
|
250
|
+
rescue Respect::ValidationError => e
|
251
|
+
assert_equal error, e
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
def test_sanitize_shebang_sanitize_on_success
|
256
|
+
s = Respect::Schema.send(:new)
|
257
|
+
doc = {}
|
258
|
+
s.stubs(:validate).with(doc).once
|
259
|
+
result = Object.new
|
260
|
+
s.stubs(:sanitize_object!).with(doc).returns(result).once
|
261
|
+
assert_equal result.object_id, s.sanitize!(doc).object_id
|
262
|
+
end
|
263
|
+
|
264
|
+
def test_sanitize_doc_shebang
|
265
|
+
s = Respect::Schema.send(:new)
|
266
|
+
doc = {}
|
267
|
+
sanitized_object = {}
|
268
|
+
s.stubs(:sanitized_object).with().returns(sanitized_object).once
|
269
|
+
result = Object.new
|
270
|
+
Respect.stubs(:sanitize_object!).with(doc, sanitized_object).returns(result).once
|
271
|
+
assert_equal result.object_id, s.sanitize_object!(doc).object_id
|
272
|
+
end
|
273
|
+
|
274
|
+
private
|
275
|
+
|
276
|
+
def assert_hash_context_error_message(prop_name, message)
|
277
|
+
[ /\bin\b/, /\bhash\b/, /\bproperty\b/, /\b#{prop_name}\b/ ].each do |rx|
|
278
|
+
assert(rx =~ message,
|
279
|
+
"error message '#{message}' does not match #{rx}")
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
def assert_array_context_error_message(index, message)
|
284
|
+
[ /\bin\b/, /\barray\b/, /\bitem\b/, /\b#{index}/ ].each do |rx|
|
285
|
+
assert(rx =~ message,
|
286
|
+
"error message '#{message}' does not match #{rx}")
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
end
|
@@ -0,0 +1,209 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class StringSchemaTest < Test::Unit::TestCase
|
4
|
+
def test_expected_value_are_not_converted
|
5
|
+
s = Respect::StringSchema.new(equal_to: 42)
|
6
|
+
assert_raise(Respect::ValidationError) do
|
7
|
+
assert s.validate("42")
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_non_string_value_get_converted
|
12
|
+
s = Respect::StringSchema.new
|
13
|
+
assert_equal "42", s.validate_type(42)
|
14
|
+
assert_nil s.sanitized_object
|
15
|
+
s.validate(42)
|
16
|
+
assert_equal "42", s.sanitized_object
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_string_property_has_no_greater_than_constraint
|
20
|
+
s = Respect::StringSchema.new(greater_than: 0)
|
21
|
+
# Using an integer constraints with a string lead to
|
22
|
+
# unexpected result.
|
23
|
+
assert_raises(ArgumentError) do
|
24
|
+
s.validate(-1)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_string_accept_options_mixed_with_constraints
|
29
|
+
s = Respect::StringSchema.new(equal_to: "42", required: false)
|
30
|
+
assert_equal false, s.options[:required]
|
31
|
+
assert_equal "42", s.options[:equal_to]
|
32
|
+
assert s.validate("42")
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_string_value_is_in_set
|
36
|
+
s = Respect::StringSchema.new(in: ["foo", "bar"])
|
37
|
+
assert_schema_validate s, "foo"
|
38
|
+
assert_schema_validate s, "bar"
|
39
|
+
assert_schema_invalidate s, "not included"
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_string_value_match_pattern
|
43
|
+
s = Respect::StringSchema.new(match: /foo*/)
|
44
|
+
assert_schema_validate s, "_foo_"
|
45
|
+
assert_schema_validate s, "fo"
|
46
|
+
assert_schema_validate s, "fol"
|
47
|
+
assert_schema_validate s, "foooooooooooo"
|
48
|
+
assert_schema_invalidate s, "f_b"
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_string_value_has_min_length
|
52
|
+
s = Respect::StringSchema.new(min_length: 2)
|
53
|
+
assert_schema_validate s, "foo"
|
54
|
+
assert_schema_validate s, "fo"
|
55
|
+
assert_schema_invalidate s, "f"
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_string_value_has_max_length
|
59
|
+
s = Respect::StringSchema.new(max_length: 2)
|
60
|
+
assert_schema_invalidate s, "foo"
|
61
|
+
assert_schema_validate s, "fo"
|
62
|
+
assert_schema_validate s, "f"
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_string_accept_equal_to_constraint
|
66
|
+
s = Respect::StringSchema.new(equal_to: "41")
|
67
|
+
assert_schema_validate s, "41"
|
68
|
+
assert_schema_invalidate s, "52"
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_string_validate_email_format
|
72
|
+
[
|
73
|
+
[ "foo", false, ],
|
74
|
+
[ "foo@bar", true, ],
|
75
|
+
[ "foo@example.om", true, ],
|
76
|
+
].each_with_index do |data, i|
|
77
|
+
assert_validate_string_format(:email, data[1], data[0])
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_string_validate_uri_format
|
82
|
+
[
|
83
|
+
[ "<>", false, ],
|
84
|
+
[ "http://foo.com", true, ],
|
85
|
+
[ "foo@example.om", true, ],
|
86
|
+
].each_with_index do |data, i|
|
87
|
+
assert_validate_string_format(:uri, data[1], data[0])
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_string_validate_regexp_format
|
92
|
+
[
|
93
|
+
[ "foo(", false, ],
|
94
|
+
[ "^foo$", true, ],
|
95
|
+
[ "^foo|bar$", true, ],
|
96
|
+
].each_with_index do |data, i|
|
97
|
+
assert_validate_string_format(:regexp, data[1], data[0])
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_string_validate_datetime_format
|
102
|
+
[
|
103
|
+
[ "invalid", false, ],
|
104
|
+
[ "2013-12-01T00:00:00+00:00", true, ],
|
105
|
+
].each_with_index do |data, i|
|
106
|
+
assert_validate_string_format(:datetime, data[1], data[0])
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_string_validate_ipv4_addr_format
|
111
|
+
[
|
112
|
+
[ "192.168.0.1", true, ],
|
113
|
+
[ "invalid", false, ],
|
114
|
+
].each_with_index do |data, i|
|
115
|
+
assert_validate_string_format(:ipv4_addr, data[1], data[0])
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_string_validate_phone_number_format
|
120
|
+
[
|
121
|
+
[ "+3360123456789", true, ],
|
122
|
+
[ "invalid", false, ],
|
123
|
+
].each_with_index do |data, i|
|
124
|
+
assert_validate_string_format(:phone_number, data[1], data[0])
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_string_validate_ipv6_addr_format
|
129
|
+
[
|
130
|
+
[ "3ffe:505:2::1", true, ],
|
131
|
+
[ "invalid", false, ],
|
132
|
+
].each_with_index do |data, i|
|
133
|
+
assert_validate_string_format(:ipv6_addr, data[1], data[0])
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_string_validate_ip_addr_format
|
138
|
+
[
|
139
|
+
[ "192.168.0.1", true, ],
|
140
|
+
[ "3ffe:505:2::1", true, ],
|
141
|
+
[ "invalid", false, ],
|
142
|
+
].each_with_index do |data, i|
|
143
|
+
assert_validate_string_format(:ip_addr, data[1], data[0])
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def test_string_validate_hostname_format
|
148
|
+
[
|
149
|
+
[ "a.b.c.d", true, ],
|
150
|
+
[ "0invalid", false, ],
|
151
|
+
].each_with_index do |data, i|
|
152
|
+
assert_validate_string_format(:hostname, data[1], data[0])
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_failed_validation_reset_sanitized_object
|
157
|
+
s = Respect::StringSchema.define equal_to: "42"
|
158
|
+
assert_schema_validate(s, "42")
|
159
|
+
assert_equal("42", s.sanitized_object)
|
160
|
+
assert_schema_invalidate(s, "51")
|
161
|
+
assert_equal(nil, s.sanitized_object)
|
162
|
+
end
|
163
|
+
|
164
|
+
def test_allow_nil
|
165
|
+
s = Respect::StringSchema.new(allow_nil: true)
|
166
|
+
assert_schema_validate s, nil
|
167
|
+
assert_equal(nil, s.sanitized_object)
|
168
|
+
assert_schema_validate s, "42"
|
169
|
+
assert_equal("42", s.sanitized_object)
|
170
|
+
assert_schema_validate s, 42
|
171
|
+
assert_equal("42", s.sanitized_object)
|
172
|
+
end
|
173
|
+
|
174
|
+
def test_disallow_nil
|
175
|
+
s = Respect::StringSchema.new
|
176
|
+
assert !s.allow_nil?
|
177
|
+
exception = assert_exception(Respect::ValidationError) { s.validate(nil) }
|
178
|
+
assert_match exception.message, /\bStringSchema\b/
|
179
|
+
assert_equal(nil, s.sanitized_object)
|
180
|
+
assert_schema_validate s, "42"
|
181
|
+
assert_equal("42", s.sanitized_object)
|
182
|
+
assert_schema_validate s, 42
|
183
|
+
assert_equal("42", s.sanitized_object)
|
184
|
+
end
|
185
|
+
|
186
|
+
def test_allow_nil_with_constraint
|
187
|
+
s = Respect::StringSchema.new(allow_nil: true, equal_to: "42")
|
188
|
+
assert_schema_validate s, nil
|
189
|
+
assert_equal(nil, s.sanitized_object)
|
190
|
+
assert_schema_validate s, "42"
|
191
|
+
assert_equal("42", s.sanitized_object)
|
192
|
+
assert_schema_invalidate s, "51"
|
193
|
+
assert_equal(nil, s.sanitized_object)
|
194
|
+
end
|
195
|
+
|
196
|
+
private
|
197
|
+
|
198
|
+
def assert_validate_string_format(format, result, doc)
|
199
|
+
s = Respect::StringSchema.new(format: format)
|
200
|
+
assert_nil s.sanitized_object
|
201
|
+
assert_schema_validation_is result, s, doc, "validate '#{doc}'"
|
202
|
+
if result
|
203
|
+
assert s.sanitized_object.is_a?(String), "is a String for '#{doc}'"
|
204
|
+
assert_equal doc, s.sanitized_object, "sanitize '#{doc}'"
|
205
|
+
else
|
206
|
+
assert_nil s.sanitized_object
|
207
|
+
end
|
208
|
+
end
|
209
|
+
end
|