Class: EasyTalk::ValidationBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_talk/validation_builder.rb

Overview

The ValidationBuilder creates ActiveModel validations based on JSON Schema constraints

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, property_name, type, constraints) ⇒ ValidationBuilder

Returns a new instance of ValidationBuilder.



28
29
30
31
32
33
# File 'lib/easy_talk/validation_builder.rb', line 28

def initialize(klass, property_name, type, constraints)
  @klass = klass
  @property_name = property_name.to_sym
  @type = type
  @constraints = constraints || {}
end

Instance Attribute Details

#constraintsObject (readonly)

Initialize a new ValidationBuilder

Parameters:

  • klass (Class)

    The model class to apply validations to

  • property_name (Symbol, String)

    The name of the property

  • type (Class, Object)

    The type of the property

  • constraints (Hash)

    The JSON Schema constraints for the property



26
27
28
# File 'lib/easy_talk/validation_builder.rb', line 26

def constraints
  @constraints
end

#klassObject (readonly)

Initialize a new ValidationBuilder

Parameters:

  • klass (Class)

    The model class to apply validations to

  • property_name (Symbol, String)

    The name of the property

  • type (Class, Object)

    The type of the property

  • constraints (Hash)

    The JSON Schema constraints for the property



26
27
28
# File 'lib/easy_talk/validation_builder.rb', line 26

def klass
  @klass
end

#property_nameObject (readonly)

Initialize a new ValidationBuilder

Parameters:

  • klass (Class)

    The model class to apply validations to

  • property_name (Symbol, String)

    The name of the property

  • type (Class, Object)

    The type of the property

  • constraints (Hash)

    The JSON Schema constraints for the property



26
27
28
# File 'lib/easy_talk/validation_builder.rb', line 26

def property_name
  @property_name
end

#typeObject (readonly)

Initialize a new ValidationBuilder

Parameters:

  • klass (Class)

    The model class to apply validations to

  • property_name (Symbol, String)

    The name of the property

  • type (Class, Object)

    The type of the property

  • constraints (Hash)

    The JSON Schema constraints for the property



26
27
28
# File 'lib/easy_talk/validation_builder.rb', line 26

def type
  @type
end

Class Method Details

.build_validations(klass, property_name, type, constraints) ⇒ void

This method returns an undefined value.

Build validations for a property and apply them to the model class

Parameters:

  • klass (Class)

    The model class to apply validations to

  • property_name (Symbol, String)

    The name of the property

  • type (Class, Object)

    The type of the property

  • constraints (Hash)

    The JSON Schema constraints for the property



15
16
17
18
# File 'lib/easy_talk/validation_builder.rb', line 15

def self.build_validations(klass, property_name, type, constraints)
  builder = new(klass, property_name, type, constraints)
  builder.apply_validations
end

Instance Method Details

#apply_validationsObject

Apply validations based on property type and constraints



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/easy_talk/validation_builder.rb', line 36

def apply_validations
  # Determine if the type is boolean
  type_class = get_type_class(@type)
  is_boolean = type_class == [TrueClass, FalseClass] ||
               type_class == TrueClass ||
               type_class == FalseClass ||
               @type.to_s.include?('T::Boolean')

  # Skip presence validation for booleans and nilable types
  apply_presence_validation unless optional? || is_boolean || nilable_type?
  if nilable_type?
    # For nilable types, get the inner type and apply validations to it
    inner_type = extract_inner_type(@type)
    apply_type_validations(inner_type)
  else
    apply_type_validations(@type)
  end

  # Common validations for most types
  apply_enum_validation if @constraints[:enum]
  apply_const_validation if @constraints[:const]
end