Giter VIP home page Giter VIP logo

date_validator's Introduction

date_validator's People

Contributors

avjaarsveld avatar ball-hayden avatar cryo28 avatar delphaber avatar dot avatar fny avatar greyblake avatar greysteil avatar jolim avatar jrallison avatar jvperrin avatar lapluviosilla avatar lardawge avatar markquezada avatar migol avatar mortadaak avatar mrcasals avatar oriolgual avatar pmukerji avatar reiz avatar robworley avatar roy-mao avatar rudionrails avatar softwarebouwer avatar t3hpr1m3 avatar toshi-kawanishi avatar txus avatar vjustov avatar xiaok avatar yurijmi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

date_validator's Issues

doesn't seem to work with just :before

I have this in my model:

  validates :date_completed, :date => {
    :before => Time.now, :message => 'must be before today'
  }

But it doesn't matter what I enter for a date, I don't get any validation error. If I change it to:

  validates :date_completed, :date => {
    :after => Time.now, :message => 'must be after today'
  }

And enter a date before today, it works as expected. Any ideas?

Skip { after: :other_attribute } validation if other_attribute is nil

๐Ÿฆ„ Feature Request

Is your feature request related to a problem?

When I'm using the after syntax using an attribute:

validates :expiration_date, date: { after: :packaging_date }

but then packaging_date is a nilable attribute, the validation fails when packaging_date is blank.

Describe the solution you'd like

The more sensible behaviour to me is if the after validation is only applied in case where packaging_date is present. If packaging_date is blank, the date validation should just check the date format of expiration_date.
If this makes sense with the authors and the users, I'm totally willing to put in a PR!

datetime check in rails views

Dears,

i was looking for Date and DateTime Checks in my Views :

to check if Time.now.before? @post.publish_at or Time.now.after? @post.publish_at or any other validations available.

Not working in certain time zones

Our tests run in random time zones. In some of the time zones, the date validator does not seem to work:

The code:

ap @appointment.valid?
ap @appointment.errors.full_messages.join(', ')
ap @appointment.start_date
ap @appointment.end_date

The output:

false
"End date must be a date on or after 2015-02-24"
Tue, 24 Feb 2015 01:11:09 KRAT +07:00
Tue, 24 Feb 2015 02:11:09 KRAT +07:00

Not really ORM-Agnostic

This gem doesn't work for Sequel, not outside of the box at least. The EachValidator object is AFAIK only defined in Rails, and is not part of the ActiveModel spec.

One could remove the ORM-agnostic description (unless there's morethan ActiveRecord this gem supports) or build a validation method to be added to Sequel::Model when loading the ValidationHelpers plugin.

Validating one date is after the other doesn't seem to work

I'm running Ruby 1.9.3-p0, with Rails 3.1.3 on OS X 10.6.8.

My model contains the following:

validates_presence_of :start_date, :end_date
validates :start_date, :date => { :before => Time.now + 1.week }
validates :end_date, :date => { :after => :start_date }

However when I try checking this functionality, I get the following error message:

1.9.3p0 :037 > report = Report.new(start_date: "10/01/2012", end_date: "11/01/2012")
 => #<Report end_date: "11/01/2012", start_date: "10/01/2012"> 
1.9.3p0 :038 > report.valid?
 => false 
1.9.3p0 :039 > report.errors
 => #<ActiveModel::Errors:0x0000010285fa90 @base=#<Report end_date: "11/01/2012", start_date: "10/01/2012">,   @messages={:end_date=>["must be after 10/01/2012"]}>

When making a string comparison of "10/01/2012" < "11/01/2012" it returns true, or a date comparison of Date.parse("10/01/2012") < Date.parse("11/01/2012") it returns true as well.

Fail to detect errors when invalid strings are provided

I found two situations where it fails to find errors, and I created two test cases to exhibit the problem:

    it "complains when an invalid date is provided" do
        I18n.backend.reload!
        TestRecord.validates :expiration_date, date: true

        model = TestRecord.new("not a date")
        model.valid?.must_equal false
        model.errors[:expiration_date].must_equal(["is not a date"])
      end
    it "complains when an invalid date is provided and option values evaluates to a negative value" do
        I18n.backend.reload!
        TestRecord.validates :expiration_date, date: { before: 50.years.ago }

        model = TestRecord.new("not a date")
        model.valid?.must_equal false
        model.errors[:expiration_date].must_equal(["is not a date"])
    end

I could not manage to fix them but, for fixing the second case I would start looking at:
https://github.com/codegram/date_validator/blob/master/lib/active_model/validations/date_validator.rb#L87.

"not_a_date".to_i

0

and

50.years.ago.to_i

-177126104

change validation example

hey guys,

first of all, thanks for this gem. it's really helpful!

i'd like to suggest that you change the validation example in your readme.
currently it says:

validates :expiration_date,
  :date => { :after => Time.now, :before => Time.now + 1.year }

the problem here is, that when using date_validator in rails production mode, the value of Time.now would be cached.

maybe something like:

validates :expiration_date, :date => {
  :after => lambda { Time.now, }
  :before => lambda { Time.now + 1.year } }

would help people to understand why using a lambda is more appropriate.

I18n conflicts with Activemodel I18n

ActiveModel contains own definition of errors.messages.equal_to, as seen in here. date_validator redefines it here which causes breakage. E.g. I get following in my specs:

    I18n::MissingInterpolationArgument:
       missing interpolation argument :date in "must be equal to %{date}" ({:model=>"Site", :attribute=>"Monthly payment", :value=>1.0, :count=>0} given)

ActiveModel variant is "must be equal to %{count}" and it works if date_validator is not loaded.

Does :before and :after support another field in the table?

Could I do this?
validates :started_at :date => { :before => :finished_at }
validates :finished_at :date => { :before => :started_at }

If so, could it be mentioned in the documentation please :D
If not, could it be added please ;-)

Thanks mate, keep up the great stuff!

Validation doesn't work if the value is represented as a string

We have an object that can hold a value which is just a string. Depends on the STI Child of the object, a specific validation is running.

t.string :value, null: true

class SmartField < ApplicationRecord
  validates :value, date: true
end


SmartField.create! value: Date.today
ActiveRecord::RecordInvalid: Validatie mislukt: Value is geen datum

this looks very odd to me and was unexpected.

      def is_time?(object)
        object.is_a?(Time) || (defined?(Date) and object.is_a?(Date)) || (defined?(ActiveSupport::TimeWithZone) and object.is_a?(ActiveSupport::TimeWithZone))
      end

do we need a string check, aka Date.parse rescue false?

Fails on :allow_blank => true

Hello.

I was trying to use gem to do some validation, and ran into a problem:

trying to do validation like so:

validates :delivery_date, :allow_blank => true, :date => {:after => Date.today, :before => Date.today + 30.days }

I keep getting "invalid date" error if the attribute is blank ( which it should allow for if the user didn't specify anything )

Am I doing something wrong?

v0.9.0 introduced a side effect about I18n.locale_available?

I noticed that return value of I18n.locale_available? method changed after upgrade from v0.7.1 to v0.9.0.

Our Rails application defines :en and :ja locales in config/locales/*.yml.

with v0.7.1:

> I18n.locale_available?(:en)
=> true
> I18n.locale_available?(:ja)
=> true
> I18n.locale_available?(:de)
=> false

with v0.9.0:

> I18n.locale_available?(:en)
=> true
> I18n.locale_available?(:ja)
=> true
> I18n.locale_available?(:de)
=> true # CHANGED

I doubting this introduced by 585cddd.
Is this behaviour change intended?

Add a changelog

Hey there ๐Ÿ‘‹

Thanks for the work from the maintainers on this Gem ๐Ÿ‘

I've been updating the version of date_validator in our application, and have spent some time debugging why some custom error messages have changed.

I tracked this down to changes to the locale keys, and made the appropriate changes in my application ๐ŸŽ‰

Something that would have made this easier however, for me and others, would have been to check a change log for the project, so I propose this is added at the project root?

I've drafted a quick starter based on what I've read from the 0.10.0 release here:

# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased

## 0.10.0 - 2020-03-03
### Changed
* Breaking changes to locale keys.
* Drop Ruby 1.9.x support, minimum Ruby version now >= 2.2
* Change locale path to Rails Engine default

Hope this helps!

Complain of missing translation

I get the following:

Translation missing: en.activerecord.errors.models.affiliation.attributes.start.before

When I have this in my model:

class Affiliation
validates :start, :date => { :before => :end }
end

Rails 5

Is this compatible with rails 5?

No LICENSE file

The Copyright says "See LICENSE for details.", but no license file is provided.

Adding locales to the the load path affects I18n.available_locales

The line below breaks the behavior of I18n.available_locales:

I18n.load_path += Dir[File.expand_path(File.join(File.dirname(FILE), '../locales', '*.yml')).to_s]

That is undesired behavior as some application dynamically figure out the available locales. Providing translations for DateValidator doesn't mean that the rest of the application is translated. None of the common gems are that intrusive and they provide at most english translation.
You can provide the locale files in the gem but do not load them all.

Thanks
Heinrich

add :allow_blank? option

I'd like to use it like this:

validates :date_of_birth, :date => {:after => ..., :before => ..., :message => 'is invalid' }, :allow_blank? => true , :if => :check_date?

Update activemodel version dependency

Trying to upgrade to Rails 3.2.11 and bundle is failing because date_validator depends on activemodel ~> 3.0.0. Please update gem dependency. Cheers.

Proc in error details

๐Ÿž Bug Report

When you set a value for after (or any other comparison option) with a Proc object, the value appearing in error details is an inspect of the Proc itself instead of being the result of Proc.call

Steps to reproduce

How can we reproduce the bug? Example:

class Model
  validates :date_field, { after: Proc.new { Time.now } }
end

m = Model.new
m.valid?
puts m.errors.details[:date_field}

# => { ..., after: #<Proc:0x0000... > }

Current Behavior

Which broken behavior are you experiencing?

String dump of Proc value from options

Expected behavior

We should have the actual value returned by Proc.call

Input Code

see above

Environment

Env doesnt matter for this issue

Possible Solution

add option_value in ***options.merge() in validations/date_validator.rb

Is this gem still maintained ?

Hi,

I'm seeing lot of PR and not much activity. Is this gem still maintained?

Are you looking for a new maintainer? How can we help make this amazing gem improved?

Regards

Differentiating between `blank` and `not_a_date`

Hello,

I'm trying to set up date validation on a field that requires a valid date be input.

validates :dob, date: true, presence: true renders both the blank and not_a_date messages.

I am working on a fork that would change the functionality so that passing:

  • nil to validates :dob, date: true would return en.errors.messages.blank
  • 'bob' to validates :dob, date: true would return en.errors.messages.not_a_date

Is this something that would be welcomed as a pull request or should I keep it separate?

Need to check time is a valid time

I am splitting date and time into separate fields in my model and need to validate them separately.

As part of the validation I need to allow nil/blank start_time but if there is something then I need to make sure it is a valid time.

I tried:

validates :start_time, :allow_nil => true, :date => { :after_or_equal_to => Proc.new { Time.now.beginning_of_day }, :before_or_equal_to => Proc.new { Time.now.end_of_day } }

and I suspect it is because I think ruby just defaults a date onto the front of times, (eg 2000-01-01 or something) which means that the start_time will never be between [in effect] today at midnight 00:00 and tonight at midnight 23:59 because it is using a date value from 12 years ago.

Can you offer any guidance how I can validate that a time is a time but doesn't matter when during the day?

I need it to fail if someone manages to send "random string" to my model.

Thanks

Btw I'm just converting over from using validates_timeliness and have found your gem to be far more straight forward to apply....up until this issue with validating a time

Add way to check is a string is a valid date

Is it possible to add a way just to check if a given string is a valid date?

validates :release_date, date: true

@product.release_date = "1/1/ab"
@product.should_not be_valid

Failure/Error: @product.should_not be_valid
       expected #<Product id: nil, name: "Assassin's Creed", properties: {"platform"=>"PS3", "release_date"=>"31/12/ab"}> not to be valid

Wrong type of error in different cases

Case 1:
validates :birth_date, :date => true, :presence => true
When not entering anything it reports: 'not a date', which is kind of true, but a better error would be the rails 'must be given'

Case 2:
validates :birth_date, :date => true, :presence => true, :allow_nil => true
Added the allow_nil clause to fix previous error. Now that works but the following case is incorrect
Entering '28-04-89' gives also 'must be given' which is incorrect, correct error should be 'not a date'...

Ruby 1.9.2, Rails 3.0.3, RSpec

I have validations like:
:date => { :after_or_equal_to => lambda { Date.today } }

And I have failing tests:
110) MessageAssignment should *************************
Failure/Error: unread_message = Factory(:message_assignment)
wrong number of arguments (1 for 0)
# ./app/models/message.rb:16:in block in <class:Message>' # ./spec/models/message_assignment_spec.rb:44:inblock (2 levels) in <top (required)>'

(line 16 is a line with date validation)

On Ruby EE everything works fine.

Don't work with Date

Raises "undefined method `to_i' for ... :Date" when valildatable value is a Date
rails 3 beta4

time range

is it possible to do something like that:
validates :end_at, :date => { :after => :start_at, :before => Proc.new { :start_at + 9.days } }

Thanks in advance for a short answer.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.