2 # Validate a value against both the target_type (new) and the previous_validation function (old).
3 Puppet::Functions.create_function(:validate_legacy) do
4 # The function checks a value against both the target_type (new) and the previous_validation function (old).
6 # The main value that will be passed to the method
11 # Any additional values that are to be passed to the method
13 # A boolean value (`true` or `false`) returned from the called function.
14 dispatch :validate_legacy do
16 param 'Type', :target_type
17 param 'String', :function_name
19 repeated_param 'Any', :args
23 # The main value that will be passed to the method
25 # @param function_name
27 # @param args Any additional values that are to be passed to the method
28 # @return Legacy validation method
30 dispatch :validate_legacy_s do
32 param 'String', :type_string
33 param 'String', :function_name
35 repeated_param 'Any', :args
38 # Workaround PUP-4438 (fixed: https://github.com/puppetlabs/puppet/commit/e01c4dc924cd963ff6630008a5200fc6a2023b08#diff-
39 # c937cc584953271bb3d3b3c2cb141790R221) to support puppet < 4.1.0 and puppet < 3.8.1.
40 def call(scope, *args)
41 manipulated_args = [scope] + args
42 self.class.dispatcher.dispatch(self, scope, manipulated_args)
45 def validate_legacy_s(scope, type_string, *args)
46 t = Puppet::Pops::Types::TypeParser.new.parse(type_string, scope)
47 validate_legacy(scope, t, *args)
50 def validate_legacy(scope, target_type, function_name, value, *prev_args)
51 if assert_type(target_type, value)
52 if previous_validation(scope, function_name, value, *prev_args)
55 Puppet.notice("Accepting previously invalid value for target type '#{target_type}'")
58 inferred_type = Puppet::Pops::Types::TypeCalculator.infer_set(value)
59 error_msg = Puppet::Pops::Types::TypeMismatchDescriber.new.describe_mismatch("validate_legacy(#{function_name})", target_type, inferred_type)
60 if previous_validation(scope, function_name, value, *prev_args)
61 call_function('deprecation', 'validate_legacy', error_msg)
63 call_function('fail', error_msg)
68 def previous_validation(scope, function_name, value, *prev_args)
69 # Call the previous validation function and catch any errors. Return true if no errors are thrown.
71 scope.send("function_#{function_name}".to_s, [value, *prev_args])
73 rescue Puppet::ParseError
77 def assert_type(type, value)
78 Puppet::Pops::Types::TypeCalculator.instance?(type, value)