Skip to content

Commit 0074284

Browse files
Josh MostafaiNecas
Josh Mostafa
authored andcommitted
Allow ArrayValidator to use other validators to validate array elements
1 parent 90ea8d1 commit 0074284

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

lib/apipie/validator.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,13 @@ def validate_item(value)
225225

226226
def has_valid_type?(value)
227227
if @items_type
228-
value.kind_of?(@items_type)
228+
item_validator = BaseValidator.find('', @items_type, nil, nil)
229+
230+
if item_validator
231+
item_validator.valid?(value)
232+
else
233+
value.kind_of?(@items_type)
234+
end
229235
else
230236
true
231237
end

spec/lib/validators/array_validator_spec.rb

+28-8
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,38 @@ module Apipie::Validator
2424
end
2525

2626
context "with a constraint on items type" do
27-
let(:validator) { ArrayValidator.new(param_desc, Array, :of => String) }
27+
let(:validator) { ArrayValidator.new(param_desc, Array, :of => type) }
2828

29-
it "accepts array of specified type" do
30-
expect(validator.validate(['string1', 'string2'])).to eq(true)
31-
end
29+
context "String" do
30+
let(:type) { String }
3231

33-
it "accepts empty array" do
34-
expect(validator.validate([])).to eq(true)
32+
it "accepts array of specified type" do
33+
expect(validator.validate(['string1', 'string2'])).to eq(true)
34+
end
35+
36+
it "accepts empty array" do
37+
expect(validator.validate([])).to eq(true)
38+
end
39+
40+
it "does not accepts array with other types" do
41+
expect(validator.validate(['string1', true])).to eq(false)
42+
end
3543
end
3644

37-
it "does not accepts array with other types" do
38-
expect(validator.validate(['string1', true])).to eq(false)
45+
context ":number" do
46+
let(:type) { :decimal }
47+
48+
it "accepts array of specified type" do
49+
expect(validator.validate([12, '14'])).to eq(true)
50+
end
51+
52+
it "accepts empty array" do
53+
expect(validator.validate([])).to eq(true)
54+
end
55+
56+
it "does not accepts array with other types" do
57+
expect(validator.validate([12, 'potato'])).to eq(false)
58+
end
3959
end
4060
end
4161

0 commit comments

Comments
 (0)