Skip to content

Commit a9c6bbe

Browse files
authored
Merge pull request #11 from OpenCageData/skip-invalid-component-names
Skip invalid component names
2 parents 6bb82c5 + 0a8ffc6 commit a9c6bbe

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

lib/Geo/Address/Formatter.pm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,13 @@ sub format_address {
287287

288288
# 2. deal with the options
289289

290+
foreach (grep { ! /^[a-z0-9_]+$/ } keys %$rh_components) {
291+
if ($debug){
292+
say STDERR "Skipping compontent $_";
293+
}
294+
delete $rh_components->{$_};
295+
}
296+
290297
# 2a. which country format will we use?
291298
# might have been specified in options
292299
# otherwise look at components

t/unit/formatter.t

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,32 @@ my $GAF = $CLASS->new(conf_path => $path);
4646
}
4747
}
4848

49+
# Make sure invalid component names are skipped
50+
{
51+
my $af_path = dirname(__FILE__) . '/../../address-formatting';
52+
my $conf_path = $af_path . '/conf/';
53+
my $GAF = $CLASS->new(conf_path => $conf_path);
54+
55+
my $rh_components = {
56+
'' => 'empty',
57+
'*' => 'star',
58+
'+' => 'plus',
59+
'\\' => 'escape',
60+
'$' => 'dollar',
61+
'(' => 'bracket',
62+
'\(' => 'quoted bracket',
63+
'country' => 'United Kingdom',
64+
'country_code' => 'gb',
65+
};
66+
67+
my $formatted = $GAF->format_address($rh_components, {});
68+
chomp $formatted;
69+
70+
is($formatted, 'United Kingdom');
71+
}
72+
73+
74+
4975
{
5076
# keep in mind this is using the test conf, not the real address-formatting conf
5177
is($GAF->_add_state_code({}), undef);

0 commit comments

Comments
 (0)