Class: EasyTalk::ValidationBuilder
- Inherits:
-
Object
- Object
- EasyTalk::ValidationBuilder
- Defined in:
- lib/easy_talk/validation_builder.rb
Overview
The ValidationBuilder creates ActiveModel validations based on JSON Schema constraints
Instance Attribute Summary collapse
-
#constraints ⇒ Object
readonly
Initialize a new ValidationBuilder.
-
#klass ⇒ Object
readonly
Initialize a new ValidationBuilder.
-
#property_name ⇒ Object
readonly
Initialize a new ValidationBuilder.
-
#type ⇒ Object
readonly
Initialize a new ValidationBuilder.
Class Method Summary collapse
-
.build_validations(klass, property_name, type, constraints) ⇒ void
Build validations for a property and apply them to the model class.
Instance Method Summary collapse
-
#apply_validations ⇒ Object
Apply validations based on property type and constraints.
-
#initialize(klass, property_name, type, constraints) ⇒ ValidationBuilder
constructor
A new instance of ValidationBuilder.
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
#constraints ⇒ Object (readonly)
Initialize a new ValidationBuilder
26 27 28 |
# File 'lib/easy_talk/validation_builder.rb', line 26 def constraints @constraints end |
#klass ⇒ Object (readonly)
Initialize a new ValidationBuilder
26 27 28 |
# File 'lib/easy_talk/validation_builder.rb', line 26 def klass @klass end |
#property_name ⇒ Object (readonly)
Initialize a new ValidationBuilder
26 27 28 |
# File 'lib/easy_talk/validation_builder.rb', line 26 def property_name @property_name end |
#type ⇒ Object (readonly)
Initialize a new ValidationBuilder
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
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_validations ⇒ Object
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 |