Module: Sinatra::ParamValidator::Helpers
- Defined in:
- lib/sinatra/param_validator/helpers.rb
Overview
Helpers for validating parameters
Instance Method Summary collapse
- #_handle_error(key, error) ⇒ Object
- #_run_block(key, block) ⇒ Object
- #_set_param_to_coerced(key, parameter, transform) ⇒ Object
- #_set_param_to_default(key, default) ⇒ Object
- #_update_params_hash(key, parameter, default, transform) ⇒ Object
- #filter_params ⇒ Object
-
#param(key, type, default: nil, message: nil, transform: nil, **args, &block) ⇒ Object
rubocop:disable Metrics/ParameterLists.
- #rule(name, *args, **kwargs, &block) ⇒ Object
- #validate(klass, identifier) ⇒ Object
Instance Method Details
#_handle_error(key, error) ⇒ Object
48 49 50 51 52 |
# File 'lib/sinatra/param_validator/helpers.rb', line 48 def _handle_error(key, error) raise InvalidParameterError, error unless defined? @_validator_errors @_validator_errors[key] = @_validator_errors.fetch(key, []).concat(Array(error)) end |
#_run_block(key, block) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/sinatra/param_validator/helpers.rb', line 54 def _run_block(key, block) args = block.arity == 1 ? [self] : [] instance_exec(*args, &block) rescue InvalidParameterError => e _handle_error key, e. end |
#_set_param_to_coerced(key, parameter, transform) ⇒ Object
69 70 71 72 73 |
# File 'lib/sinatra/param_validator/helpers.rb', line 69 def _set_param_to_coerced(key, parameter, transform) return if parameter.coerced.nil? params[key] = transform.nil? ? parameter.coerced : transform.to_proc.call(parameter.coerced) end |
#_set_param_to_default(key, default) ⇒ Object
75 76 77 |
# File 'lib/sinatra/param_validator/helpers.rb', line 75 def _set_param_to_default(key, default) params[key] = default.respond_to?(:call) ? default.call : default end |
#_update_params_hash(key, parameter, default, transform) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/sinatra/param_validator/helpers.rb', line 61 def _update_params_hash(key, parameter, default, transform) if !params.key?(key) || params[key].nil? _set_param_to_default key, default unless default.nil? else _set_param_to_coerced key, parameter, transform end end |
#filter_params ⇒ Object
7 8 9 10 11 12 13 14 |
# File 'lib/sinatra/param_validator/helpers.rb', line 7 def filter_params params.each do |(param, value)| params[param] = nil if value == '' params[param] = [] if value == [''] end rescue StandardError => e raise "Filter params failed: #{e}" end |
#param(key, type, default: nil, message: nil, transform: nil, **args, &block) ⇒ Object
rubocop:disable Metrics/ParameterLists
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/sinatra/param_validator/helpers.rb', line 16 def param(key, type, default: nil, message: nil, transform: nil, **args, &block) # rubocop:disable Metrics/ParameterLists parameter = Parameter.new(params[key], type, **args) if parameter.valid? _update_params_hash key, parameter, default, transform _run_block(key, block) if block else _handle_error key, || parameter.errors end params[key] rescue NameError raise 'Invalid parameter type' end |
#rule(name, *args, **kwargs, &block) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/sinatra/param_validator/helpers.rb', line 29 def rule(name, *args, **kwargs, &block) rule = Rule.new(name, params, *args, **kwargs) if rule.passes? _run_block(:rules, block) if block else _handle_error :rules, rule.errors unless rule.passes? end rescue NameError raise 'Invalid rule type' end |
#validate(klass, identifier) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/sinatra/param_validator/helpers.rb', line 40 def validate(klass, identifier) identifier = Identifier.new(identifier) if identifier.is_a? Symbol definition = settings.validator_definitions.get(identifier.identifier) validator = klass.new(&definition) validator.run(self, *identifier.args) validator.handle_failure(self) unless validator.success? end |