Module: CMDx::Validators::Format

Extended by:
Format
Included in:
Format
Defined in:
lib/cmdx/validators/format.rb

Overview

Validates that a value matches a ‘:with` regex and/or does not match a `:without` regex. Both may be combined; at least one is required.

Instance Method Summary collapse

Instance Method Details

#call(value, options = EMPTY_HASH) ⇒ Validators::Failure?

Parameters:

  • value (String, nil)
  • options (Hash{Symbol => Object}) (defaults to: EMPTY_HASH)

Options Hash (options):

  • :with (Regexp)

    must match

  • :without (Regexp)

    must not match

  • :message (String)

    override for the failure message

Returns:

Raises:

  • (ArgumentError)

    when neither ‘:with` nor `:without` is given



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cmdx/validators/format.rb', line 18

def call(value, options = EMPTY_HASH)
  match =
    case options
    in with:, without:
      value&.match?(with) && !value&.match?(without)
    in with:
      value&.match?(with)
    in without:
      !value&.match?(without)
    else
      raise ArgumentError, "format validator requires :with and/or :without option"
    end

  return if match

  Failure.new(options[:message] || I18nProxy.t("cmdx.validators.format"))
end