Sha256: 41400f95df9e68dbae7f01282f5d9d89869d9db3ca519a44f5ae178a0489e602

Contents?: true

Size: 1.52 KB

Versions: 6

Compression:

Stored size: 1.52 KB

Contents

require 'spec_helper'
require 'integration/automatic_validation/spec_helper'

describe 'Inferred validations' do
  it "allow overriding a single error message" do
    custom_boat = Class.new do
      include DataMapper::Resource
      property :id,   DataMapper::Property::Serial
      property :name, String, :required => true, :message => "This boat must have name"
    end
    boat = custom_boat.new
    boat.should_not be_valid
    boat.errors.on(:name).should == [ 'This boat must have name' ]
  end

  it "should have correct error messages" do
    custom_boat = Class.new do
      include DataMapper::Resource
      property :id,   DataMapper::Property::Serial
      property :name, String, :required => true, :length => 5..20, :format => /^[a-z]+$/,
               :messages => {
                 :presence => "This boat must have name",
                 :length => "Name must have at least 4 and at most 20 chars",
                 :format => "Please use only small letters"
               }
    end

    boat = custom_boat.new
    boat.should_not be_valid
    boat.errors.on(:name).should == [ 'This boat must have name' ]

    boat.name = "%%"
    boat.should_not be_valid
    boat.errors.on(:name).should == [
      'Name must have at least 4 and at most 20 chars',
      'Please use only small letters',
    ]

    boat.name = "%%asd"
    boat.should_not be_valid
    boat.errors.on(:name).should == [ 'Please use only small letters' ]

    boat.name = "superboat"
    boat.should be_valid
    boat.errors.on(:name).should be_nil
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
dm-validations-1.1.0 spec/integration/automatic_validation/custom_messages_for_inferred_validation_spec.rb
dm-validations-1.1.0.rc3 spec/integration/automatic_validation/custom_messages_for_inferred_validation_spec.rb
dm-validations-1.1.0.rc2 spec/integration/automatic_validation/custom_messages_for_inferred_validation_spec.rb
dm-validations-1.1.0.rc1 spec/integration/automatic_validation/custom_messages_for_inferred_validation_spec.rb
dm-validations-1.0.2 spec/integration/automatic_validation/custom_messages_for_inferred_validation_spec.rb
dm-validations-1.0.1 spec/integration/automatic_validation/custom_messages_for_inferred_validation_spec.rb