Skip to content

Commit

Permalink
validation specs
Browse files Browse the repository at this point in the history
  • Loading branch information
ajb committed Sep 26, 2013
1 parent 9355ada commit c23f136
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/models/formbuilder/response_field_date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def render_entry(value, opts = {})
end

def validate_response(value)
if !(DateTime.new(value['year'].to_i, value['month'].to_i, value['day'].to_i) rescue false)
if value['year'].to_i == 0 || !(DateTime.new(value['year'].to_i, value['month'].to_i, value['day'].to_i) rescue false)
"isn't a valid date."
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/formbuilder/response_field_price.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def render_entry(value, opts = {})
# only one is required, and it must consist only of numbers
def validate_response(value)
value.select { |k, v| k.in?(['dollars', 'cents']) && v.present? }.each do |k, v|
unless v =~ /^[0-9]+$/
unless (Float(v) rescue 0) > 0
return "isn't a valid price."
end
end
Expand Down
18 changes: 10 additions & 8 deletions lib/formbuilder/entry_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ def validate(record)
next
end

# Field-specific validation
add_error(@response_field.validate_response(@value))
if @record.value_present?(@response_field)
# Field-specific validation
add_error(@response_field.validate_response(@value))

SHARED_VALIDATION_METHODS.each do |method_name|
run_validation(method_name)
SHARED_VALIDATION_METHODS.each do |method_name|
run_validation(method_name)
end
end
end
end
Expand Down Expand Up @@ -56,21 +58,21 @@ def min_max_length
end

def min_max_length_characters
if @response_field.field_options["minlength"].present? && @value && (@value.length < @response_field.field_options["minlength"].to_i)
if @response_field.field_options["minlength"].present? && (@value.length < @response_field.field_options["minlength"].to_i)
return "is too short. It should be #{@response_field.field_options["minlength"]} characters or more."
end

if @response_field.field_options["maxlength"].present? && @value && (@value.length > @response_field.field_options["maxlength"].to_i)
if @response_field.field_options["maxlength"].present? && (@value.length > @response_field.field_options["maxlength"].to_i)
return "is too long. It should be #{@response_field.field_options["maxlength"]} characters or less."
end
end

def min_max_length_words
if @response_field.field_options["minlength"].present? && @value && (@value.scan(/\w+/).count < @response_field.field_options["minlength"].to_i)
if @response_field.field_options["minlength"].present? && (@value.scan(/\w+/).count < @response_field.field_options["minlength"].to_i)
return "is too short. It should be #{@response_field.field_options["minlength"]} words or more."
end

if @response_field.field_options["maxlength"].present? && @value && (@value.scan(/\w+/).count > @response_field.field_options["maxlength"].to_i)
if @response_field.field_options["maxlength"].present? && (@value.scan(/\w+/).count > @response_field.field_options["maxlength"].to_i)
return "is too long. It should be #{@response_field.field_options["maxlength"]} words or less."
end
end
Expand Down
182 changes: 181 additions & 1 deletion spec/lib/formbuilder/entry_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,186 @@

describe Formbuilder::EntryValidator do

pending
let!(:form) { FactoryGirl.create(:form) }
let!(:entry) { e = Entry.new(form: form); e.save(validate: false); e }

describe '#validate' do
before do
form.response_fields.create(label: "Text", type: "Formbuilder::ResponseFieldText", sort_order: 0, required: true)
end

it 'should always validate if there is no form' do
entry.should_not be_valid
entry.form = nil
entry.should be_valid
end

it 'should always validate if it was already submitted' do
entry.should_not be_valid
entry.submit!(true)
entry.should be_valid
end

it 'should validate if the :skip_validation flag is passed' do
entry.should_not be_valid
entry.skip_validation = true
entry.should be_valid
end
end

describe 'ResponseFieldText' do
let(:field) { form.response_fields.create(label: "Text", type: "Formbuilder::ResponseFieldText", sort_order: 0) }

it 'required' do
field.update_attributes(required: true)
entry.should_not be_valid
entry.save_response('Boo', field)
entry.should be_valid
end

it 'min/max length characters' do
field.update_attributes(field_options: { 'minlength' => '5', 'maxlength' => '10' })
entry.should be_valid
entry.save_response('Boo', field)
entry.should_not be_valid
entry.save_response('Boooo', field)
entry.should be_valid
entry.save_response('Boooooooooooooooooooo', field)
entry.should_not be_valid
entry.save_response('Boooo', field)
entry.should be_valid
end

it 'min/max length words' do
field.update_attributes(field_options: { 'minlength' => '2', 'maxlength' => '3', 'min_max_length_units' => 'words' })
entry.should be_valid
entry.save_response('Boo', field)
entry.should_not be_valid
entry.save_response('Boo hoo', field)
entry.should be_valid
entry.save_response('Boo hoo hoo', field)
entry.should be_valid
entry.save_response('Booo hoo hoo hooo', field)
entry.should_not be_valid
end
end

describe 'ResponseFieldAddress' do
let(:field) { form.response_fields.create(label: "Address", type: "Formbuilder::ResponseFieldAddress", sort_order: 0) }

it 'required' do
field.update_attributes(required: true)
entry.should_not be_valid
entry.save_response({'street' => '123'}, field)
entry.should be_valid
end
end

describe 'ResponseFieldDate' do
let(:field) { form.response_fields.create(label: "Date", type: "Formbuilder::ResponseFieldDate", sort_order: 0) }

it 'validates properly' do
entry.should be_valid
entry.save_response({'month' => '2'}, field)
entry.should_not be_valid
entry.save_response({'month' => '2', 'day' => '3'}, field)
entry.should_not be_valid
entry.save_response({'month' => '2', 'day' => '3', 'year' => '2011'}, field)
entry.should be_valid

# @todo automatically add 20** or 19** to dates
end
end

describe 'ResponseFieldEmail' do
let(:field) { form.response_fields.create(label: "Email", type: "Formbuilder::ResponseFieldEmail", sort_order: 0) }

it 'validates properly' do
entry.should be_valid
entry.save_response('a', field)
entry.should_not be_valid
entry.save_response('a@a.com', field)
entry.should be_valid
end
end

describe 'ResponseFieldNumber' do
let(:field) { form.response_fields.create(label: "Number", type: "Formbuilder::ResponseFieldNumber", sort_order: 0) }

it 'min/max' do
field.update_attributes(field_options: { 'min' => '5', 'max' => '10' })
entry.should be_valid
entry.save_response('1', field)
entry.should_not be_valid
entry.save_response('4.99999', field)
entry.should_not be_valid
entry.save_response('5.01', field)
entry.should be_valid
entry.save_response('10.00', field)
entry.should be_valid
entry.save_response('10.9', field)
entry.should_not be_valid
end

it 'integer only' do
field.update_attributes(field_options: { 'integer_only' => true })
entry.should be_valid
entry.save_response('1.2', field)
entry.should_not be_valid
entry.save_response('1.0', field)
entry.should_not be_valid
entry.save_response('1', field)
entry.should be_valid
end
end

describe 'ResponseFieldPrice' do
let(:field) { form.response_fields.create(label: "Price", type: "Formbuilder::ResponseFieldPrice", sort_order: 0) }

it 'validates properly' do
entry.should be_valid
entry.save_response({ 'dollars' => 'a' }, field)
entry.should_not be_valid
entry.save_response({ 'dollars' => '0' }, field)
entry.should_not be_valid
entry.save_response({ 'dollars' => '1a' }, field)
entry.should_not be_valid
entry.save_response({ 'dollars' => '1' }, field)
entry.should be_valid
entry.save_response({ 'cents' => 'a' }, field)
entry.should_not be_valid
entry.save_response({ 'cents' => '0' }, field)
entry.should_not be_valid
entry.save_response({ 'cents' => '1a' }, field)
entry.should_not be_valid
entry.save_response({ 'cents' => '1' }, field)
entry.should be_valid
entry.save_response({ 'dollars' => '3a', 'cents' => '1' }, field)
entry.should_not be_valid
end
end

describe 'ResponseFieldTime' do
let(:field) { form.response_fields.create(label: "Time", type: "Formbuilder::ResponseFieldTime", sort_order: 0) }

it 'validates properly' do
entry.should be_valid
entry.save_response({'hours' => '0'}, field)
entry.should_not be_valid
entry.save_response({'hours' => '0', 'minutes' => '1'}, field)
entry.should_not be_valid
entry.save_response({'hours' => '1', 'minutes' => '3'}, field)
entry.should be_valid
end
end

describe 'ResponseFieldWebsite' do
let(:field) { form.response_fields.create(label: "Website", type: "Formbuilder::ResponseFieldWebsite", sort_order: 0) }

it 'validates properly' do
entry.should be_valid
# @todo add real validations for website field?
end
end

end

0 comments on commit c23f136

Please sign in to comment.