Module: Sxn::RuntimeValidations
- Defined in:
- lib/sxn/runtime_validations.rb
Overview
Runtime validation helpers for Thor commands and type safety
Class Method Summary collapse
-
.validate_and_coerce_type(value, target_type, context = nil) ⇒ Object
Validate and coerce types for runtime safety.
-
.validate_template_variables(variables) ⇒ Object
Validate template variables for Liquid templates.
-
.validate_thor_arguments(command_name, args, options, validations) ⇒ Object
Validate Thor command arguments at runtime.
Class Method Details
.validate_and_coerce_type(value, target_type, context = nil) ⇒ Object
Validate and coerce types for runtime safety
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/sxn/runtime_validations.rb', line 40 def validate_and_coerce_type(value, target_type, context = nil) case target_type.name when "String" value.to_s when "Integer" Integer(value) when "Float" Float(value) when "TrueClass", "FalseClass", "Boolean" !!value when "Array" Array(value) when "Hash" value.is_a?(Hash) ? value : {} else value end rescue StandardError => e raise TypeError, "Cannot coerce #{value.class} to #{target_type} in #{context}: #{e.message}" end |
.validate_template_variables(variables) ⇒ Object
Validate template variables for Liquid templates
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/sxn/runtime_validations.rb', line 62 def validate_template_variables(variables) return {} unless variables.is_a?(Hash) # Ensure all required variable categories exist validated = { session: variables[:session] || {}, project: variables[:project] || {}, git: variables[:git] || {}, user: variables[:user] || {}, environment: variables[:environment] || {}, timestamp: variables[:timestamp] || {}, custom: variables[:custom] || {} } # Ensure no nil values in the hash validated.each do |key, value| validated[key] = {} unless value.is_a?(Hash) end validated end |
.validate_thor_arguments(command_name, args, options, validations) ⇒ Object
Validate Thor command arguments at runtime
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/sxn/runtime_validations.rb', line 8 def validate_thor_arguments(command_name, args, , validations) # Ensure args and options are not nil args ||= [] ||= {} # Validate argument count if validations[:args] count_range = validations[:args][:count] raise ArgumentError, "#{command_name} expects #{count_range} arguments, got #{args.size}" if count_range && !count_range.include?(args.size) # Validate argument types if validations[:args][:types] args.each_with_index do |arg, index| expected_types = Array(validations[:args][:types][index] || validations[:args][:types].last) unless expected_types.any? { |type| arg.is_a?(type) } raise TypeError, "#{command_name} argument #{index + 1} must be #{expected_types.join(" or ")}" end end end end # Validate options if validations[:options] && .respond_to?(:each) .each do |key, value| validate_option_type(command_name, key, value, validations[:options][key.to_sym]) if validations[:options][key.to_sym] end end true end |