File Coverage

blib/lib/Sah/Schema/country/code.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;
2              
3 1     1   400744 use strict;
  1         3  
  1         46  
4 1     1   2364 use Locale::Codes::Country_Codes ();
  1         54707  
  1         757  
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             for my $alpha3 (keys(%{ $Locale::Codes::Data{'country'}{'code2id'}{'alpha-3'} })) {
20             push @$codes, $alpha3;
21             my $id = $Locale::Codes::Data{'country'}{'code2id'}{'alpha-3'}{$alpha3}[0];
22             push @$names, $Locale::Codes::Data{'country'}{'id2names'}{$id}[0];
23             }
24              
25             die "Can't extract country codes from Locale::Codes::Country_Codes"
26             unless @$codes;
27             }
28              
29             our $schema = [str => {
30             summary => 'Country code (alpha-2 or alpha-3)',
31             description => <<'_',
32              
33             Accept only current (not retired) codes. Alpha-2 or alpha-3 codes are accepted.
34              
35             Code will be converted to lowercase.
36              
37             _
38             match => '\A[a-z]{2,3}\z',
39             in => $codes,
40             'x.in.summaries' => $names,
41             'x.perl.coerce_rules' => ['From_str::to_lower'],
42             examples => [
43             {value=>'', valid=>0},
44             {value=>'ID' , valid=>1, validated_value=>'id'},
45             {value=>'IDN', valid=>1, validated_value=>'idn'},
46             {value=>'xx', valid=>0},
47             {value=>'xxx', valid=>0},
48             ],
49             }];
50              
51             1;
52             # ABSTRACT: Country code (alpha-2 or alpha-3)
53              
54             __END__
55              
56             =pod
57              
58             =encoding UTF-8
59              
60             =head1 NAME
61              
62             Sah::Schema::country::code - Country code (alpha-2 or alpha-3)
63              
64             =head1 VERSION
65              
66             This document describes version 0.009 of Sah::Schema::country::code (from Perl distribution Sah-Schemas-Country), released on 2023-08-07.
67              
68             =head1 SYNOPSIS
69              
70             =head2 Sample data and validation results against this schema
71              
72             "" # INVALID
73              
74             "ID" # valid, becomes "id"
75              
76             "IDN" # valid, becomes "idn"
77              
78             "xx" # INVALID
79              
80             "xxx" # INVALID
81              
82             =head2 Using with Data::Sah
83              
84             To check data against this schema (requires L<Data::Sah>):
85              
86             use Data::Sah qw(gen_validator);
87             my $validator = gen_validator("country::code*");
88             say $validator->($data) ? "valid" : "INVALID!";
89              
90             The above validator returns a boolean result (true if data is valid, false if
91             otherwise). To return an error message string instead (empty string if data is
92             valid, a non-empty error message otherwise):
93              
94             my $validator = gen_validator("country::code", {return_type=>'str_errmsg'});
95             my $errmsg = $validator->($data);
96            
97             # a sample valid data
98             $data = "IDN";
99             my $errmsg = $validator->($data); # => ""
100            
101             # a sample invalid data
102             $data = "xx";
103             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\",\"phl\",\"esp\",\"tkm\",\"ago\",\"jor\",\"cyp\",\"cuw\",\"blm\",\"can\",\"aia\",\"hun\",\"atg\",\"vat\",\"esh\",\"glp\",\"mli\",\"mda\",\"ind\",\"eth\",\"png\",\"asm\",\"bfa\",\"guy\",\"deu\",\"ukr\",\"jpn\",\"prt\",\"cck\",\"lao\",\"tkl\",\"blr\",\"afg\",\"are\",\"gab\",\"swz\",\"vnm\",\"idn\",\"tcd\",\"bgd\",\"srb\",\"ala\",\"mwi\",\"rus\",\"bes\",\"tgo\",\"che\",\"zwe\",\"cok\",\"mne\",\"mar\",\"guf\",\"grl\",\"sjm\",\"mmr\",\"flk\",\"plw\",\"ben\",\"bmu\",\"gnq\",\"bol\",\"lca\",\"sgs\",\"zaf\",\"omn\",\"shn\",\"gtm\",\"nam\",\"lso\",\"iot\",\"svn\",\"gnb\",\"fro\",\"cmr\",\"bra\",\"grc\",\"bel\",\"niu\",\"gmb\",\"eri\",\"pyf\",\"arg\",\"abw\",\"nld\",\"hmd\",\"vgb\",\"dza\",\"hti\",\"hnd\",\"mac\",\"msr\",\"mtq\",\"tto\",\"tca\",\"chn\",\"mhl\",\"egy\",\"sxm\",\"pse\",\"qat\",\"fsm\",\"lby\",\"tza\",\"chl\",\"ita\",\"mrt\",\"moz\",\"mng\",\"cod\",\"pan\",\"khm\",\"smr\",\"arm\",\"cxr\",\"aut\",\"prk\",\"uga\",\"gum\",\"kgz\",\"ncl\",\"kaz\",\"bvt\",\"gbr\",\"lie\",\"nga\",\"ton\",\"ner\",\"mco\",\"ggy\",\"sgp\",\"bgr\",\"rwa\",\"brn\",\"isr\",\"civ\",\"ltu\",\"imn\",\"slv\",\"bih\",\"fji\",\"vut\",\"svk\",\"cri\",\"blz\",\"gin\",\"nzl\",\"wlf\",\"cze\",\"dom\",\"umi\",\"npl\",\"nor\",\"sur\",\"pol\",\"gib\",\"yem\",\"isl\",\"syr\",\"jey\",\"kna\",\"tjk\",\"fin\",\"reu\",\"bhs\",\"brb\",\"cym\",\"cub\",\"maf\",\"pcn\",\"grd\",\"atf\",\"hrv\",\"gha\",\"uzb\",\"rou\",\"nic\",\"aus\",\"zmb\",\"caf\",\"kir\",\"slb\",\"tls\",\"sau\",\"and\",\"per\",\"pak\",\"ecu\",\"geo\",\"ven\",\"tur\",\"bdi\",\"mdv\",\"sen\",\"mdg\",\"lbr\",\"mnp\",\"kor\",\"irl\",\"fra\",\"dma\",\"sle\",\"dnk\",\"irn\",\"jam\",\"wsm\",\"stp\",\"aze\",\"dji\",\"som\",\"pry\",\"mys\",\"cpv\",\"ata\",\"tha\",\"vct\",\"kwt\",\"mex\",\"alb\",\"usa\",\"ury\",\"nru\",\"sdn\",\"spm\",\"pri\",\"mus\",\"lva\",\"bhr\",\"swe\",\"lux\",\"tun\",\"twn\",\"col\",\"lka\",\"mlt\",\"lbn\",\"myt\",\"cog\",\"mkd\",\"com\",\"nfk\",\"est\",\"ssd\",\"ken\",\"tuv\",\"syc\",\"bwa\",\"hkg\",\"vir\",\"irq\",\"btn\"]"
104              
105             Often a schema has coercion rule or default value rules, so after validation the
106             validated value will be different from the original. To return the validated
107             (set-as-default, coerced, prefiltered) value:
108              
109             my $validator = gen_validator("country::code", {return_type=>'str_errmsg+val'});
110             my $res = $validator->($data); # [$errmsg, $validated_val]
111            
112             # a sample valid data
113             $data = "IDN";
114             my $res = $validator->($data); # => ["","idn"]
115            
116             # a sample invalid data
117             $data = "xx";
118             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\",\"phl\",\"esp\",\"tkm\",\"ago\",\"jor\",\"cyp\",\"cuw\",\"blm\",\"can\",\"aia\",\"hun\",\"atg\",\"vat\",\"esh\",\"glp\",\"mli\",\"mda\",\"ind\",\"eth\",\"png\",\"asm\",\"bfa\",\"guy\",\"deu\",\"ukr\",\"jpn\",\"prt\",\"cck\",\"lao\",\"tkl\",\"blr\",\"afg\",\"are\",\"gab\",\"swz\",\"vnm\",\"idn\",\"tcd\",\"bgd\",\"srb\",\"ala\",\"mwi\",\"rus\",\"bes\",\"tgo\",\"che\",\"zwe\",\"cok\",\"mne\",\"mar\",\"guf\",\"grl\",\"sjm\",\"mmr\",\"flk\",\"plw\",\"ben\",\"bmu\",\"gnq\",\"bol\",\"lca\",\"sgs\",\"zaf\",\"omn\",\"shn\",\"gtm\",\"nam\",\"lso\",\"iot\",\"svn\",\"gnb\",\"fro\",\"cmr\",\"bra\",\"grc\",\"bel\",\"niu\",\"gmb\",\"eri\",\"pyf\",\"arg\",\"abw\",\"nld\",\"hmd\",\"vgb\",\"dza\",\"hti\",\"hnd\",\"mac\",\"msr\",\"mtq\",\"tto\",\"tca\",\"chn\",\"mhl\",\"egy\",\"sxm\",\"pse\",\"qat\",\"fsm\",\"lby\",\"tza\",\"chl\",\"ita\",\"mrt\",\"moz\",\"mng\",\"cod\",\"pan\",\"khm\",\"smr\",\"arm\",\"cxr\",\"aut\",\"prk\",\"uga\",\"gum\",\"kgz\",\"ncl\",\"kaz\",\"bvt\",\"gbr\",\"lie\",\"nga\",\"ton\",\"ner\",\"mco\",\"ggy\",\"sgp\",\"bgr\",\"rwa\",\"brn\",\"isr\",\"civ\",\"ltu\",\"imn\",\"slv\",\"bih\",\"fji\",\"vut\",\"svk\",\"cri\",\"blz\",\"gin\",\"nzl\",\"wlf\",\"cze\",\"dom\",\"umi\",\"npl\",\"nor\",\"sur\",\"pol\",\"gib\",\"yem\",\"isl\",\"syr\",\"jey\",\"kna\",\"tjk\",\"fin\",\"reu\",\"bhs\",\"brb\",\"cym\",\"cub\",\"maf\",\"pcn\",\"grd\",\"atf\",\"hrv\",\"gha\",\"uzb\",\"rou\",\"nic\",\"aus\",\"zmb\",\"caf\",\"kir\",\"slb\",\"tls\",\"sau\",\"and\",\"per\",\"pak\",\"ecu\",\"geo\",\"ven\",\"tur\",\"bdi\",\"mdv\",\"sen\",\"mdg\",\"lbr\",\"mnp\",\"kor\",\"irl\",\"fra\",\"dma\",\"sle\",\"dnk\",\"irn\",\"jam\",\"wsm\",\"stp\",\"aze\",\"dji\",\"som\",\"pry\",\"mys\",\"cpv\",\"ata\",\"tha\",\"vct\",\"kwt\",\"mex\",\"alb\",\"usa\",\"ury\",\"nru\",\"sdn\",\"spm\",\"pri\",\"mus\",\"lva\",\"bhr\",\"swe\",\"lux\",\"tun\",\"twn\",\"col\",\"lka\",\"mlt\",\"lbn\",\"myt\",\"cog\",\"mkd\",\"com\",\"nfk\",\"est\",\"ssd\",\"ken\",\"tuv\",\"syc\",\"bwa\",\"hkg\",\"vir\",\"irq\",\"btn\"]","xx"]
119              
120             Data::Sah can also create validator that returns a hash of detailed error
121             message. Data::Sah can even create validator that targets other language, like
122             JavaScript, from the same schema. Other things Data::Sah can do: show source
123             code for validator, generate a validator code with debug comments and/or log
124             statements, generate human text from schema. See its documentation for more
125             details.
126              
127             =head2 Using with Params::Sah
128              
129             To validate function parameters against this schema (requires L<Params::Sah>):
130              
131             use Params::Sah qw(gen_validator);
132              
133             sub myfunc {
134             my @args = @_;
135             state $validator = gen_validator("country::code*");
136             $validator->(\@args);
137             ...
138             }
139              
140             =head2 Using with Perinci::CmdLine::Lite
141              
142             To specify schema in L<Rinci> function metadata and use the metadata with
143             L<Perinci::CmdLine> (L<Perinci::CmdLine::Lite>) to create a CLI:
144              
145             # in lib/MyApp.pm
146             package
147             MyApp;
148             our %SPEC;
149             $SPEC{myfunc} = {
150             v => 1.1,
151             summary => 'Routine to do blah ...',
152             args => {
153             arg1 => {
154             summary => 'The blah blah argument',
155             schema => ['country::code*'],
156             },
157             ...
158             },
159             };
160             sub myfunc {
161             my %args = @_;
162             ...
163             }
164             1;
165              
166             # in myapp.pl
167             package
168             main;
169             use Perinci::CmdLine::Any;
170             Perinci::CmdLine::Any->new(url=>'/MyApp/myfunc')->run;
171              
172             # in command-line
173             % ./myapp.pl --help
174             myapp - Routine to do blah ...
175             ...
176              
177             % ./myapp.pl --version
178              
179             % ./myapp.pl --arg1 ...
180              
181             =head2 Using on the CLI with validate-with-sah
182              
183             To validate some data on the CLI, you can use L<validate-with-sah> utility.
184             Specify the schema as the first argument (encoded in Perl syntax) and the data
185             to validate as the second argument (encoded in Perl syntax):
186              
187             % validate-with-sah '"country::code*"' '"data..."'
188              
189             C<validate-with-sah> has several options for, e.g. validating multiple data,
190             showing the generated validator code (Perl/JavaScript/etc), or loading
191             schema/data from file. See its manpage for more details.
192              
193              
194             =head2 Using with Type::Tiny
195              
196             To create a type constraint and type library from a schema (requires
197             L<Type::Tiny> as well as L<Type::FromSah>):
198              
199             package My::Types {
200             use Type::Library -base;
201             use Type::FromSah qw( sah2type );
202              
203             __PACKAGE__->add_type(
204             sah2type('country::code*', name=>'CountryCode')
205             );
206             }
207              
208             use My::Types qw(CountryCode);
209             CountryCode->assert_valid($data);
210              
211             =head1 DESCRIPTION
212              
213             Accept only current (not retired) codes. Alpha-2 or alpha-3 codes are accepted.
214              
215             Code will be converted to lowercase.
216              
217             =head1 HOMEPAGE
218              
219             Please visit the project's homepage at L<https://metacpan.org/release/Sah-Schemas-Country>.
220              
221             =head1 SOURCE
222              
223             Source repository is at L<https://github.com/perlancar/perl-Sah-Schemas-Country>.
224              
225             =head1 SEE ALSO
226              
227             L<Sah::Schema::country::code::alpha2>
228              
229             L<Sah::Schema::country::code::alpha3>
230              
231             =head1 AUTHOR
232              
233             perlancar <perlancar@cpan.org>
234              
235             =head1 CONTRIBUTING
236              
237              
238             To contribute, you can send patches by email/via RT, or send pull requests on
239             GitHub.
240              
241             Most of the time, you don't need to build the distribution yourself. You can
242             simply modify the code, then test via:
243              
244             % prove -l
245              
246             If you want to build the distribution (e.g. to try to install it locally on your
247             system), you can install L<Dist::Zilla>,
248             L<Dist::Zilla::PluginBundle::Author::PERLANCAR>,
249             L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
250             Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond
251             that are considered a bug and can be reported to me.
252              
253             =head1 COPYRIGHT AND LICENSE
254              
255             This software is copyright (c) 2023, 2020, 2019, 2018 by perlancar <perlancar@cpan.org>.
256              
257             This is free software; you can redistribute it and/or modify it under
258             the same terms as the Perl 5 programming language system itself.
259              
260             =head1 BUGS
261              
262             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Sah-Schemas-Country>
263              
264             When submitting a bug or request, please include a test-file or a
265             patch to an existing test-file that illustrates the bug or desired
266             feature.
267              
268             =cut