Sha256: 3954478e1dfc9e93305281067414283dcc91dce228e9e217bd3e80216dad77d9

Contents?: true

Size: 1.45 KB

Versions: 13

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

require 'luna_park/errors'

begin
  require 'dry-validation'
rescue LoadError => e
  raise unless e.message == 'cannot load such file -- dry-validation'
end

module LunaPark
  module Validators
    class Dry
      def initialize(params)
        @params = params
      end

      def success?
        result.success?
      end

      def valid_params
        (success? && result.to_h) || {}
      end

      def errors_array
        result.errors.map { |error| { path: error.path, text: error.text, input: error.input } }
      end

      def errors_tree
        result.errors.to_h || {}
      end

      alias errors errors_tree

      private

      attr_reader :params

      def result
        @result ||= self.class.schema.call(params)
      end

      class << self
        def schema
          @_schema
        end

        alias validate new

        def validation_schema(&block)
          unless defined?(::Dry::Validation)
            raise NameError, "uninitialized constant ::Dry::Validation\n" \
                             'Perhaps you forgot to add gem "dry-validation"'
          end

          unless defined?(::Dry::Validation::Contract)
            raise NameError, "uninitialized constant ::Dry::Validation::Contract\n" \
                             'which appears in version 1.0 of gem "dry-validation"'
          end

          @_schema = Class.new(::Dry::Validation::Contract, &block).new
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
luna_park-0.13.2 lib/luna_park/validators/dry.rb
luna_park-0.13.1 lib/luna_park/validators/dry.rb
luna_park-0.13.0 lib/luna_park/validators/dry.rb
luna_park-0.12.1 lib/luna_park/validators/dry.rb
luna_park-0.12.0 lib/luna_park/validators/dry.rb
luna_park-0.11.7 lib/luna_park/validators/dry.rb
luna_park-0.11.6 lib/luna_park/validators/dry.rb
luna_park-0.11.5 lib/luna_park/validators/dry.rb
luna_park-0.11.4 lib/luna_park/validators/dry.rb
luna_park-0.11.3 lib/luna_park/validators/dry.rb
luna_park-0.11.2 lib/luna_park/validators/dry.rb
luna_park-0.11.1 lib/luna_park/validators/dry.rb
luna_park-0.11.0 lib/luna_park/validators/dry.rb