Module: Chef::Mixin::ParamsValidate

Defined Under Namespace

Classes: SetOrReturnProperty

Instance Method Summary collapse

Instance Method Details

#lazy(&block) ⇒ Object



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

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

#set_or_return(symbol, value, validation) ⇒ Object



127
128
129
130
# File 'lib/chef/mixin/params_validate.rb', line 127

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 [String] :validation_message A custom message to return

    should validation fail.
    

    @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[:equal_to].any? { |v| v == value }`)  (See #_pv_equal_to.)
    

    @option opts [Regexp,Array<Regexp>] :regex An object, or

    list of objects, that must match the value with `regex.match(value)`.
    (See #_pv_regex)
    

    @option opts [Class,Array<Class>] :kind_of A class, or

    list of classes, that the value must be an instance of.  (See
    #_pv_kind_of.)
    

    @option opts [Hash<String,Proc>] :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<Symbol>] :respond_to A method

    name, or list of method names, the value must respond to.  (See
    #_pv_respond_to.)
    

    @option opts [Symbol,Array<Symbol>] :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)


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
116
117
118
119
120
121
# File 'lib/chef/mixin/params_validate.rb', line 85

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.is_a?(Hash)
  raise ArgumentError, "Validation Map must be a hash" unless map.is_a?(Hash)

  @validation_message ||= {}

  map.each do |key, validation|
    unless key.is_a?(Symbol) || key.is_a?(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_message[key] = validation.delete(:validation_message) if validation.key?(:validation_message)
      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