Class: Sorta::Http::Web::ParamsValidator
- Inherits:
-
Object
- Object
- Sorta::Http::Web::ParamsValidator
- Defined in:
- lib/sorta/http/web/params_validator.rb
Defined Under Namespace
Classes: ValidationError
Class Method Summary collapse
Instance Method Summary collapse
- #call(params) ⇒ Object
-
#initialize ⇒ ParamsValidator
constructor
A new instance of ParamsValidator.
- #param(name, type:) ⇒ Object
Constructor Details
#initialize ⇒ ParamsValidator
15 16 17 |
# File 'lib/sorta/http/web/params_validator.rb', line 15 def initialize @rules = {} end |
Class Method Details
.build(&block) ⇒ Object
9 10 11 12 13 |
# File 'lib/sorta/http/web/params_validator.rb', line 9 def self.build(&block) obj = new obj.instance_exec(&block) obj end |
Instance Method Details
#call(params) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/sorta/http/web/params_validator.rb', line 23 def call(params) errors = [] result = @rules.each_with_object({}) do |(key, klass), acc| acc[key] = Kernel.send(klass.name, params[key.to_s]) rescue => e errors << "#{e.message}" end raise ValidationError.new(errors.join("\n")) if errors.any? result end |
#param(name, type:) ⇒ Object
19 20 21 |
# File 'lib/sorta/http/web/params_validator.rb', line 19 def param(name, type:) @rules[name] = type end |