File Coverage

blib/lib/Sah/Schema/country/code/alpha2.pm
Criterion Covered Total %
statement 6 6 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 8 8 100.0


line stmt bran cond sub pod time code
1             package Sah::Schema::country::code::alpha2;
2              
3 1     1   412397 use strict;
  1         3  
  1         48  
4 1     1   1575 use Locale::Codes::Country_Codes ();
  1         51871  
  1         798  
5              
6             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
7             our $DATE = '2023-08-07'; # DATE
8             our $DIST = 'Sah-Schemas-Country'; # DIST
9             our $VERSION = '0.009'; # VERSION
10              
11             my $codes = [];
12             my $names = [];
13             {
14             for my $alpha2 (keys(%{ $Locale::Codes::Data{'country'}{'code2id'}{'alpha-2'} })) {
15             push @$codes, $alpha2;
16             my $id = $Locale::Codes::Data{'country'}{'code2id'}{'alpha-2'}{$alpha2}[0];
17             push @$names, $Locale::Codes::Data{'country'}{'id2names'}{$id}[0];
18             }
19              
20             die "Can't extract country codes from Locale::Codes::Country_Codes"
21             unless @$codes;
22             }
23              
24             our $schema = [str => {
25             summary => 'Country code (alpha-2)',
26             description => <<'_',
27              
28             Accept only current (not retired) codes. Only alpha-2 codes are accepted.
29              
30             Code will be converted to lowercase.
31              
32             _
33             match => '\A[a-z]{2}\z',
34             in => $codes,
35             'x.in.summaries' => $names,
36             'x.perl.coerce_rules' => ['From_str::to_lower'],
37             examples => [
38             {value=>'', valid=>0},
39             {value=>'ID' , valid=>1, validated_value=>'id'},
40             {value=>'IDN', valid=>0, summary=>'Only alpha-2 codes are allowed'},
41             {value=>'xx', valid=>0},
42             {value=>'xxx', valid=>0},
43             ],
44             }];
45              
46             1;
47             # ABSTRACT: Country code (alpha-2)
48              
49             __END__
50              
51             =pod
52              
53             =encoding UTF-8
54              
55             =head1 NAME
56              
57             Sah::Schema::country::code::alpha2 - Country code (alpha-2)
58              
59             =head1 VERSION
60              
61             This document describes version 0.009 of Sah::Schema::country::code::alpha2 (from Perl distribution Sah-Schemas-Country), released on 2023-08-07.
62              
63             =head1 SYNOPSIS
64              
65             =head2 Sample data and validation results against this schema
66              
67             "" # INVALID
68              
69             "ID" # valid, becomes "id"
70              
71             "IDN" # INVALID (Only alpha-2 codes are allowed)
72              
73             "xx" # INVALID
74              
75             "xxx" # INVALID
76              
77             =head2 Using with Data::Sah
78              
79             To check data against this schema (requires L<Data::Sah>):
80              
81             use Data::Sah qw(gen_validator);
82             my $validator = gen_validator("country::code::alpha2*");
83             say $validator->($data) ? "valid" : "INVALID!";
84              
85             The above validator returns a boolean result (true if data is valid, false if
86             otherwise). To return an error message string instead (empty string if data is
87             valid, a non-empty error message otherwise):
88              
89             my $validator = gen_validator("country::code::alpha2", {return_type=>'str_errmsg'});
90             my $errmsg = $validator->($data);
91            
92             # a sample valid data
93             $data = "ID";
94             my $errmsg = $validator->($data); # => ""
95            
96             # a sample invalid data
97             $data = "xx";
98             my $errmsg = $validator->($data); # => "Must be one of [\"hu\",\"tv\",\"ca\",\"pk\",\"by\",\"tf\",\"pn\",\"tm\",\"bl\",\"ie\",\"pm\",\"tn\",\"lt\",\"pf\",\"tk\",\"cg\",\"ls\",\"ga\",\"mg\",\"mq\",\"ph\",\"jp\",\"fr\",\"dm\",\"yt\",\"ax\",\"as\",\"bb\",\"th\",\"sr\",\"kg\",\"dk\",\"us\",\"ma\",\"ee\",\"at\",\"gg\",\"io\",\"gq\",\"ec\",\"lv\",\"jo\",\"qa\",\"ps\",\"bi\",\"sa\",\"pt\",\"mr\",\"vn\",\"tt\",\"lk\",\"ro\",\"id\",\"gr\",\"kr\",\"sg\",\"am\",\"sj\",\"af\",\"um\",\"je\",\"nf\",\"bw\",\"fj\",\"re\",\"cr\",\"mx\",\"na\",\"gs\",\"aq\",\"gt\",\"il\",\"ag\",\"ug\",\"tr\",\"sh\",\"om\",\"ua\",\"mt\",\"pr\",\"rw\",\"ru\",\"be\",\"ng\",\"ms\",\"vg\",\"sn\",\"fm\",\"sk\",\"cx\",\"sm\",\"fk\",\"bo\",\"sv\",\"la\",\"va\",\"kn\",\"zw\",\"ch\",\"mv\",\"lr\",\"gn\",\"mf\",\"mm\",\"bd\",\"mn\",\"bz\",\"gf\",\"gm\",\"mk\",\"km\",\"tj\",\"ar\",\"pa\",\"ss\",\"cn\",\"tg\",\"ck\",\"kh\",\"gh\",\"st\",\"mh\",\"nr\",\"cv\",\"dj\",\"pg\",\"sx\",\"cm\",\"cf\",\"zm\",\"ci\",\"sl\",\"de\",\"rs\",\"sy\",\"ad\",\"uz\",\"az\",\"mu\",\"mw\",\"to\",\"gw\",\"gu\",\"lc\",\"vc\",\"kw\",\"nz\",\"np\",\"nc\",\"ki\",\"in\",\"gi\",\"do\",\"eh\",\"br\",\"sb\",\"pe\",\"im\",\"cu\",\"cw\",\"es\",\"hr\",\"jm\",\"dz\",\"cl\",\"cy\",\"ba\",\"et\",\"si\",\"ae\",\"bg\",\"bq\",\"fi\",\"ne\",\"ye\",\"tc\",\"gb\",\"ao\",\"it\",\"gl\",\"tz\",\"gy\",\"ve\",\"td\",\"bj\",\"ky\",\"is\",\"no\",\"ml\",\"my\",\"bh\",\"ni\",\"lu\",\"vu\",\"cz\",\"er\",\"cd\",\"se\",\"mc\",\"ht\",\"ai\",\"py\",\"au\",\"aw\",\"pl\",\"bn\",\"mz\",\"md\",\"mp\",\"fo\",\"cc\",\"bv\",\"so\",\"kp\",\"ws\",\"li\",\"vi\",\"kz\",\"nu\",\"bf\",\"bm\",\"tl\",\"gp\",\"gd\",\"ir\",\"nl\",\"lb\",\"hm\",\"tw\",\"mo\",\"uy\",\"pw\",\"al\",\"iq\",\"hn\",\"hk\",\"sd\",\"sz\",\"sc\",\"me\",\"wf\",\"bt\",\"bs\",\"ke\",\"za\",\"ge\",\"eg\",\"co\",\"ly\"]"
99              
100             Often a schema has coercion rule or default value rules, so after validation the
101             validated value will be different from the original. To return the validated
102             (set-as-default, coerced, prefiltered) value:
103              
104             my $validator = gen_validator("country::code::alpha2", {return_type=>'str_errmsg+val'});
105             my $res = $validator->($data); # [$errmsg, $validated_val]
106            
107             # a sample valid data
108             $data = "ID";
109             my $res = $validator->($data); # => ["","id"]
110            
111             # a sample invalid data
112             $data = "xx";
113             my $res = $validator->($data); # => ["Must be one of [\"hu\",\"tv\",\"ca\",\"pk\",\"by\",\"tf\",\"pn\",\"tm\",\"bl\",\"ie\",\"pm\",\"tn\",\"lt\",\"pf\",\"tk\",\"cg\",\"ls\",\"ga\",\"mg\",\"mq\",\"ph\",\"jp\",\"fr\",\"dm\",\"yt\",\"ax\",\"as\",\"bb\",\"th\",\"sr\",\"kg\",\"dk\",\"us\",\"ma\",\"ee\",\"at\",\"gg\",\"io\",\"gq\",\"ec\",\"lv\",\"jo\",\"qa\",\"ps\",\"bi\",\"sa\",\"pt\",\"mr\",\"vn\",\"tt\",\"lk\",\"ro\",\"id\",\"gr\",\"kr\",\"sg\",\"am\",\"sj\",\"af\",\"um\",\"je\",\"nf\",\"bw\",\"fj\",\"re\",\"cr\",\"mx\",\"na\",\"gs\",\"aq\",\"gt\",\"il\",\"ag\",\"ug\",\"tr\",\"sh\",\"om\",\"ua\",\"mt\",\"pr\",\"rw\",\"ru\",\"be\",\"ng\",\"ms\",\"vg\",\"sn\",\"fm\",\"sk\",\"cx\",\"sm\",\"fk\",\"bo\",\"sv\",\"la\",\"va\",\"kn\",\"zw\",\"ch\",\"mv\",\"lr\",\"gn\",\"mf\",\"mm\",\"bd\",\"mn\",\"bz\",\"gf\",\"gm\",\"mk\",\"km\",\"tj\",\"ar\",\"pa\",\"ss\",\"cn\",\"tg\",\"ck\",\"kh\",\"gh\",\"st\",\"mh\",\"nr\",\"cv\",\"dj\",\"pg\",\"sx\",\"cm\",\"cf\",\"zm\",\"ci\",\"sl\",\"de\",\"rs\",\"sy\",\"ad\",\"uz\",\"az\",\"mu\",\"mw\",\"to\",\"gw\",\"gu\",\"lc\",\"vc\",\"kw\",\"nz\",\"np\",\"nc\",\"ki\",\"in\",\"gi\",\"do\",\"eh\",\"br\",\"sb\",\"pe\",\"im\",\"cu\",\"cw\",\"es\",\"hr\",\"jm\",\"dz\",\"cl\",\"cy\",\"ba\",\"et\",\"si\",\"ae\",\"bg\",\"bq\",\"fi\",\"ne\",\"ye\",\"tc\",\"gb\",\"ao\",\"it\",\"gl\",\"tz\",\"gy\",\"ve\",\"td\",\"bj\",\"ky\",\"is\",\"no\",\"ml\",\"my\",\"bh\",\"ni\",\"lu\",\"vu\",\"cz\",\"er\",\"cd\",\"se\",\"mc\",\"ht\",\"ai\",\"py\",\"au\",\"aw\",\"pl\",\"bn\",\"mz\",\"md\",\"mp\",\"fo\",\"cc\",\"bv\",\"so\",\"kp\",\"ws\",\"li\",\"vi\",\"kz\",\"nu\",\"bf\",\"bm\",\"tl\",\"gp\",\"gd\",\"ir\",\"nl\",\"lb\",\"hm\",\"tw\",\"mo\",\"uy\",\"pw\",\"al\",\"iq\",\"hn\",\"hk\",\"sd\",\"sz\",\"sc\",\"me\",\"wf\",\"bt\",\"bs\",\"ke\",\"za\",\"ge\",\"eg\",\"co\",\"ly\"]","xx"]
114              
115             Data::Sah can also create validator that returns a hash of detailed error
116             message. Data::Sah can even create validator that targets other language, like
117             JavaScript, from the same schema. Other things Data::Sah can do: show source
118             code for validator, generate a validator code with debug comments and/or log
119             statements, generate human text from schema. See its documentation for more
120             details.
121              
122             =head2 Using with Params::Sah
123              
124             To validate function parameters against this schema (requires L<Params::Sah>):
125              
126             use Params::Sah qw(gen_validator);
127              
128             sub myfunc {
129             my @args = @_;
130             state $validator = gen_validator("country::code::alpha2*");
131             $validator->(\@args);
132             ...
133             }
134              
135             =head2 Using with Perinci::CmdLine::Lite
136              
137             To specify schema in L<Rinci> function metadata and use the metadata with
138             L<Perinci::CmdLine> (L<Perinci::CmdLine::Lite>) to create a CLI:
139              
140             # in lib/MyApp.pm
141             package
142             MyApp;
143             our %SPEC;
144             $SPEC{myfunc} = {
145             v => 1.1,
146             summary => 'Routine to do blah ...',
147             args => {
148             arg1 => {
149             summary => 'The blah blah argument',
150             schema => ['country::code::alpha2*'],
151             },
152             ...
153             },
154             };
155             sub myfunc {
156             my %args = @_;
157             ...
158             }
159             1;
160              
161             # in myapp.pl
162             package
163             main;
164             use Perinci::CmdLine::Any;
165             Perinci::CmdLine::Any->new(url=>'/MyApp/myfunc')->run;
166              
167             # in command-line
168             % ./myapp.pl --help
169             myapp - Routine to do blah ...
170             ...
171              
172             % ./myapp.pl --version
173              
174             % ./myapp.pl --arg1 ...
175              
176             =head2 Using on the CLI with validate-with-sah
177              
178             To validate some data on the CLI, you can use L<validate-with-sah> utility.
179             Specify the schema as the first argument (encoded in Perl syntax) and the data
180             to validate as the second argument (encoded in Perl syntax):
181              
182             % validate-with-sah '"country::code::alpha2*"' '"data..."'
183              
184             C<validate-with-sah> has several options for, e.g. validating multiple data,
185             showing the generated validator code (Perl/JavaScript/etc), or loading
186             schema/data from file. See its manpage for more details.
187              
188              
189             =head2 Using with Type::Tiny
190              
191             To create a type constraint and type library from a schema (requires
192             L<Type::Tiny> as well as L<Type::FromSah>):
193              
194             package My::Types {
195             use Type::Library -base;
196             use Type::FromSah qw( sah2type );
197              
198             __PACKAGE__->add_type(
199             sah2type('country::code::alpha2*', name=>'CountryCodeAlpha2')
200             );
201             }
202              
203             use My::Types qw(CountryCodeAlpha2);
204             CountryCodeAlpha2->assert_valid($data);
205              
206             =head1 DESCRIPTION
207              
208             Accept only current (not retired) codes. Only alpha-2 codes are accepted.
209              
210             Code will be converted to lowercase.
211              
212             =head1 HOMEPAGE
213              
214             Please visit the project's homepage at L<https://metacpan.org/release/Sah-Schemas-Country>.
215              
216             =head1 SOURCE
217              
218             Source repository is at L<https://github.com/perlancar/perl-Sah-Schemas-Country>.
219              
220             =head1 SEE ALSO
221              
222             L<Sah::Schema::country::code::alpha3>
223              
224             L<Sah::Schema::country::code>
225              
226             =head1 AUTHOR
227              
228             perlancar <perlancar@cpan.org>
229              
230             =head1 CONTRIBUTING
231              
232              
233             To contribute, you can send patches by email/via RT, or send pull requests on
234             GitHub.
235              
236             Most of the time, you don't need to build the distribution yourself. You can
237             simply modify the code, then test via:
238              
239             % prove -l
240              
241             If you want to build the distribution (e.g. to try to install it locally on your
242             system), you can install L<Dist::Zilla>,
243             L<Dist::Zilla::PluginBundle::Author::PERLANCAR>,
244             L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
245             Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond
246             that are considered a bug and can be reported to me.
247              
248             =head1 COPYRIGHT AND LICENSE
249              
250             This software is copyright (c) 2023, 2020, 2019, 2018 by perlancar <perlancar@cpan.org>.
251              
252             This is free software; you can redistribute it and/or modify it under
253             the same terms as the Perl 5 programming language system itself.
254              
255             =head1 BUGS
256              
257             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Sah-Schemas-Country>
258              
259             When submitting a bug or request, please include a test-file or a
260             patch to an existing test-file that illustrates the bug or desired
261             feature.
262              
263             =cut