Class: Rbdantic::Validators::FieldValidator
- Inherits:
-
Object
- Object
- Rbdantic::Validators::FieldValidator
- 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
-
#field_name ⇒ Object
readonly
Returns the value of attribute field_name.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#validator_proc ⇒ Object
readonly
Returns the value of attribute validator_proc.
Instance Method Summary collapse
-
#apply(value, context, handler = nil, &handler_block) ⇒ Array
Execute the validator and optionally transform the value.
-
#call(value, context) ⇒ Array<ErrorDetail>
Execute the validator.
-
#initialize(field_name, mode: :after, &block) ⇒ FieldValidator
constructor
A new instance of FieldValidator.
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_name ⇒ Object (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 |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
13 14 15 |
# File 'lib/rbdantic/validators/field_validator.rb', line 13 def mode @mode end |
#validator_proc ⇒ Object (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
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., input: value ) end [errors, transformed_value] end |
#call(value, context) ⇒ Array<ErrorDetail>
Execute the validator
25 26 27 28 |
# File 'lib/rbdantic/validators/field_validator.rb', line 25 def call(value, context) errors, = apply(value, context) errors end |