Sha256: 631c860a12e6a159144d94f4bccda8852a2aaa11e6dd3dcd91503029d2fe2d9a

Contents?: true

Size: 1.64 KB

Versions: 55

Compression:

Stored size: 1.64 KB

Contents

module Appfuel
  module Validation
    # Any validator that is run for an action or command will be created with
    # this class. It service to abstract away Dry::Validation, the library we
    # use from the system that runs all the validators. This allows us to use
    # validators along side things like validator pipes without the system
    # having to care.
    class Validator
      attr_reader :name, :schema

      # @param name [String] used to register this validator in a container
      # @param schema [Dry::Validation::Schema] the actual validator
      # @param fail_fast [Bool] tell the system how to fail
      # @return [Validator]
      def initialize(name, schema, fail_fast: false)
        @name = name
        unless schema.respond_to?(:call)
          fail ArgumentError, "schema must implement :call"
        end
        @schema = schema

        fail_fast == true ? enable_fail_fast : disable_fail_fast
      end

      # Ensures the system will stop validating when this validator fails
      #
      # @return [Bool]
      def enable_fail_fast
        @fail_fast = true
      end

      # Ensures the system will continue validating when errors exist
      #
      # @return [Bool]
      def disable_fail_fast
        @fail_fast = false
      end

      # @return [Bool]
      def fail_fast?
        @fail_fast
      end


      # Delegate's to the Dry::Validation schema to validate the inputs
      #
      # @param inputs [Hash]
      # @return [Dry::Validation::Result]
      def call(inputs)
        schema.call(inputs)
      end

      # Tell this system this is not a validation pipe
      def pipe?
        false
      end
    end
  end
end

Version data entries

55 entries across 55 versions & 1 rubygems

Version Path
appfuel-0.7.0 lib/appfuel/validation/validator.rb
appfuel-0.6.16 lib/appfuel/validation/validator.rb
appfuel-0.6.15 lib/appfuel/validation/validator.rb
appfuel-0.6.14 lib/appfuel/validation/validator.rb
appfuel-0.6.13 lib/appfuel/validation/validator.rb
appfuel-0.6.12 lib/appfuel/validation/validator.rb
appfuel-0.6.11 lib/appfuel/validation/validator.rb
appfuel-0.6.10 lib/appfuel/validation/validator.rb
appfuel-0.6.9 lib/appfuel/validation/validator.rb
appfuel-0.6.8 lib/appfuel/validation/validator.rb
appfuel-0.6.7 lib/appfuel/validation/validator.rb
appfuel-0.6.6 lib/appfuel/validation/validator.rb
appfuel-0.6.5 lib/appfuel/validation/validator.rb
appfuel-0.6.4 lib/appfuel/validation/validator.rb
appfuel-0.6.3 lib/appfuel/validation/validator.rb
appfuel-0.6.1 lib/appfuel/validation/validator.rb
appfuel-0.5.16 lib/appfuel/validation/validator.rb
appfuel-0.5.15 lib/appfuel/validation/validator.rb
appfuel-0.5.14 lib/appfuel/validation/validator.rb
appfuel-0.5.13 lib/appfuel/validation/validator.rb