Sha256: 4c49bf08a1ef0ce651a89d4fb83d40a61c848f4e8c15bfd7a23cbcde4a6a901a

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

module Servicer
  module Layers
    # Layer validating provided params and raising ::Servicer::ParamsError if validation was unsuccessful.
    # It uses `dry-validation` gem schema to build validation options. Please note that you need to add
    # `dry-validations` to your Gemfile, as Servicer is not requiring it automatically.
    # Please consult http://dry-rb.org/gems/dry-validation/ for details of how to build it.
    # Example:
    #   layer :validate_params do
    #     optional(:limit).maybe(:int?, gt?: 0, lt?: 1_000)
    #     optional(:offset).maybe(:int?)
    #   end
    class ValidateParams < ::Servicer::Layers::Base
      def initialize(options, &block)
        super
        @schema = ::Dry::Validation.Schema do
          configure do
            config.input_processor = :sanitizer
          end

          instance_eval(&block)
        end
      end

      def call(current_user, params)
        validation = @schema.call(params)
        raise ParamsError, validation.errors unless validation.success?

        [current_user, validation.output]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
servicer-1.0.0 lib/servicer/layers/validate_params.rb