File Coverage

blib/lib/Sah/Schema/language/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::language::code::alpha2;
2              
3 1     1   366434 use strict;
  1         2  
  1         50  
4 1     1   6407 use Locale::Codes::Language_Codes ();
  1         367822  
  1         3965  
5              
6             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
7             our $DATE = '2023-08-07'; # DATE
8             our $DIST = 'Sah-Schemas-Language'; # DIST
9             our $VERSION = '0.005'; # VERSION
10              
11             my $codes = [];
12             my $names = [];
13             {
14             for my $alpha2 (keys(%{ $Locale::Codes::Data{'language'}{'code2id'}{'alpha-2'} })) {
15             push @$codes, $alpha2;
16             my $id = $Locale::Codes::Data{'language'}{'code2id'}{'alpha-2'}{$alpha2}[0];
17             push @$names, $Locale::Codes::Data{'language'}{'id2names'}{$id}[0];
18             }
19              
20             die "Can't extract country codes from Locale::Codes::Language_Codes"
21             unless @$codes;
22             }
23              
24             our $schema = [str => {
25             summary => 'Language code (alpha-2)',
26             description => <<'_',
27              
28             Accept only current (not retired) codes. Only alpha-2 codes are accepted.
29              
30             _
31             match => '\A[a-z]{2}\z',
32             in => $codes,
33             'x.in.summaries' => $names,
34             'x.perl.coerce_rules' => ['From_str::to_lower'],
35              
36             examples => [
37             {value=>"", valid=>0},
38             {value=>"ID", valid=>1, validated_value=>"id", summary=>"Indonesian (2 letter)"},
39             {value=>"IND", valid=>0, summary=>"Indonesian (3 letter, rejected)"},
40             {value=>"qq", valid=>0, summary=>"Unknown language code"},
41             ],
42              
43             }, {}];
44              
45             1;
46             # ABSTRACT: Language code (alpha-2)
47              
48             __END__
49              
50             =pod
51              
52             =encoding UTF-8
53              
54             =head1 NAME
55              
56             Sah::Schema::language::code::alpha2 - Language code (alpha-2)
57              
58             =head1 VERSION
59              
60             This document describes version 0.005 of Sah::Schema::language::code::alpha2 (from Perl distribution Sah-Schemas-Language), released on 2023-08-07.
61              
62             =head1 SYNOPSIS
63              
64             =head2 Sample data and validation results against this schema
65              
66             "" # INVALID
67              
68             "ID" # valid (Indonesian (2 letter)), becomes "id"
69              
70             "IND" # INVALID (Indonesian (3 letter, rejected))
71              
72             "qq" # INVALID (Unknown language code)
73              
74             =head2 Using with Data::Sah
75              
76             To check data against this schema (requires L<Data::Sah>):
77              
78             use Data::Sah qw(gen_validator);
79             my $validator = gen_validator("language::code::alpha2*");
80             say $validator->($data) ? "valid" : "INVALID!";
81              
82             The above validator returns a boolean result (true if data is valid, false if
83             otherwise). To return an error message string instead (empty string if data is
84             valid, a non-empty error message otherwise):
85              
86             my $validator = gen_validator("language::code::alpha2", {return_type=>'str_errmsg'});
87             my $errmsg = $validator->($data);
88            
89             # a sample valid data
90             $data = "ID";
91             my $errmsg = $validator->($data); # => ""
92            
93             # a sample invalid data
94             $data = "IND";
95             my $errmsg = $validator->($data); # => "Must be one of [\"km\",\"nn\",\"vo\",\"fa\",\"rm\",\"en\",\"aa\",\"oc\",\"mr\",\"ab\",\"tn\",\"lu\",\"oj\",\"cy\",\"sv\",\"lt\",\"sl\",\"tr\",\"lb\",\"la\",\"jv\",\"ps\",\"nr\",\"pi\",\"bm\",\"dv\",\"mn\",\"or\",\"af\",\"ny\",\"ff\",\"ht\",\"ie\",\"tk\",\"ty\",\"ha\",\"cr\",\"su\",\"av\",\"lv\",\"ja\",\"st\",\"id\",\"bh\",\"da\",\"be\",\"my\",\"vi\",\"sa\",\"mk\",\"gd\",\"hu\",\"qu\",\"pl\",\"nd\",\"sw\",\"mh\",\"fo\",\"si\",\"zh\",\"ss\",\"ee\",\"sg\",\"ne\",\"lo\",\"kk\",\"ky\",\"ik\",\"th\",\"te\",\"hi\",\"bn\",\"ch\",\"ce\",\"pt\",\"ho\",\"kr\",\"kj\",\"li\",\"pa\",\"gn\",\"lg\",\"as\",\"so\",\"fi\",\"rn\",\"om\",\"br\",\"ug\",\"kn\",\"ms\",\"ku\",\"ng\",\"iu\",\"mi\",\"se\",\"sh\",\"ru\",\"de\",\"ba\",\"tg\",\"gu\",\"ts\",\"sq\",\"co\",\"ti\",\"am\",\"ia\",\"he\",\"ka\",\"tw\",\"it\",\"os\",\"es\",\"ga\",\"sd\",\"mg\",\"eo\",\"no\",\"to\",\"wa\",\"cs\",\"sm\",\"gv\",\"gl\",\"ae\",\"kl\",\"kv\",\"nv\",\"el\",\"hz\",\"bo\",\"nl\",\"sr\",\"ay\",\"cu\",\"tl\",\"ak\",\"fy\",\"yo\",\"xh\",\"sc\",\"sn\",\"dz\",\"ko\",\"io\",\"ml\",\"hr\",\"ca\",\"ro\",\"uk\",\"bg\",\"ks\",\"ii\",\"ur\",\"ki\",\"rw\",\"is\",\"et\",\"zu\",\"hy\",\"wo\",\"nb\",\"ta\",\"kw\",\"tt\",\"az\",\"na\",\"an\",\"ve\",\"ln\",\"fr\",\"za\",\"ar\",\"yi\",\"sk\",\"cv\",\"eu\",\"mt\",\"kg\",\"bs\",\"fj\",\"bi\",\"ig\",\"uz\"]"
96              
97             Often a schema has coercion rule or default value rules, so after validation the
98             validated value will be different from the original. To return the validated
99             (set-as-default, coerced, prefiltered) value:
100              
101             my $validator = gen_validator("language::code::alpha2", {return_type=>'str_errmsg+val'});
102             my $res = $validator->($data); # [$errmsg, $validated_val]
103            
104             # a sample valid data
105             $data = "ID";
106             my $res = $validator->($data); # => ["","id"]
107            
108             # a sample invalid data
109             $data = "IND";
110             my $res = $validator->($data); # => ["Must be one of [\"km\",\"nn\",\"vo\",\"fa\",\"rm\",\"en\",\"aa\",\"oc\",\"mr\",\"ab\",\"tn\",\"lu\",\"oj\",\"cy\",\"sv\",\"lt\",\"sl\",\"tr\",\"lb\",\"la\",\"jv\",\"ps\",\"nr\",\"pi\",\"bm\",\"dv\",\"mn\",\"or\",\"af\",\"ny\",\"ff\",\"ht\",\"ie\",\"tk\",\"ty\",\"ha\",\"cr\",\"su\",\"av\",\"lv\",\"ja\",\"st\",\"id\",\"bh\",\"da\",\"be\",\"my\",\"vi\",\"sa\",\"mk\",\"gd\",\"hu\",\"qu\",\"pl\",\"nd\",\"sw\",\"mh\",\"fo\",\"si\",\"zh\",\"ss\",\"ee\",\"sg\",\"ne\",\"lo\",\"kk\",\"ky\",\"ik\",\"th\",\"te\",\"hi\",\"bn\",\"ch\",\"ce\",\"pt\",\"ho\",\"kr\",\"kj\",\"li\",\"pa\",\"gn\",\"lg\",\"as\",\"so\",\"fi\",\"rn\",\"om\",\"br\",\"ug\",\"kn\",\"ms\",\"ku\",\"ng\",\"iu\",\"mi\",\"se\",\"sh\",\"ru\",\"de\",\"ba\",\"tg\",\"gu\",\"ts\",\"sq\",\"co\",\"ti\",\"am\",\"ia\",\"he\",\"ka\",\"tw\",\"it\",\"os\",\"es\",\"ga\",\"sd\",\"mg\",\"eo\",\"no\",\"to\",\"wa\",\"cs\",\"sm\",\"gv\",\"gl\",\"ae\",\"kl\",\"kv\",\"nv\",\"el\",\"hz\",\"bo\",\"nl\",\"sr\",\"ay\",\"cu\",\"tl\",\"ak\",\"fy\",\"yo\",\"xh\",\"sc\",\"sn\",\"dz\",\"ko\",\"io\",\"ml\",\"hr\",\"ca\",\"ro\",\"uk\",\"bg\",\"ks\",\"ii\",\"ur\",\"ki\",\"rw\",\"is\",\"et\",\"zu\",\"hy\",\"wo\",\"nb\",\"ta\",\"kw\",\"tt\",\"az\",\"na\",\"an\",\"ve\",\"ln\",\"fr\",\"za\",\"ar\",\"yi\",\"sk\",\"cv\",\"eu\",\"mt\",\"kg\",\"bs\",\"fj\",\"bi\",\"ig\",\"uz\"]","ind"]
111              
112             Data::Sah can also create validator that returns a hash of detailed error
113             message. Data::Sah can even create validator that targets other language, like
114             JavaScript, from the same schema. Other things Data::Sah can do: show source
115             code for validator, generate a validator code with debug comments and/or log
116             statements, generate human text from schema. See its documentation for more
117             details.
118              
119             =head2 Using with Params::Sah
120              
121             To validate function parameters against this schema (requires L<Params::Sah>):
122              
123             use Params::Sah qw(gen_validator);
124              
125             sub myfunc {
126             my @args = @_;
127             state $validator = gen_validator("language::code::alpha2*");
128             $validator->(\@args);
129             ...
130             }
131              
132             =head2 Using with Perinci::CmdLine::Lite
133              
134             To specify schema in L<Rinci> function metadata and use the metadata with
135             L<Perinci::CmdLine> (L<Perinci::CmdLine::Lite>) to create a CLI:
136              
137             # in lib/MyApp.pm
138             package
139             MyApp;
140             our %SPEC;
141             $SPEC{myfunc} = {
142             v => 1.1,
143             summary => 'Routine to do blah ...',
144             args => {
145             arg1 => {
146             summary => 'The blah blah argument',
147             schema => ['language::code::alpha2*'],
148             },
149             ...
150             },
151             };
152             sub myfunc {
153             my %args = @_;
154             ...
155             }
156             1;
157              
158             # in myapp.pl
159             package
160             main;
161             use Perinci::CmdLine::Any;
162             Perinci::CmdLine::Any->new(url=>'/MyApp/myfunc')->run;
163              
164             # in command-line
165             % ./myapp.pl --help
166             myapp - Routine to do blah ...
167             ...
168              
169             % ./myapp.pl --version
170              
171             % ./myapp.pl --arg1 ...
172              
173             =head2 Using on the CLI with validate-with-sah
174              
175             To validate some data on the CLI, you can use L<validate-with-sah> utility.
176             Specify the schema as the first argument (encoded in Perl syntax) and the data
177             to validate as the second argument (encoded in Perl syntax):
178              
179             % validate-with-sah '"language::code::alpha2*"' '"data..."'
180              
181             C<validate-with-sah> has several options for, e.g. validating multiple data,
182             showing the generated validator code (Perl/JavaScript/etc), or loading
183             schema/data from file. See its manpage for more details.
184              
185              
186             =head2 Using with Type::Tiny
187              
188             To create a type constraint and type library from a schema (requires
189             L<Type::Tiny> as well as L<Type::FromSah>):
190              
191             package My::Types {
192             use Type::Library -base;
193             use Type::FromSah qw( sah2type );
194              
195             __PACKAGE__->add_type(
196             sah2type('language::code::alpha2*', name=>'LanguageCodeAlpha2')
197             );
198             }
199              
200             use My::Types qw(LanguageCodeAlpha2);
201             LanguageCodeAlpha2->assert_valid($data);
202              
203             =head1 DESCRIPTION
204              
205             Accept only current (not retired) codes. Only alpha-2 codes are accepted.
206              
207             =head1 HOMEPAGE
208              
209             Please visit the project's homepage at L<https://metacpan.org/release/Sah-Schemas-Language>.
210              
211             =head1 SOURCE
212              
213             Source repository is at L<https://github.com/perlancar/perl-Sah-Schemas-Language>.
214              
215             =head1 SEE ALSO
216              
217             L<Sah::Schema::language::code::alpha3>
218              
219             L<Sah::Schema::language::code>
220              
221             =head1 AUTHOR
222              
223             perlancar <perlancar@cpan.org>
224              
225             =head1 CONTRIBUTING
226              
227              
228             To contribute, you can send patches by email/via RT, or send pull requests on
229             GitHub.
230              
231             Most of the time, you don't need to build the distribution yourself. You can
232             simply modify the code, then test via:
233              
234             % prove -l
235              
236             If you want to build the distribution (e.g. to try to install it locally on your
237             system), you can install L<Dist::Zilla>,
238             L<Dist::Zilla::PluginBundle::Author::PERLANCAR>,
239             L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
240             Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond
241             that are considered a bug and can be reported to me.
242              
243             =head1 COPYRIGHT AND LICENSE
244              
245             This software is copyright (c) 2023, 2020, 2019, 2018 by perlancar <perlancar@cpan.org>.
246              
247             This is free software; you can redistribute it and/or modify it under
248             the same terms as the Perl 5 programming language system itself.
249              
250             =head1 BUGS
251              
252             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Sah-Schemas-Language>
253              
254             When submitting a bug or request, please include a test-file or a
255             patch to an existing test-file that illustrates the bug or desired
256             feature.
257              
258             =cut