Sha256: 7a6db4f1421472199919adef98bbcfa4d56508fcccc5e6f0cf05c3dcb2d6ab71

Contents?: true

Size: 777 Bytes

Versions: 1

Compression:

Stored size: 777 Bytes

Contents

module WeakParameters
  class BaseValidator
    attr_reader :params, :key, :options

    def initialize(params, key, options = {})
      @params = params
      @key = key
      @options = options
    end

    def validate
      raise_error if required? && nil? || exist? && invalid_type?
    end

    def required?
      !!options[:required]
    end

    def type
      :string
    end

    private

    def nil?
      value.nil?
    end

    def exist?
      !nil?
    end

    def value
      params[key]
    end

    def raise_error
      raise WeakParameters::ValidationError, error_message
    end

    def error_message
      "params[#{key.inspect}] is required"
    end

    def valid_type?
      true
    end

    def invalid_type?
      !valid_type?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
weak_parameters-0.0.2 lib/weak_parameters/base_validator.rb