Skip to content

Commit

Permalink
Merge pull request bbatsov#4 from tsvetomilam/master
Browse files Browse the repository at this point in the history
Added date format validator
  • Loading branch information
bbatsov committed Feb 6, 2012
2 parents 42dd8e0 + 1935f86 commit 477f268
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/activerecord-extra-validators.rb
Expand Up @@ -3,3 +3,4 @@
require 'validators/url_format_validator'
require 'validators/name_format_validator'
require 'validators/boolean_validator'
require 'validators/date_format_validator'
51 changes: 51 additions & 0 deletions lib/validators/date_format_validator.rb
@@ -0,0 +1,51 @@
class DateFormatValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
record.errors[attribute] << (options[:message] || 'is not a valid date format') unless value.blank? || DateFormatValidator.valid_date_format?(value)
end

def self.valid_date_format?(value)
valid_parsed_date_format?(value) || valid_localized_date_format?(value)
end

private
# Check whether given string can be parsed to a valid date using Date.parse
def self.valid_parsed_date_format?(value)
begin
parsed_date = Date.parse(value)
rescue
# do nothing
end

parsed_date.present?
end

# Check whether given string can be parsed with some of the date.format options given in the localization settings
def self.valid_localized_date_format?(value)
parsed_date = nil

I18n.t('date.formats').each_value do |format|
begin
parsed_date = Date.strptime(value, format)
rescue
#do nothing
end
end

parsed_date.present?
end
end

if defined?(::Rails)
module ClientSideValidations::Middleware
class DateFormat < Base
def response
if DateFormatValidator.valid_date_format?(request.params[:date])
self.status = 200
else
self.status = 404
end
super
end
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -12,6 +12,7 @@
ActiveRecord::Schema.define(:version => 1) do
create_table :mocked_users, :force => true do |t|
t.string :name
t.string :birth_date
t.string :email
t.string :url
t.string :active
Expand Down
2 changes: 1 addition & 1 deletion spec/validators/boolean_validator_spec.rb
Expand Up @@ -3,7 +3,7 @@
describe BooleanValidator do
before(:all) do
class UserWithBoolean < ActiveRecord::Base
set_table_name 'mocked_users'
self.table_name = 'mocked_users'

validates :active, boolean: true
end
Expand Down
51 changes: 51 additions & 0 deletions spec/validators/date_format_validator_spec.rb
@@ -0,0 +1,51 @@
require 'spec_helper'

describe DateFormatValidator do
before(:all) do
class UserWithDate < ActiveRecord::Base
self.table_name = 'mocked_users'

validates :birth_date, date_format: true
end
end

let(:user) { UserWithDate.new }

it 'does not accept invalid date format' do
user.birth_date = 'invalid'

user.should_not be_valid
end

it 'accepts valid date format accepted by Date.parse' do
user.birth_date = '2010-03-21'

user.should be_valid
end

it 'accepts date format specified in localization date.formats options' do
I18n.stub(:t).with('date.formats').and_return(custom_format: '%Y-%d-%m')

user.birth_date = '2010-21-03'

user.should be_valid
end

it 'does not accept date formats not accepted by Date.parse and not found in localization options' do
user.birth_date = '2010-21-03'

user.should_not be_valid
end

it 'does not validate empty values' do
user.birth_date = ''

user.should be_valid
end

it 'does not validate nil values' do
user.birth_date = nil

user.should be_valid
end
end
2 changes: 1 addition & 1 deletion spec/validators/email_format_validator_spec.rb
Expand Up @@ -3,7 +3,7 @@
describe EmailFormatValidator do
before(:all) do
class UserWithEmail < ActiveRecord::Base
set_table_name 'mocked_users'
self.table_name = 'mocked_users'

validates :email, email_format: true
end
Expand Down
2 changes: 1 addition & 1 deletion spec/validators/name_format_validator_spec.rb
Expand Up @@ -3,7 +3,7 @@
describe NameFormatValidator do
before(:all) do
class UserWithName < ActiveRecord::Base
set_table_name 'mocked_users'
self.table_name = 'mocked_users'

validates :name, name_format: true
end
Expand Down
6 changes: 1 addition & 5 deletions spec/validators/url_format_validator_spec.rb
Expand Up @@ -3,7 +3,7 @@
describe UrlFormatValidator do
before(:all) do
class UserWithUrl < ActiveRecord::Base
set_table_name 'mocked_users'
self.table_name = 'mocked_users'

validates :url, url_format: true
end
Expand All @@ -12,16 +12,12 @@ class UserWithUrl < ActiveRecord::Base
let(:user) { UserWithUrl.new }

it 'does not accept invalid url format' do
user.validates_with UrlFormatValidator, :attributes => :url

user.url = 'invalid'

user.should_not be_valid
end

it 'accepts valid url format' do
user.validates_with UrlFormatValidator, :attributes => :url

user.url = 'test.com'

user.should be_valid
Expand Down

0 comments on commit 477f268

Please sign in to comment.