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