Class: Rbdantic::Validators::Types::Base

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

Overview

Base class for type validators Provides common validation pattern with type checking and coercion

Direct Known Subclasses

Array, Boolean, Date, DateTime, Hash, Model, Number, String, Symbol, Time

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#constraintsObject (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

Returns:

  • (Object, nil)

    coerced value or nil if cannot coerce



45
46
47
# File 'lib/rbdantic/validators/types/base.rb', line 45

def coerce(_value)
  nil
end

#expected_type_nameString

Expected type name for error messages

Returns:

  • (String)

    human-readable type name

Raises:

  • (NotImplementedError)


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

Returns:

  • (Boolean)

    true if value matches expected type

Raises:

  • (NotImplementedError)


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

Returns:



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

Parameters:

  • value

    the validated value

  • errors (Array)

    error accumulator

  • location (Array)

    current location path

  • strict (Boolean) (defaults to: false)

    strict mode flag

Returns:

  • (Object)

    the (potentially transformed) value



55
56
57
# File 'lib/rbdantic/validators/types/base.rb', line 55

def validate_constraints(value, _errors, _location, strict: false)
  value
end