Module: Chef::Mixin::ParamsValidate

Defined Under Namespace

Classes: SetOrReturnProperty

Instance Method Summary collapse

Instance Method Details

#lazy(&block) ⇒ Object


117
118
119
# File 'lib/chef/mixin/params_validate.rb', line 117

def lazy(&block)
  DelayedEvaluator.new(&block)
end

#set_or_return(symbol, value, validation) ⇒ Object


121
122
123
124
# File 'lib/chef/mixin/params_validate.rb', line 121

def set_or_return(symbol, value, validation)
  property = SetOrReturnProperty.new(name: symbol, **validation)
  property.call(self, value)
end

#validate(opts, map) ⇒ Object

Takes a hash of options, along with a map to validate them. Returns the original options hash, plus any changes that might have been made (through things like setting default values in the validation map)

For example:

validate({ :one => "neat" }, { :one => { :kind_of => String }})

Would raise an exception if the value of :one above is not a kind_of? string. Valid map options are:

Parameters:

  • opts (Hash<Symbol,Object>)

    Validation opts. @option opts [Object,Array] :is An object, or list of objects, that must match the value using Ruby's === operator (opts[:is].any? { |v| v === value }). (See #_pv_is.) @option opts [Object,Array] :equal_to An object, or list of objects, that must be equal to the value using Ruby's == operator (opts[:is].any? { |v| v == value }) (See #_pv_equal_to.) @option opts [Regexp,Array] :regex An object, or list of objects, that must match the value with regex.match(value). (See #_pv_regex) @option opts [Class,Array] :kind_of A class, or list of classes, that the value must be an instance of. (See #_pv_kind_of.) @option opts [Hash] :callbacks A hash of messages -> procs, all of which match the value. The proc must return a truthy or falsey value (true means it matches). (See #_pv_callbacks.) @option opts [Symbol,Array] :respond_to A method name, or list of method names, the value must respond to. (See #_pv_respond_to.) @option opts [Symbol,Array] :cannot_be A property, or a list of properties, that the value cannot have (such as :nil or :empty). The method with a questionmark at the end is called on the value (e.g. value.empty?). If the value does not have this method, it is considered valid (i.e. if you don't respond to empty? we assume you are not empty). (See #_pv_cannot_be.) @option opts [Proc] :coerce A proc which will be called to transform the user input to canonical form. The value is passed in, and the transformed value returned as output. Lazy values will not be passed to this method until after they are evaluated. Called in the context of the resource (meaning you can access other properties). (See #_pv_coerce.) (See #_pv_coerce.) @option opts [Boolean] :required true if this property must be present and not nil; false otherwise. This is checked after the resource is fully initialized. (See #_pv_required.) @option opts [Boolean] :name_property true if this property defaults to the same value as name. Equivalent to default: lazy { name }, except that #property_is_set? will return true if the property is set or if name is set. (See #_pv_name_property.) @option opts [Boolean] :name_attribute Same as name_property. @option opts [Object] :default The value this property will return if the user does not set one. If this is lazy, it will be run in the context of the instance (and able to access other properties). (See #_pv_default.)

Raises:

  • (ArgumentError)

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/chef/mixin/params_validate.rb', line 83

def validate(opts, map)
  map = map.validation_options if map.is_a?(Property)

  #--
  # validate works by taking the keys in the validation map, assuming it's a hash, and
  # looking for _pv_:symbol as methods.  Assuming it find them, it calls the right
  # one.
  #++
  raise ArgumentError, "Options must be a hash" unless opts.kind_of?(Hash)
  raise ArgumentError, "Validation Map must be a hash" unless map.kind_of?(Hash)

  map.each do |key, validation|
    unless key.kind_of?(Symbol) || key.kind_of?(String)
      raise ArgumentError, "Validation map keys must be symbols or strings!"
    end
    case validation
    when true
      _pv_required(opts, key)
    when false
      true
    when Hash
      validation.each do |check, carg|
        check_method = "_pv_#{check}"
        if respond_to?(check_method, true)
          send(check_method, opts, key, carg)
        else
          raise ArgumentError, "Validation map has unknown check: #{check}"
        end
      end
    end
  end
  opts
end