#859 ✓resolved
eydaimon

format validate doesn't work on numerical values

Reported by eydaimon | May 22nd, 2009 @ 10:53 AM | in 0.10.0

## code!
usr/bin/env ruby
require 'rubygems'
require 'dm-core'
require 'dm-validations'


DataMapper.setup(:default, "sqlite3:///#{Dir.pwd}/test.db")
DataObjects::Sqlite3.logger = DataObjects::Logger.new(STDOUT, 0)

class Foo
  include DataMapper::Resource
  property :id, Serial
  property :foo, Integer, :format => /[0-9]+/

end

DataMapper.auto_migrate!

f = Foo.new(:foo => 33)
f.save
p f.errors
## stdout
Fri, 22 May 2009 04:37:42 GMT ~ debug ~ (0.003803) DROP TABLE IF EXISTS "foos"
Fri, 22 May 2009 04:37:42 GMT ~ debug ~ (0.000074) PRAGMA table_info('foos')
Fri, 22 May 2009 04:37:42 GMT ~ debug ~ (0.000060) SELECT sqlite_version(*)
Fri, 22 May 2009 04:37:42 GMT ~ debug ~ (0.004777) CREATE TABLE "foos" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "foo" INTEGER)
#<DataMapper::Validate::ValidationErrors:0x28b2d48c @errors={:foo=>["Foo has an invalid format"]}>

Comments and changes to this ticket

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) May 27th, 2009 @ 04:02 PM

    I haven't confirmed this, but the format checking done in dm-validations should probably call to_s on the value prior to comparing it against the Regexp.

    Using =~ against a Ruby Integer will always return false since Integer inherits Object#=~, which is hard-coded to always be false.

  • Martin Gamsjaeger (snusnu)

    Martin Gamsjaeger (snusnu) July 9th, 2009 @ 05:20 PM

    • Assigned user set to “Martin Gamsjaeger (snusnu)”
    • State changed from “unconfirmed” to “accepted”
    • Milestone set to 0.10.0

    As can be seen below, this is still an issue in current next branch.

    require 'rubygems'
    require 'dm-core'
    require 'dm-validations'
    require 'spec'
    
    DataMapper.setup(:default, "sqlite3:///#{Dir.pwd}/test.db")
    DataObjects::Sqlite3.logger = DataObjects::Logger.new(STDOUT, 0)
    
    class Foo
      include DataMapper::Resource
      property :id, Serial
      property :foo, Integer, :format => /[0-9]+/
    end
    
    describe ":format validation on numerical values " do
      before(:all) do
        DataMapper.auto_migrate!
      end
      it "should allow to validate numerical values" do
        f = Foo.new(:foo => 33)
        f.save.should be_true
        f.errors.should be_empty
      end
    end
    
    # mungo:snippets snusnu$ spec -cfs 859.rb 
    # 
    # :format validation on numerical values 
    # Thu, 09 Jul 2009 22:19:31 GMT ~ debug ~ (0.000143) SELECT sqlite_version(*)
    # Thu, 09 Jul 2009 22:19:31 GMT ~ debug ~ (0.003289) DROP TABLE IF EXISTS "foos"
    # Thu, 09 Jul 2009 22:19:31 GMT ~ debug ~ (0.000036) PRAGMA table_info("foos")
    # Thu, 09 Jul 2009 22:19:31 GMT ~ debug ~ (0.001744) CREATE TABLE "foos" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "foo" INTEGER)
    # - should allow to validate numerical values (FAILED - 1)
    # 
    # 1)
    # ':format validation on numerical values  should allow to validate numerical values' FAILED
    # expected true, got false
    # ./859.rb:21:
    # 
    # Finished in 0.010991 seconds
    # 
    # 1 example, 1 failure
    
  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) July 10th, 2009 @ 01:20 PM

    • State changed from “accepted” to “resolved”

    (from [79ebeb310a0a82b763eaf24988a9ab807140e981]) [dm-validations] :format validator now works with fixnums

    • Always calling #to_s on the value to validate leads to 4 spec errors, therefore why I added the check if value is_a?(Fixnum)

    [#859 state:resolved] http://github.com/datamapper/dm-more/commit/79ebeb310a0a82b763eaf24...

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Referenced by

Pages