Class: Rbdantic::Validators::FieldValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/rbdantic/validators/field_validator.rb

Overview

Decorator for field-level validators Usage:

field_validator :age, mode: :after do |value|
raise "must be at least 18" if value < 18
end

Constant Summary collapse

MODES =
%i[before after plain wrap].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field_name, mode: :after, &block) ⇒ FieldValidator

Returns a new instance of FieldValidator.



15
16
17
18
19
# File 'lib/rbdantic/validators/field_validator.rb', line 15

def initialize(field_name, mode: :after, &block)
  @field_name = field_name
  @mode = validate_mode!(mode)
  @validator_proc = block
end

Instance Attribute Details

#field_nameObject (readonly)

Returns the value of attribute field_name.



13
14
15
# File 'lib/rbdantic/validators/field_validator.rb', line 13

def field_name
  @field_name
end

#modeObject (readonly)

Returns the value of attribute mode.



13
14
15
# File 'lib/rbdantic/validators/field_validator.rb', line 13

def mode
  @mode
end

#validator_procObject (readonly)

Returns the value of attribute validator_proc.



13
14
15
# File 'lib/rbdantic/validators/field_validator.rb', line 13

def validator_proc
  @validator_proc
end

Instance Method Details

#apply(value, context, handler = nil, &handler_block) ⇒ Array

Execute the validator and optionally transform the value

Parameters:

  • the field value

  • validation context

  • (defaults to: nil)

    callable for inner validation (wrap mode only)

Returns:

  • tuple of [errors, transformed_value]



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
64
65
66
67
68
69
70
71
# File 'lib/rbdantic/validators/field_validator.rb', line 35

def apply(value, context, handler = nil, &handler_block)
  errors = []
  transformed_value = value

  begin
    handler ||= handler_block

    # For wrap mode, pass handler if available (Issue 4 fix)
    if @mode == :wrap && handler
      result = @validator_proc.call(value, context, handler)
    else
      result = @validator_proc.call(value, context)
    end

    # Returning false signals validation failure.
    # Any other non-nil/non-true return value is treated as a transformed value.
    if result == false
      errors << ErrorDetail.new(
        type: :validation_failed,
        loc: [@field_name],
        msg: "Custom validation failed",
        input: value
      )
    elsif !result.nil? && result != true
      transformed_value = result
    end
  rescue StandardError => e
    errors << ErrorDetail.new(
      type: :validation_failed,
      loc: [@field_name],
      msg: e.message,
      input: value
    )
  end

  [errors, transformed_value]
end

#call(value, context) ⇒ Array<ErrorDetail>

Execute the validator

Parameters:

  • the field value

  • validation context

Returns:

  • errors from validation



25
26
27
28
# File 'lib/rbdantic/validators/field_validator.rb', line 25

def call(value, context)
  errors, = apply(value, context)
  errors
end