Class: Rbdantic::Validators::Types::Base
- Inherits:
-
Object
- Object
- Rbdantic::Validators::Types::Base
- Defined in:
- lib/rbdantic/validators/types/base.rb
Overview
Base class for type validators Provides common validation pattern with type checking and coercion
Instance Attribute Summary collapse
-
#constraints ⇒ Object
readonly
Returns the value of attribute constraints.
Instance Method Summary collapse
-
#coerce(_value) ⇒ Object?
Override in subclasses to define coercion logic.
-
#expected_type_name ⇒ String
Expected type name for error messages.
-
#initialize(**constraints) ⇒ Base
constructor
A new instance of Base.
-
#matches_type?(value) ⇒ Boolean
Override in subclasses to define type matching.
-
#validate(value, location = [], strict: false) ⇒ Array<ErrorDetail>, value
Main validation entry point.
-
#validate_constraints(value, _errors, _location, strict: false) ⇒ Object
Override in subclasses to add constraint validation.
Constructor Details
#initialize(**constraints) ⇒ Base
Returns a new instance of Base.
11 12 13 |
# File 'lib/rbdantic/validators/types/base.rb', line 11 def initialize(**constraints) @constraints = constraints end |
Instance Attribute Details
#constraints ⇒ Object (readonly)
Returns the value of attribute constraints.
9 10 11 |
# File 'lib/rbdantic/validators/types/base.rb', line 9 def constraints @constraints end |
Instance Method Details
#coerce(_value) ⇒ Object?
Override in subclasses to define coercion logic
45 46 47 |
# File 'lib/rbdantic/validators/types/base.rb', line 45 def coerce(_value) nil end |
#expected_type_name ⇒ String
Expected type name for error messages
61 62 63 |
# File 'lib/rbdantic/validators/types/base.rb', line 61 def expected_type_name raise NotImplementedError, "Type validators must implement #expected_type_name" end |
#matches_type?(value) ⇒ Boolean
Override in subclasses to define type matching
39 40 41 |
# File 'lib/rbdantic/validators/types/base.rb', line 39 def matches_type?(value) raise NotImplementedError, "Type validators must implement #matches_type?" end |
#validate(value, location = [], strict: false) ⇒ Array<ErrorDetail>, value
Main validation entry point
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rbdantic/validators/types/base.rb', line 17 def validate(value, location = [], strict: false) errors = [] # Type check with optional coercion unless matches_type?(value) if !strict && !(coerced = coerce(value)).nil? value = coerced else errors << type_error(value, location) return [errors, value] end end # Constraint validation (override in subclasses) # Returns potentially transformed value (e.g., for nested type coercion) value = validate_constraints(value, errors, location, strict: strict) [errors, value] end |
#validate_constraints(value, _errors, _location, strict: false) ⇒ Object
Override in subclasses to add constraint validation
55 56 57 |
# File 'lib/rbdantic/validators/types/base.rb', line 55 def validate_constraints(value, _errors, _location, strict: false) value end |