Sha256: 2c4afaa20b69f8bf11193a4a148773e834a4f67447521c07063bb771a51f9c07

Contents?: true

Size: 550 Bytes

Versions: 4

Compression:

Stored size: 550 Bytes

Contents

module ChefAPI
  class Validator::Type < Validator::Base
    attr_reader :types

    #
    # Overload the super method to capture the type attribute in the options
    # hash.
    #
    def initialize(attribute, type)
      super
      @types = Array(type)
    end

    def validate(resource)
      value = resource._attributes[attribute]

      if value && !types.any? { |type| value.is_a?(type) }
        short_name = types.to_s.split("::").last
        resource.errors.add(attribute, "must be a kind of #{short_name}")
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
chef-infra-api-0.10.10 lib/chef-api/validators/type.rb
chef-api-0.10.10 lib/chef-api/validators/type.rb
chef-infra-api-0.10.7 lib/chef-api/validators/type.rb
chef-api-0.10.7 lib/chef-api/validators/type.rb