floraison / fugit

time tools (cron, parsing, durations, ...) for Ruby, rufus-scheduler, and flor
MIT License
355 stars 29 forks source link

[feature request] cron syntax validation #23

Closed darwin67 closed 5 years ago

darwin67 commented 5 years ago

Issue description

It would be great to have a method like .validate that can validate and check if the syntax of the cron string is correct.

How to reproduce

N/A

Expected behaviour

N/A

Context

I want to store the cron string into the database and use it later for checking purposes. So it would be great if I can do some validation with the string before saving it.

Additional context

N/A

jmettraux commented 5 years ago

Hello,

you can easily achieve that with:


require 'fugit'

class Fugit::Cron
  def self.validate(s)
    !! parse(s)
  end
end

def Fugit.validate_cron(s)
  !! parse_cron(s)
end

p Fugit::Cron.validate('5 * * * * *') # => true
p Fugit::Cron.validate('nada') # => false

p Fugit.validate_cron('5 * * * * *') # => true
p Fugit.validate_cron('nada') # => false

If you have any question or suggestion, they're welcome.

Best regards.

darwin67 commented 5 years ago

Thanks!