Class: CronValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/cron_validator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.compliant?(value) ⇒ Boolean


3
4
5
6
7
8
# File 'app/validators/cron_validator.rb', line 3

def self.compliant?(value)
  cron = ::Fugit::Cron.parse(value)
  !cron.nil?
rescue StandardError => e
  p e
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object


10
11
12
13
14
# File 'app/validators/cron_validator.rb', line 10

def validate_each(record, attribute, value)
  unless value.present? && self.class.compliant?(value)
    record.errors.add(attribute, "is not a valid cron. Check your cron schedule expression here: https://crontab.guru")
  end
end