Class: Servactory::Inputs::Validations::Required

Inherits:
Base
  • Object
show all
Defined in:
lib/servactory/inputs/validations/required.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context:, input:, value:) ⇒ Required

Returns a new instance of Required.



19
20
21
22
23
24
25
# File 'lib/servactory/inputs/validations/required.rb', line 19

def initialize(context:, input:, value:)
  super()

  @context = context
  @input = input
  @value = value
end

Class Method Details

.check(context:, attribute:, value:, check_key:) ⇒ Object



7
8
9
10
11
# File 'lib/servactory/inputs/validations/required.rb', line 7

def self.check(context:, attribute:, value:, check_key:, **)
  return unless should_be_checked_for?(attribute, check_key)

  new(context:, input: attribute, value:).check
end

.should_be_checked_for?(input, check_key) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/servactory/inputs/validations/required.rb', line 13

def self.should_be_checked_for?(input, check_key)
  check_key == :required && input.required?
end

Instance Method Details

#checkObject



27
28
29
30
31
32
33
# File 'lib/servactory/inputs/validations/required.rb', line 27

def check
  return if Servactory::Utils.value_present?(@value)

  _, message = @input.required.values_at(:is, :message)

  add_error_with(message)
end