Module: Chef::Mixin::ParamsValidate
- Extended by:
- ParamsValidate
- Included in:
- ApiClient, ApiClientV1, Cookbook::Metadata, DataBag, DataBagItem, Environment, FileCache, Key, ParamsValidate, Properties, Node, Org, ResourceDefinition, Role, RunList, Runner, User, UserV1
- Defined in:
- lib/chef/mixin/params_validate.rb
Defined Under Namespace
Classes: SetOrReturnProperty
Instance Method Summary collapse
- #lazy(&block) ⇒ Object
- #set_or_return(symbol, value, validation) ⇒ Object
-
#validate(opts, map) ⇒ Object
Takes a hash of options, along with a map to validate them.
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:
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. 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 |