Skip to content

Commit

Permalink
Introduce try_validate
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidS committed May 22, 2018
1 parent c42df5e commit 83868ef
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
23 changes: 20 additions & 3 deletions lib/puppet/resource_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -517,13 +517,30 @@ def self.try_mungify(type, value, error_msg_prefix)
# fall through to default handling
end

# a match!
return [value, nil] if type.instance?(value)
is_valid, error_msg = try_validate(type, value, error_msg_prefix)
if is_valid
# a match!
[value, nil]
else
# an error :-(
[nil, error_msg]
end
end

# Tries to validate the `value` against the specified `type`.
# @param type[Puppet::Pops::Types::TypedModelObject] the type to check against
# @param value the value to clean
# @param error_msg_prefix[String] a prefix for the error messages
# @return [Array] The first element of the array is `true` or `false` indication the validity of the value. If the value is not valid (`false`),
# then the second element of the array is a error message indicating the problem, nil otherwise.
# @private
def self.try_validate(type, value, error_msg_prefix)
return [true, nil] if type.instance?(value)

# an error :-(
inferred_type = Puppet::Pops::Types::TypeCalculator.infer_set(value)
error_msg = Puppet::Pops::Types::TypeMismatchDescriber.new.describe_mismatch(error_msg_prefix, type, inferred_type)
[nil, error_msg]
[false, error_msg]
end

def self.validate_ensure(definition)
Expand Down
47 changes: 47 additions & 0 deletions spec/puppet/resource_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,53 @@ def set(_context, changes) end

# keep test data consistent # rubocop:disable Style/WordArray
# run try_mungify only once to get both value, and error # rubocop:disable RSpec/InstanceVariable
describe '#try_validate(type, value)' do
let(:result) do
pops_type = Puppet::Pops::Types::TypeParser.singleton.parse(type)
described_class.try_validate(pops_type, value, 'error prefix')
end
let(:is_valid) { result[0] }
let(:error_msg) { result[1] }

[
{
type: 'String',
valid: ['a', 'true'],
invalid: [1, true],
},
{
type: 'Integer',
valid: [1, -1, 0],
invalid: ['a', :a, 'true', 1.0],
},
].each do |testcase|
context "when validating '#{testcase[:type]}" do
let(:type) { testcase[:type] }

testcase[:valid].each do |valid_value|
context "when validating #{valid_value.inspect}" do
let(:value) { valid_value }

it { expect(result).to be_a Array }
it { expect(result.size).to eq 2 }
it { expect(is_valid).to be true }
it { expect(error_msg).to be nil }
end
end
testcase[:invalid].each do |invalid_value|
context "when validating #{invalid_value.inspect}" do
let(:value) { invalid_value }

it { expect(result).to be_a Array }
it { expect(result.size).to eq 2 }
it { expect(is_valid).to be false }
it { expect(error_msg).to match %r{^error prefix } }
end
end
end
end
end

describe '#try_mungify(type, value)' do
before(:each) do
@value, @error = described_class.try_mungify(type, input, 'error prefix')
Expand Down

0 comments on commit 83868ef

Please sign in to comment.