| 1 | package Sah::Schema::country::code::alpha2; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||
| 4 | 1 | our $DATE = '2020-03-08'; # DATE | |||||||||
| 5 | 1 | our $DIST = 'Sah-Schemas-Country'; # DIST | |||||||||
| 6 | 1 | our $VERSION = '0.008'; # VERSION | |||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use Locale::Codes::Country_Codes (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | my $codes = [sort ( | ||||||||||
| 11 | 1 | keys(%{ $Locale::Codes::Data{'country'}{'code2id'}{'alpha-2'} }), | |||||||||
| 12 | )]; | ||||||||||
| 13 | 1 | ▶ | die "Can't extract country codes from Locale::Codes::Language_Codes" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 14 | unless @$codes; | ||||||||||
| 15 | |||||||||||
| 16 | 1 | our $schema = [str => { | |||||||||
| 17 | summary => 'Country code (alpha-2)', | ||||||||||
| 18 | description => <<'_', | ||||||||||
| 19 | |||||||||||
| 20 | Accept only current (not retired) codes. Only alpha-2 codes are accepted. | ||||||||||
| 21 | |||||||||||
| 22 | Code will be converted to lowercase. | ||||||||||
| 23 | |||||||||||
| 24 | _ | ||||||||||
| 25 | match => '\A[a-z]{2}\z', | ||||||||||
| 26 | in => $codes, | ||||||||||
| 27 | 'x.perl.coerce_rules' => ['From_str::to_lower'], | ||||||||||
| 28 | examples => [ | ||||||||||
| 29 | {value=>'', valid=>0}, | ||||||||||
| 30 | {value=>'ID' , valid=>1, validated_value=>'id'}, | ||||||||||
| 31 | {value=>'IDN', valid=>0, summary=>'Only alpha-2 codes are allowed'}, | ||||||||||
| 32 | {value=>'xx', valid=>0}, | ||||||||||
| 33 | {value=>'xxx', valid=>0}, | ||||||||||
| 34 | ], | ||||||||||
| 35 | }, {}]; | ||||||||||
| 36 | |||||||||||
| 37 | 1 | 1; | |||||||||
| 38 | # ABSTRACT: Country code (alpha-2) | ||||||||||
| 39 | |||||||||||
| 40 | __END__ | ||||||||||
| 41 | |||||||||||
| 42 | =pod | ||||||||||
| 43 | |||||||||||
| 44 | =encoding UTF-8 | ||||||||||
| 45 | |||||||||||
| 46 | =head1 NAME | ||||||||||
| 47 | |||||||||||
| 48 | Sah::Schema::country::code::alpha2 - Country code (alpha-2) | ||||||||||
| 49 | |||||||||||
| 50 | =head1 VERSION | ||||||||||
| 51 | |||||||||||
| 52 | This document describes version 0.008 of Sah::Schema::country::code::alpha2 (from Perl distribution Sah-Schemas-Country), released on 2020-03-08. | ||||||||||
| 53 | |||||||||||
| 54 | =head1 SYNOPSIS | ||||||||||
| 55 | |||||||||||
| 56 | Using with L<Data::Sah>: | ||||||||||
| 57 | |||||||||||
| 58 | use Data::Sah qw(gen_validator); | ||||||||||
| 59 | my $vdr = gen_validator("country::code::alpha2*"); | ||||||||||
| 60 | say $vdr->($data) ? "valid" : "INVALID!"; | ||||||||||
| 61 | |||||||||||
| 62 | # Data::Sah can also create a validator to return error message, coerced value, | ||||||||||
| 63 | # even validators in other languages like JavaScript, from the same schema. | ||||||||||
| 64 | # See its documentation for more details. | ||||||||||
| 65 | |||||||||||
| 66 | Using in L<Rinci> function metadata (to be used with L<Perinci::CmdLine>, etc): | ||||||||||
| 67 | |||||||||||
| 68 | package MyApp; | ||||||||||
| 69 | our %SPEC; | ||||||||||
| 70 | $SPEC{myfunc} = { | ||||||||||
| 71 | v => 1.1, | ||||||||||
| 72 | summary => 'Routine to do blah ...', | ||||||||||
| 73 | args => { | ||||||||||
| 74 | arg1 => { | ||||||||||
| 75 | summary => 'The blah blah argument', | ||||||||||
| 76 | schema => ['country::code::alpha2*'], | ||||||||||
| 77 | }, | ||||||||||
| 78 | ... | ||||||||||
| 79 | }, | ||||||||||
| 80 | }; | ||||||||||
| 81 | sub myfunc { | ||||||||||
| 82 | my %args = @_; | ||||||||||
| 83 | ... | ||||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | Sample data: | ||||||||||
| 87 | |||||||||||
| 88 | "" # INVALID | ||||||||||
| 89 | |||||||||||
| 90 | "ID" # valid, becomes "id" | ||||||||||
| 91 | |||||||||||
| 92 | "IDN" # INVALID (Only alpha-2 codes are allowed) | ||||||||||
| 93 | |||||||||||
| 94 | "xx" # INVALID | ||||||||||
| 95 | |||||||||||
| 96 | "xxx" # INVALID | ||||||||||
| 97 | |||||||||||
| 98 | =head1 DESCRIPTION | ||||||||||
| 99 | |||||||||||
| 100 | Accept only current (not retired) codes. Only alpha-2 codes are accepted. | ||||||||||
| 101 | |||||||||||
| 102 | Code will be converted to lowercase. | ||||||||||
| 103 | |||||||||||
| 104 | =head1 HOMEPAGE | ||||||||||
| 105 | |||||||||||
| 106 | Please visit the project's homepage at L<https://metacpan.org/release/Sah-Schemas-Country>. | ||||||||||
| 107 | |||||||||||
| 108 | =head1 SOURCE | ||||||||||
| 109 | |||||||||||
| 110 | Source repository is at L<https://github.com/perlancar/perl-Sah-Schemas-Country>. | ||||||||||
| 111 | |||||||||||
| 112 | =head1 BUGS | ||||||||||
| 113 | |||||||||||
| 114 | Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Sah-Schemas-Country> | ||||||||||
| 115 | |||||||||||
| 116 | When submitting a bug or request, please include a test-file or a | ||||||||||
| 117 | patch to an existing test-file that illustrates the bug or desired | ||||||||||
| 118 | feature. | ||||||||||
| 119 | |||||||||||
| 120 | =head1 SEE ALSO | ||||||||||
| 121 | |||||||||||
| 122 | L<Sah::Schema::country::code::alpha3> | ||||||||||
| 123 | |||||||||||
| 124 | L<Sah::Schema::country::code> | ||||||||||
| 125 | |||||||||||
| 126 | =head1 AUTHOR | ||||||||||
| 127 | |||||||||||
| 128 | perlancar <perlancar@cpan.org> | ||||||||||
| 129 | |||||||||||
| 130 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 131 | |||||||||||
| 132 | This software is copyright (c) 2020, 2019, 2018 by perlancar@cpan.org. | ||||||||||
| 133 | |||||||||||
| 134 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 135 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 136 | |||||||||||
| 137 | =cut | ||||||||||