class Errapi::Validations::Format

Constants

OPTIONS

Public Class Methods

new(options = {}) click to toggle source
# File lib/errapi/validations/format.rb, line 7
def initialize options = {}
  unless key = exactly_one_option?(OPTIONS, options)
    raise ArgumentError, "Either :with or :without must be supplied (but not both)."
  end

  @format = options[key]
  @should_match = key == :with

  unless @format.kind_of?(Regexp) or callable_option_value?(@format)
    raise callable_option_type_error ":with (or :without)", "a regular expression", @format
  end
end

Public Instance Methods

validate(value, context, options = {}) click to toggle source
# File lib/errapi/validations/format.rb, line 20
def validate value, context, options = {}

  regexp = actual_option_value @format, options
  unless regexp.kind_of? Regexp
    raise callable_option_value_error ":with (or :without)", "a regular expression", regexp
  end

  if !regexp.match(value.to_s) == @should_match
    context.add_error reason: :invalid_format, check_value: regexp
  end
end