Module: Rbdantic::Validators::Types

Defined in:
lib/rbdantic/validators/types.rb,
lib/rbdantic/validators/types/base.rb,
lib/rbdantic/validators/types/date.rb,
lib/rbdantic/validators/types/hash.rb,
lib/rbdantic/validators/types/time.rb,
lib/rbdantic/validators/types/array.rb,
lib/rbdantic/validators/types/float.rb,
lib/rbdantic/validators/types/model.rb,
lib/rbdantic/validators/types/number.rb,
lib/rbdantic/validators/types/string.rb,
lib/rbdantic/validators/types/symbol.rb,
lib/rbdantic/validators/types/boolean.rb,
lib/rbdantic/validators/types/integer.rb,
lib/rbdantic/validators/types/datetime.rb

Defined Under Namespace

Classes: Array, Base, Boolean, Date, DateTime, Float, Hash, Integer, Model, Number, String, Symbol, Time

Constant Summary collapse

VALIDATOR_CLASSES =
{
  ::String => Validators::Types::String,
  ::Integer => Validators::Types::Integer,
  ::Float => Validators::Types::Float,
  Rbdantic::Boolean => Validators::Types::Boolean,
  ::Array => Validators::Types::Array,
  ::Hash => Validators::Types::Hash,
  ::Symbol => Validators::Types::Symbol,
  ::Time => Validators::Types::Time,
  ::Date => Validators::Types::Date,
  ::DateTime => Validators::Types::DateTime
}.freeze

Class Method Summary collapse

Class Method Details

.create_validator(type, **constraints) ⇒ Base?

Create validator for type with constraints

Parameters:

  • type (Class)

    the field type

  • constraints (Hash)

    constraint options (min_length, gt, etc.)

Returns:

  • (Base, nil)

    validator instance

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rbdantic/validators/types.rb', line 44

def self.create_validator(type, **constraints)
  return nil if type.nil?

  if nested_model?(type)
    return Validators::Types::Model.new(type, **constraints)
  end

  validator_class = VALIDATOR_CLASSES[type]
  raise ArgumentError, "Unsupported field type: #{type}" unless validator_class

  validator_class.new(**constraints)
end

.has_validator?(type) ⇒ Boolean

Returns:



57
58
59
# File 'lib/rbdantic/validators/types.rb', line 57

def self.has_validator?(type)
  VALIDATOR_CLASSES.key?(type)
end

.nested_model?(type) ⇒ Boolean

Check if type is a nested model

Returns:



62
63
64
# File 'lib/rbdantic/validators/types.rb', line 62

def self.nested_model?(type)
  type.is_a?(Class) && type < Rbdantic::BaseModel && !type.equal?(Rbdantic::BaseModel)
end

.validator_class_for(type) ⇒ Object



36
37
38
# File 'lib/rbdantic/validators/types.rb', line 36

def self.validator_class_for(type)
  VALIDATOR_CLASSES[type]
end