Class: AWSCronParser::Validators::QuartzCronValidator

Inherits:
Base
  • Object
show all
Defined in:
lib/aws_cron_parser/validators/quartz_cron_validator.rb

Instance Attribute Summary

Attributes inherited from Base

#source

Instance Method Summary collapse

Methods inherited from Base

#initialize, validate

Methods included from Helpers

included

Constructor Details

This class inherits a constructor from AWSCronParser::Validators::Base

Instance Method Details

#validateObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/aws_cron_parser/validators/quartz_cron_validator.rb', line 6

def validate
  parts = @source.split

  # Validate field count (5 or 6 fields)
  unless (5..6).cover?(parts.length)
    raise ArgumentError, "Invalid CRON format: expected 5 or 6 fields, got #{parts.length}"
  end

  # Define field ranges and names
  field_ranges = if parts.length == 6
                   [{ min: 0, max: 59, name: 'second' },
                    { min: 0, max: 59, name: 'minute' },
                    { min: 0, max: 23, name: 'hour' },
                    { min: 1, max: 31, name: 'day-of-month' },
                    { min: 1, max: 12, name: 'month' },
                    { min: 0, max: 7, name: 'day-of-week' }]
                 else
                   [{ min: 0, max: 59, name: 'minute' },
                    { min: 0, max: 23, name: 'hour' },
                    { min: 1, max: 31, name: 'day-of-month' },
                    { min: 1, max: 12, name: 'month' },
                    { min: 0, max: 7, name: 'day-of-week' }]
                 end

  # Check for AWS-specific characters and validate ranges
  aws_chars = %w[L W # ?]
  parts.each_with_index do |field, index|
    range_info = field_ranges[index]
    field_name = range_info[:name]

    # Normalize month and weekday fields before validation
    normalized_field = if field_name == 'month'
                         normalize_month_field(field)
                       elsif field_name == 'day-of-week'
                         normalize_weekday_field(field, is_aws: false)
                       else
                         field
                       end

    # Check for AWS-specific characters
    aws_chars.each do |char|
      next unless field.include?(char)

      msg = "#{field_name} field cannot contain '#{char}'. " \
            'Use cron() wrapper for AWS expressions.'
      raise ArgumentError, msg
    end

    # Validate numeric values in ranges (simple check for obvious errors)
    validate_field_values(normalized_field, range_info)
  end

  # Use FugitAdapter for comprehensive validation
  AWSCronParser::FugitAdapter.new(@source)
  true
rescue ArgumentError => e
  raise ArgumentError, e.message
end