Sha256: 2111ea9592a751219a97641692d622c3d7dd3b50820887a206eca48c843c7ca8

Contents?: true

Size: 1.48 KB

Versions: 21

Compression:

Stored size: 1.48 KB

Contents

# typed: strict
# frozen_string_literal: true

module DearInventory
  class Validator
    extend T::Sig

    extend T::Helpers
    abstract!

    DEFAULT_OPTIONS = T.let(
      {
        limit: nil,
        max_length: nil,
        values: nil,
      }.freeze,
      T::Hash[Symbol, T.nilable(T.any(Integer, T::Array[String]))]
    )
    private_constant :DEFAULT_OPTIONS

    sig do
      params(
        field_name: Symbol,
        value: T.nilable(T.any(Date, Numeric, String, T::Boolean, Time)),
        options: T::Hash[Symbol, T.untyped]
      ).void
    end
    def self.call(field_name, value, options: DEFAULT_OPTIONS)
      new(field_name, value, options: options).call
    end

    sig do
      params(
        field_name: Symbol,
        value: T.nilable(T.any(Date, Numeric, String, T::Boolean, Time)),
        options: T::Hash[Symbol, T.untyped]
      ).void
    end
    def initialize(field_name, value, options: DEFAULT_OPTIONS)
      @field_name = T.let(field_name, Symbol)
      @value =
        T.let(value, T.nilable(T.any(Date, Numeric, String, T::Boolean, Time)))

      @limit = T.let(options[:limit], T.nilable(Integer))
      @max_length = T.let(options[:limit], T.nilable(Integer))
      @values = T.let(options[:values], T.nilable(T::Array[String]))
    end

    private

    sig { abstract.void }
    def call; end

    protected

    sig { params(message: String).void }
    def raise_error(message)
      raise ValidationError, "#{@field_name} is invalid, #{message}"
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
dear_inventory-1.4.0 lib/dear_inventory/validator.rb
dear_inventory-1.3.0 lib/dear_inventory/validator.rb
dear_inventory-1.2.0 lib/dear_inventory/validator.rb
dear_inventory-1.1.1 lib/dear_inventory/validator.rb
dear_inventory-1.1.0 lib/dear_inventory/validator.rb
dear_inventory-1.0.0 lib/dear_inventory/validator.rb
dear_inventory-0.7.5 lib/dear_inventory/validator.rb
dear_inventory-0.7.4 lib/dear_inventory/validator.rb
dear_inventory-0.7.3 lib/dear_inventory/validator.rb
dear_inventory-0.7.2 lib/dear_inventory/validator.rb
dear_inventory-0.7.1 lib/dear_inventory/validator.rb
dear_inventory-0.7.0 lib/dear_inventory/validator.rb
dear_inventory-0.6.3 lib/dear_inventory/validator.rb
dear_inventory-0.6.2 lib/dear_inventory/validator.rb
dear_inventory-0.6.1 lib/dear_inventory/validator.rb
dear_inventory-0.6.0 lib/dear_inventory/validator.rb
dear_inventory-0.5.0 lib/dear_inventory/validator.rb
dear_inventory-0.4.1 lib/dear_inventory/validator.rb
dear_inventory-0.4.0 lib/dear_inventory/validator.rb
dear_inventory-0.3.0 lib/dear_inventory/validator.rb