forked from bbatsov/activerecord-extra-validators
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request bbatsov#4 from tsvetomilam/master
Added date format validator
- Loading branch information
Showing
8 changed files
with
108 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters