dm-validations 0.9.5 → 0.9.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -25,6 +25,8 @@ lib/dm-validations/uniqueness_validator.rb
25
25
  lib/dm-validations/validation_errors.rb
26
26
  lib/dm-validations/version.rb
27
27
  lib/dm-validations/within_validator.rb
28
+ lib/dm-validations/formats/url.rb
29
+ lib/dm-validations/formats/email.rb
28
30
  spec/integration/absent_field_validator_spec.rb
29
31
  spec/integration/acceptance_validator_spec.rb
30
32
  spec/integration/auto_validate_spec.rb
data/Rakefile CHANGED
@@ -53,7 +53,7 @@ end
53
53
  desc 'Run specifications'
54
54
  Spec::Rake::SpecTask.new(:spec) do |t|
55
55
  t.spec_opts << '--options' << 'spec/spec.opts' if File.exists?('spec/spec.opts')
56
- t.spec_files = Pathname.glob(Pathname.new(__FILE__).dirname + 'spec/**/*_spec.rb')
56
+ t.spec_files = Pathname.glob((ROOT + 'spec/**/*_spec.rb').to_s)
57
57
 
58
58
  begin
59
59
  t.rcov = ENV.has_key?('NO_RCOV') ? ENV['NO_RCOV'] != 'true' : true
@@ -1,7 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'pathname'
3
3
 
4
- gem 'dm-core', '=0.9.5'
4
+ gem 'dm-core', '=0.9.6'
5
5
  require 'dm-core'
6
6
 
7
7
  dir = Pathname(__FILE__).dirname.expand_path / 'dm-validations'
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'pathname'
4
4
  require Pathname(__FILE__).dirname.expand_path + 'formats/email'
5
+ require Pathname(__FILE__).dirname.expand_path + 'formats/url'
5
6
 
6
7
  module DataMapper
7
8
  module Validate
@@ -14,6 +15,7 @@ module DataMapper
14
15
 
15
16
  FORMATS = {}
16
17
  include DataMapper::Validate::Format::Email
18
+ include DataMapper::Validate::Format::Url
17
19
 
18
20
  def initialize(field_name, options = {}, &b)
19
21
  super(field_name, options)
@@ -66,6 +68,7 @@ module DataMapper
66
68
  #
67
69
  # @details [Pre-defined Formats]
68
70
  # :email_address (format is specified in DataMapper::Validate::Format::Email)
71
+ # :url (format is specified in DataMapper::Validate::Format::Url)
69
72
  #
70
73
  # @example [Usage]
71
74
  # require 'dm-validations'
@@ -0,0 +1,20 @@
1
+ module DataMapper
2
+ module Validate
3
+ module Format
4
+ module Url
5
+
6
+ def self.included(base)
7
+ DataMapper::Validate::FormatValidator::FORMATS.merge!(
8
+ :url => [ Url, lambda { |field, value| '%s is not a valid URL'.t(value) }]
9
+ )
10
+ end
11
+
12
+ Url = begin
13
+ # Regex from http://www.igvita.com/2006/09/07/validating-url-in-ruby-on-rails/
14
+ /(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$)/ix
15
+ end
16
+
17
+ end # module Url
18
+ end # module Format
19
+ end # module Validate
20
+ end # module DataMapper
@@ -89,6 +89,7 @@ module DataMapper
89
89
  def ==(other)
90
90
  self.class == other.class &&
91
91
  self.field_name == other.field_name &&
92
+ self.class == other.class &&
92
93
  self.if_clause == other.if_clause &&
93
94
  self.unless_clause == other.unless_clause &&
94
95
  self.instance_variable_get(:@options) == other.instance_variable_get(:@options)
@@ -26,7 +26,7 @@ module DataMapper
26
26
  protected
27
27
 
28
28
  def default_error(property)
29
- "%s must be of type #{property.primitive.to_s}".t(Extlib::Inflection.humanize(@field_name))
29
+ "%s must be of type %s".t(Extlib::Inflection.humanize(@field_name), property.primitive)
30
30
  end
31
31
 
32
32
  end # class PrimitiveValidator
@@ -33,8 +33,8 @@ module DataMapper
33
33
  end
34
34
 
35
35
  def default_error(property)
36
- actual = boolean_type?(property) ? "nil" : "blank"
37
- "%s must not be #{actual}".t(Extlib::Inflection.humanize(@field_name))
36
+ actual = boolean_type?(property) ? "nil".t : "blank".t
37
+ "%s must not be %s".t(Extlib::Inflection.humanize(@field_name), actual)
38
38
  end
39
39
 
40
40
  # Is +property+ a boolean property?
@@ -1,5 +1,5 @@
1
1
  module DataMapper
2
2
  module Validations
3
- VERSION = "0.9.5"
3
+ VERSION = "0.9.6"
4
4
  end
5
5
  end
@@ -17,19 +17,19 @@ module DataMapper
17
17
  includes = @options[:set].include?(target.send(field_name))
18
18
  return true if includes
19
19
 
20
+ field_name = Extlib::Inflection.humanize(@field_name)
20
21
  if @options[:set].is_a?(Range)
21
22
  if @options[:set].first != -n && @options[:set].last != n
22
- message = "%s must be between #{@options[:set].first} and #{@options[:set].last}"
23
+ error_message = @options[:message] || "%s must be between %s and %s".t(field_name, @options[:set].first, @options[:set].last)
23
24
  elsif @options[:set].first == -n
24
- message = "%s must be less than #{@options[:set].last}"
25
+ error_message = @options[:message] || "%s must be less than %s".t(field_name, @options[:set].last)
25
26
  elsif @options[:set].last == n
26
- message = "%s must be greater than #{@options[:set].first}"
27
+ error_message = @options[:message] || "%s must be greater than %s".t(field_name, @options[:set].first)
27
28
  end
28
29
  else
29
- message = "%s must be one of [#{ @options[:set].join(', ')}]"
30
+ error_message = "%s must be one of [%s]".t(field_name, @options[:set].join(', '))
30
31
  end
31
32
 
32
- error_message = @options[:message] || message.t(Extlib::Inflection.humanize(@field_name))
33
33
  add_error(target, error_message , @field_name)
34
34
  return false
35
35
  end
@@ -10,6 +10,7 @@ describe DataMapper::Validate::FormatValidator do
10
10
  property :doc_no, String, :auto_validation => false
11
11
  property :email, String, :auto_validation => false
12
12
  property :username, String, :auto_validation => false
13
+ property :url, String, :auto_validation => false
13
14
 
14
15
  # this is a trivial example
15
16
  validates_format :doc_no, :with => lambda { |code|
@@ -17,13 +18,14 @@ describe DataMapper::Validate::FormatValidator do
17
18
  }
18
19
 
19
20
  validates_format :email, :as => :email_address
21
+ validates_format :url, :as => :url
20
22
 
21
23
  validates_format :username, :with => /[a-z]/, :message => 'Username must have at least one letter', :allow_nil => true
22
24
  end
23
25
  end
24
26
 
25
27
  def valid_attributes
26
- { :id => 1, :doc_no => 'A1234', :email => 'user@example.com' }
28
+ { :id => 1, :doc_no => 'A1234', :email => 'user@example.com', :url => 'http://example.com' }
27
29
  end
28
30
 
29
31
  it 'should validate the format of a value on an instance of a resource' do
@@ -81,6 +83,38 @@ describe DataMapper::Validate::FormatValidator do
81
83
 
82
84
  end
83
85
 
86
+ it 'should have a pre-defined URL format' do
87
+ bad = [ 'http:// example.com',
88
+ 'ftp://example.com',
89
+ 'http://.com',
90
+ 'http://',
91
+ 'test',
92
+ '...'
93
+ ]
94
+
95
+ good = [
96
+ 'http://example.com',
97
+ 'http://www.example.com',
98
+ ]
99
+
100
+ bol = BillOfLading.new(valid_attributes.except(:url))
101
+ bol.should_not be_valid
102
+ bol.errors.on(:url).should include('Url has an invalid format')
103
+
104
+ bad.map do |e|
105
+ bol.url = e
106
+ bol.valid?
107
+ bol.errors.on(:url).should include('Url has an invalid format')
108
+ end
109
+
110
+ good.map do |e|
111
+ bol.url = e
112
+ bol.valid?
113
+ bol.errors.on(:url).should be_nil
114
+ end
115
+
116
+ end
117
+
84
118
  describe 'with a regexp' do
85
119
  before do
86
120
  @bol = BillOfLading.new(valid_attributes)
@@ -1,9 +1,17 @@
1
1
  require 'pathname'
2
2
  require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
3
 
4
- describe DataMapper::Validate::GenericValidator do
5
- it "should have specs" do
6
- pending
7
- # FIXME do something, add specs
4
+ module DataMapper
5
+ module Validate
6
+ describe DataMapper::Validate::GenericValidator do
7
+ describe "#==" do
8
+ it "should return true if types and fields are equal" do
9
+ RequiredFieldValidator.new(:name).should == RequiredFieldValidator.new(:name)
10
+ end
11
+ it "should return false of types differ" do
12
+ RequiredFieldValidator.new(:name).should_not == UniquenessValidator.new(:name)
13
+ end
14
+ end
15
+ end
8
16
  end
9
17
  end
@@ -12,8 +12,6 @@ end
12
12
  describe DataMapper::Validate::PrimitiveValidator do
13
13
  it "should validate a property to check for the type" do
14
14
  b = Monica.new
15
- p b.valid?
16
- p b.errors
17
15
  b.should be_valid
18
16
 
19
17
  b.birth_date = 'ABC'
@@ -12,6 +12,18 @@ describe DataMapper::Validate do
12
12
  end
13
13
  end
14
14
 
15
+ describe '#validations' do
16
+ it 'should support more different validations of a different type' do
17
+ number_of_validators_before = Yacht.validators.contexts[:default].length
18
+ class Yacht
19
+ validates_is_unique :name
20
+ end
21
+ number_of_validators_after = Yacht.validators.contexts[:default].length
22
+ (number_of_validators_after - number_of_validators_before).should == 1
23
+ end
24
+
25
+ end
26
+
15
27
  it 'should respond to save' do
16
28
  Yacht.new.should respond_to(:save)
17
29
  end
@@ -21,7 +21,7 @@ describe DataMapper::Validate::WithinValidator do
21
21
  validates_within :between, :set => (10..20)
22
22
  end
23
23
 
24
- class Reciever
24
+ class Receiver
25
25
  include DataMapper::Resource
26
26
  property :id, Integer, :serial => true
27
27
  property :holder, String, :auto_validation => false, :default => 'foo'
@@ -48,7 +48,7 @@ describe DataMapper::Validate::WithinValidator do
48
48
  end
49
49
 
50
50
  it "should validate a value by its default" do
51
- tel = Reciever.new
51
+ tel = Receiver.new
52
52
  tel.should be_valid
53
53
  end
54
54
  end
@@ -9,7 +9,7 @@ def load_driver(name, default_uri)
9
9
  lib = "do_#{name}"
10
10
 
11
11
  begin
12
- gem lib, '=0.9.5'
12
+ gem lib, '>=0.9.5'
13
13
  require lib
14
14
  DataMapper.setup(name, ENV["#{name.to_s.upcase}_SPEC_URI"] || default_uri)
15
15
  DataMapper::Repository.adapters[:default] = DataMapper::Repository.adapters[name]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dm-validations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.5
4
+ version: 0.9.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guy van den Berg
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-08-26 00:00:00 -05:00
12
+ date: 2008-10-12 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - "="
22
22
  - !ruby/object:Gem::Version
23
- version: 0.9.5
23
+ version: 0.9.6
24
24
  version:
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: hoe
@@ -71,6 +71,7 @@ files:
71
71
  - lib/dm-validations/validation_errors.rb
72
72
  - lib/dm-validations/version.rb
73
73
  - lib/dm-validations/within_validator.rb
74
+ - lib/dm-validations/formats/url.rb
74
75
  - spec/integration/absent_field_validator_spec.rb
75
76
  - spec/integration/acceptance_validator_spec.rb
76
77
  - spec/integration/auto_validate_spec.rb