Sha256: 52d4afcf156ba9ad3f0156de1d516287c9e6a3174f8bf67b1f8173dc43b52c47

Contents?: true

Size: 868 Bytes

Versions: 8

Compression:

Stored size: 868 Bytes

Contents

require 'active_support/core_ext/array/conversions'

module Hermod
  module Validators
    class Base
      # Public: Runs the test for the validator returning true if it passes and
      # raising if it fails
      #
      # Raises a Hermod::InvalidInputError if the test fails
      # Returns true if it succeeds
      def valid?(value, attributes)
        !!test(value, attributes) || raise(InvalidInputError, message(value, attributes))
      end

      private

      # Private: override in subclasses to implement the logic for that
      # validator
      #
      # Returns a boolean
      def test(value, attributes)
        raise NotImplementedError
      end

      # Private: override in subclasses to provide a more useful error message
      #
      # Returns a string
      def message(value, attributes)
        "is invalid"
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
hermod-3.0.0 lib/hermod/validators/base.rb
hermod-2.7.0 lib/hermod/validators/base.rb
hermod-2.7.0.pre.rc.1 lib/hermod/validators/base.rb
hermod-2.6.2 lib/hermod/validators/base.rb
hermod-2.6.1 lib/hermod/validators/base.rb
hermod-2.5.3 lib/hermod/validators/base.rb
hermod-2.5.2 lib/hermod/validators/base.rb
hermod-2.5.1 lib/hermod/validators/base.rb