Sha256: 18af926d306fe9c514bf4174781fed00d38e74f139d7733a1cb03687b7a71caa

Contents?: true

Size: 1.11 KB

Versions: 7

Compression:

Stored size: 1.11 KB

Contents

# typed: strict
# frozen_string_literal: true

require "time"

module LunchMoney
  # module containing reusable methods for validating data objects
  module Validators
    include Kernel

    sig { params(value: String, valid_values: T::Array[String]).returns(String) }
    def validate_one_of!(value, valid_values)
      return value unless LunchMoney.validate_object_attributes?

      if valid_values.exclude?(value)
        raise(InvalidObjectAttribute, "#{value} is invalid, must be one of #{valid_values.join(", ")}")
      end

      value
    end

    sig { params(value: String).returns(String) }
    def validate_iso8601!(value)
      return value unless LunchMoney.validate_object_attributes?

      raise(InvalidObjectAttribute, "#{value} is not a valid ISO 8601 string") unless valid_iso8601_string?(value)

      value
    end

    private

    sig { params(time_string: String).returns(T::Boolean) }
    def valid_iso8601_string?(time_string)
      Time.iso8601(time_string)
      true
    rescue ArgumentError => error
      raise unless error.message.match?("invalid xmlschema format")

      false
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
lunchmoney-1.4.0 lib/lunchmoney/validators.rb
lunchmoney-1.2.0 lib/lunchmoney/validators.rb
lunchmoney-1.1.2 lib/lunchmoney/validators.rb
lunchmoney-1.1.1 lib/lunchmoney/validators.rb
lunchmoney-1.1.0 lib/lunchmoney/validators.rb
lunchmoney-1.0.0 lib/lunchmoney/validators.rb
lunchmoney-0.10.0 lib/lunchmoney/validators.rb