Skip to content

Commit 1bcbf87

Browse files
committed
Deprecate the validate_legacy() function
`validate_legacy()` was added to help migrating from the legacy validation functions to the new Puppet data types which where not 100% backward compatible with the previous validation (e.g. `'42'` was a valid integer according to `validate_integer()` but indeed a String and not an Integer when using data types. The legacy validation functions have been removed from stdlib, so `validate_legacy()` will now fail if it tries to run them. But as they produced deprecation warning, they are supposed to have already been fixed. For the sake of security, instead of removing `validate_legacy()` now, deprecate it so that it is strictly equivalent to validating using a data type, but also emits a warning.
1 parent bdf14f5 commit 1bcbf87

File tree

2 files changed

+21
-47
lines changed

2 files changed

+21
-47
lines changed

lib/puppet/functions/validate_legacy.rb

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# frozen_string_literal: true
22

33
# @summary
4-
# Validate a value against both the target_type (new) and the previous_validation function (old).
4+
# **Deprecated:** Validate a value against both the target_type (new).
55
Puppet::Functions.create_function(:validate_legacy) do
6-
# The function checks a value against both the target_type (new) and the previous_validation function (old).
6+
# The function checks a value against both the target_type (new).
77
# @param scope
88
# The main value that will be passed to the method
99
# @param target_type
1010
# @param function_name
11+
# Unused
1112
# @param value
1213
# @param args
1314
# Any additional values that are to be passed to the method
@@ -25,6 +26,7 @@
2526
# The main value that will be passed to the method
2627
# @param type_string
2728
# @param function_name
29+
# Unused
2830
# @param value
2931
# @param args Any additional values that are to be passed to the method
3032
# @return Legacy validation method
@@ -49,33 +51,17 @@ def validate_legacy_s(scope, type_string, *args)
4951
validate_legacy(scope, t, *args)
5052
end
5153

52-
def validate_legacy(scope, target_type, function_name, value, *prev_args)
54+
def validate_legacy(_scope, target_type, _function_name, value, *_prev_args)
55+
call_function('deprecation', 'validate_legacy', 'This method is deprecated, please use Puppet data types to validate parameters')
5356
if assert_type(target_type, value)
54-
if previous_validation(scope, function_name, value, *prev_args)
55-
# Silently passes
56-
else
57-
Puppet.notice("Accepting previously invalid value for target type '#{target_type}'")
58-
end
57+
# "Silently" passes
5958
else
6059
inferred_type = Puppet::Pops::Types::TypeCalculator.infer_set(value)
61-
error_msg = Puppet::Pops::Types::TypeMismatchDescriber.new.describe_mismatch("validate_legacy(#{function_name})", target_type, inferred_type)
62-
if previous_validation(scope, function_name, value, *prev_args)
63-
call_function('deprecation', 'validate_legacy', error_msg)
64-
else
65-
call_function('fail', error_msg)
66-
end
60+
error_msg = Puppet::Pops::Types::TypeMismatchDescriber.new.describe_mismatch("validate_legacy(#{target_type}, ...)", target_type, inferred_type)
61+
call_function('fail', error_msg)
6762
end
6863
end
6964

70-
def previous_validation(scope, function_name, value, *prev_args)
71-
# Call the previous validation function and catch any errors. Return true if no errors are thrown.
72-
73-
scope.send("function_#{function_name}".to_s, [value, *prev_args])
74-
true
75-
rescue Puppet::ParseError
76-
false
77-
end
78-
7965
def assert_type(type, value)
8066
Puppet::Pops::Types::TypeCalculator.instance?(type, value)
8167
end

spec/functions/validate_legacy_spec.rb

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,37 @@
77
it { is_expected.not_to eq(nil) }
88
it { is_expected.to run.with_params.and_raise_error(ArgumentError) }
99

10-
describe 'when passing the type assertion and passing the previous validation' do
11-
it 'passes without notice' do
12-
expect(scope).to receive(:function_validate_foo).with([5]).once
10+
describe 'when passing the type assertion' do
11+
it 'passes with a deprecation warning' do
12+
expect(subject.func).to receive(:call_function).with('deprecation', 'validate_legacy', include('deprecated')).once
13+
expect(scope).to receive(:function_validate_foo).never
1314
expect(Puppet).to receive(:notice).never
1415
is_expected.to run.with_params('Integer', 'validate_foo', 5)
1516
end
1617
end
1718

18-
describe 'when passing the type assertion and failing the previous validation' do
19-
it 'passes with a notice about newly accepted value' do
20-
expect(scope).to receive(:function_validate_foo).with([5]).and_raise(Puppet::ParseError, 'foo').once
21-
expect(Puppet).to receive(:notice).with(include('Accepting previously invalid value for target type'))
22-
is_expected.to run.with_params('Integer', 'validate_foo', 5)
23-
end
24-
end
25-
26-
describe 'when failing the type assertion and passing the previous validation' do
27-
it 'passes with a deprecation message' do
28-
expect(scope).to receive(:function_validate_foo).with(['5']).once
29-
expect(subject.func).to receive(:call_function).with('deprecation', 'validate_legacy', include('Integer')).once
30-
is_expected.to run.with_params('Integer', 'validate_foo', '5')
31-
end
32-
end
33-
34-
describe 'when failing the type assertion and failing the previous validation' do
19+
describe 'when failing the type assertion' do
3520
it 'fails with a helpful message' do
36-
expect(scope).to receive(:function_validate_foo).with(['5']).and_raise(Puppet::ParseError, 'foo').once
37-
expect(subject.func).to receive(:call_function).with('fail', include('Integer')).once
21+
expect(subject.func).to receive(:call_function).with('deprecation', 'validate_legacy', include('deprecated')).once
22+
expect(scope).to receive(:function_validate_foo).never
23+
expect(subject.func).to receive(:call_function).with('fail', 'validate_legacy(Integer, ...) expects an Integer value, got String').once
3824
is_expected.to run.with_params('Integer', 'validate_foo', '5')
3925
end
4026
end
4127

4228
describe 'when passing in undef' do
4329
it 'works' do
44-
expect(scope).to receive(:function_validate_foo).with([:undef]).once
30+
expect(subject.func).to receive(:call_function).with('deprecation', 'validate_legacy', include('deprecated')).once
31+
expect(scope).to receive(:function_validate_foo).never
4532
expect(Puppet).to receive(:notice).never
4633
is_expected.to run.with_params('Optional[Integer]', 'validate_foo', :undef)
4734
end
4835
end
4936

5037
describe 'when passing in multiple arguments' do
5138
it 'passes with a deprecation message' do
52-
expect(scope).to receive(:function_validate_foo).with([:undef, 1, 'foo']).once
39+
expect(subject.func).to receive(:call_function).with('deprecation', 'validate_legacy', include('deprecated')).once
40+
expect(scope).to receive(:function_validate_foo).never
5341
expect(Puppet).to receive(:notice).never
5442
is_expected.to run.with_params('Optional[Integer]', 'validate_foo', :undef, 1, 'foo')
5543
end

0 commit comments

Comments
 (0)