Module: Roda::Endpoints::Endpoint::Validations
- Includes:
- Functions::Shortcut
- Included in:
- Roda::Endpoints::Endpoint
- Defined in:
- lib/roda/endpoints/endpoint/validations.rb
Overview
Parameters validations for Roda::Endpoints based on ‘Dry::Validation` gem.
Instance Method Summary collapse
- #defaults(verb = :any, **attributes) ⇒ Object
- #prepare_validations! ⇒ Object
- #provide_default_validation! ⇒ Object
- #validate(verb = :any, &block) ⇒ Object
- #validation(verb) ⇒ Dry::Validation::Schema::Form
Methods included from Functions::Shortcut
Instance Method Details
#defaults(verb = :any, **attributes) ⇒ Object
15 16 17 18 |
# File 'lib/roda/endpoints/endpoint/validations.rb', line 15 def defaults(verb = :any, **attributes) key = "validations.defaults.#{ns}.#{verb}" if key.is_a?(Symbol) container.register key, attributes end |
#prepare_validations! ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/roda/endpoints/endpoint/validations.rb', line 73 def prepare_validations! return if @validations_prepared (verbs - %i(get head options)).each do |verb| key = "validations.#{ns}.#{verb}" default = "validations.#{ns}.any" unless container.key?(key) || container.key?(default) provide_default_validation! end end @validations_prepared = true end |
#provide_default_validation! ⇒ Object
69 70 71 |
# File 'lib/roda/endpoints/endpoint/validations.rb', line 69 def provide_default_validation! validate {} unless container.key?("validations.#{ns}.any") end |
#validate(verb = :any, &block) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/roda/endpoints/endpoint/validations.rb', line 38 def validate(verb = :any, &block) defaults = "validations.defaults.#{ns}.#{verb}" verb = "validations.#{ns}.#{verb}" schema = Dry::Validation.Form(&block) container.register(verb) do |params| if container.key?(defaults) params = f(:deep_merge).call(params, container[defaults]) end validation = schema.call(params) if validation.success? Right(validation.output) else Left([:unprocessable_entity, {}, validation]) end end schema end |
#validation(verb) ⇒ Dry::Validation::Schema::Form
60 61 62 63 64 65 66 67 |
# File 'lib/roda/endpoints/endpoint/validations.rb', line 60 def validation(verb) if (validation = validation_for(verb)) container[validation] else # default validation requires no params and provide no results provide_default_validation! end end |