Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate if we have an array of integers #15572

Merged
merged 1 commit into from
Jul 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 29 additions & 25 deletions app/models/miq_request_workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ def validate(values)

# Check the disabled flag here so we reset the "error" value on each field
next if dialog_disabled || fld[:display] == :hide

value = get_value(values[f])
value = fld[:data_type] =~ /array_/ ? values[f] : get_value(values[f])

if fld[:required] == true
# If :required_method is defined let it determine if the field is value
Expand Down Expand Up @@ -187,35 +186,40 @@ def validate(values)
next if value.blank?

msg = "'#{fld[:description]}' in dialog #{dlg[:description]} must be of type #{fld[:data_type]}"
case fld[:data_type]
when :integer
unless is_integer?(value)
fld[:error] = msg; valid = false
end
when :float
unless is_numeric?(value)
fld[:error] = msg; valid = false
end
when :boolean
# TODO: do we need validation for boolean
when :button
# Ignore
when :array_integer
unless value.kind_of?(Array)
fld[:error] = msg; valid = false
end
else
data_type = Object.const_get(fld[:data_type].to_s.camelize)
unless value.kind_of?(data_type)
fld[:error] = msg; valid = false
end
end
validate_data_types(value, fld, msg, valid)
end
end

valid
end

def validate_data_types(value, fld, msg, valid)
case fld[:data_type]
when :integer
unless is_integer?(value)
fld[:error] = msg; valid = false
end
when :float
unless is_numeric?(value)
fld[:error] = msg; valid = false
end
when :boolean
# TODO: do we need validation for boolean
when :button
# Ignore
when :array_integer
unless value.kind_of?(Array)
fld[:error] = msg; valid = false
end
else
data_type = Object.const_get(fld[:data_type].to_s.camelize)
unless value.kind_of?(data_type)
fld[:error] = msg; valid = false
end
end
[valid, fld]
end

def get_dialog_order
@dialogs[:dialog_order]
end
Expand Down
54 changes: 54 additions & 0 deletions spec/models/miq_request_workflow_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,60 @@
end
end

describe '#validate_data_types?' do
%w(array_integer integer float array).each do |name|
let("fld_#{name}".to_sym) { {:error => nil, :data_type => name.to_sym} }
end

it 'valid with no error if integer and is an integer' do
results = workflow.validate_data_types(3, fld_integer, '', true)
expect(results).to include true
expect(results[1][:error]).to be_falsey
end

it 'invalid with an error message if integer and is not an integer' do
results = workflow.validate_data_types('a', fld_integer, 'bad data', true)
expect(results).to include false
expect(results[1][:error]).to eql 'bad data'
end

it 'valid with no error if float and is an float' do
results = workflow.validate_data_types(3.23, fld_float, '', true)
expect(results).to include true
expect(results[1][:error]).to be_falsey
end

it 'invalid with an error message if float and is not a float' do
results = workflow.validate_data_types('a.aa', fld_float, 'bad data', true)
expect(results).to include false
expect(results[1][:error]).to eql 'bad data'
end

it 'valid with no error if array_integer and is an array' do
results = workflow.validate_data_types([1, 2, 3], fld_array_integer, '', true)
expect(results).to include true
expect(results[1][:error]).to be_falsey
end

it 'invalid with an error message if array_integer is not an array' do
results = workflow.validate_data_types(3, fld_array_integer, 'bad data', true)
expect(results).to include false
expect(results[1][:error]).to eql 'bad data'
end

it 'valid with no error if array and is an array' do
results = workflow.validate_data_types([1, 'test'], fld_array, '', true)
expect(results).to include true
expect(results[1][:error]).to be_falsey
end

it 'invalid with an error message if array is not an array' do
results = workflow.validate_data_types(3, fld_array, 'bad data', true)
expect(results).to include false
expect(results[1][:error]).to eql 'bad data'
end
end

describe '#cast_value' do
it 'integer' do
expect(workflow.cast_value(1, :integer)).to eq(1)
Expand Down