1

I am using the Regex to validate the date format(YYYY-MM-DD) using this gem(https://github.com/nicolasblanco/rails_param)

param! :start_date, Date, format: /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/, required: true, message: "Query start_date is not specified and invalid"

For right date format also it is saying error Query start_date is not specified

A sample query http://localhost:3005/data?start_date=2008-01-27&end_date=2009-01-27

It's not working

Sam
  • 5,040
  • 12
  • 43
  • 95
  • Also note that this validation, even if it has the correct year syntax, will not make sure the date is correct. You can still receive `2019-02-31` which is not a date that exists but will be valid according to the regex. It's extremely complex to make a correct date validation using regex alone, you're better off just checking for broad conformance and then using a date library for the actual verification. – VLAZ Dec 17 '19 at 07:47
  • 1
    @Stefan yeah, I just noticed the `\d` there. I've removed the comment a second before you posted. – VLAZ Dec 17 '19 at 07:47
  • Can we see the full context of the `param!` call? Is it, perhaps, nested? – Schwern Dec 17 '19 at 07:53
  • 4
    Why do you want to validate the string format of a date? Why not just parse it (you do it anyway) and if the parser doesn't raise an error then accept it as correct and if the parser raises an error then you return a useful error message? – spickermann Dec 17 '19 at 08:02
  • Rubular (https://rubular.com) is a good tool to easily battle-test your regexes – Qortex Dec 17 '19 at 09:34
  • 1
    The string represents a valid date if and only if `DateTime.strptime(input, '%Y-%m-%d')` does not raise an exception. You therefore need to execute that (after `'require 'date'`) within a `begin-rescue-end` clause and catch an exception if thrown. For a regular expression to be used it would have to be complex, in part because it would have to implement all the rules for determining if a year is a leap year (it's not, for example, if the year is evenly divided by 100, but not by 400). Needless to say, a regular expression is not the right tool for the job here. – Cary Swoveland Dec 17 '19 at 17:31
  • 1
    While I suggest using [DateTime::strptime](https://ruby-doc.org/stdlib-2.6.3/libdoc/date/rdoc/DateTime.html#method-c-strptime), I caution use of [Date#parse](https://ruby-doc.org/stdlib-2.6.3/libdoc/date/rdoc/Date.html#method-c-parse), which is notoriously unreliable. For example, if `parse`'s argument (a string) contains the word "may" or "march" (or even a word that begins with "may" or "march", such as "marching" and "maybe"), it will dutifully turn the string into a date. – Cary Swoveland Dec 17 '19 at 17:45

2 Answers2

1

Proper validation of YYYY-MM-DD aka ISO8601 format would be:

require 'date'
input = '2019-12-11'
Date.parse(input).to_s == input

Regular expressions should not be used for this purpose.

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
-1

show resultsuppose you are validate 1900-2019

/^(19[0-9][0-9]|20[0-1][0-9])-(0[0-9]|1[0-2])-(0[0-9]|1[0-9]|2[0-9]|3[0-1])$/
Qing Feng
  • 1
  • 1
  • 4
    I doubt that this helps or even works at all. To convince me otherwise please explain how this works and why it helps. – Yunnosch Dec 17 '19 at 08:24