Module: BetterController::ParameterValidation
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/better_controller/parameter_validation.rb
Overview
Module providing parameter validation helpers for controllers
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#validate_param_schema(schema) ⇒ Object
Validate parameters against a schema.
-
#validate_required_params(*params) ⇒ Object
Validate that required parameters are present.
Instance Method Details
#validate_param_schema(schema) ⇒ Object
Validate parameters against a schema
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/better_controller/parameter_validation.rb', line 52 def validate_param_schema(schema) errors = [] schema.each do |param, rules| value = params[param] if rules[:required] && value.nil? errors << "#{param} is required" next end next if value.nil? errors << "#{param} must be a #{rules[:type]}" if rules[:type] && !value.is_a?(rules[:type]) errors << "#{param} must be one of: #{rules[:in].join(', ')}" if rules[:in]&.exclude?(value) errors << "#{param} has invalid format" if rules[:format] && rules[:format] !~ value.to_s end raise BetterController::Error, errors.join(', ') unless errors.empty? true end |
#validate_required_params(*params) ⇒ Object
Validate that required parameters are present
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/better_controller/parameter_validation.rb', line 34 def validate_required_params(*params) missing_params = [] params.each do |param| missing_params << param unless parameter_present?(param) end unless missing_params.empty? = "Missing required parameters: #{missing_params.join(', ')}" raise BetterController::Error, end true end |