| 1 | package App::FileSync::Args; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | # One place that turns a routine's @_ into a validated hashref, so every public | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # entry point reads its arguments the same way and reports a bad one with a | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # catalogued message instead of the validator's own text. | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 439 | ▶ | use App::FileSync::Platform; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 439 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 439 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 439 | ▶ | use autodie qw(:all); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 439 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 439 | ▶ | use Params::Get qw(get_params); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 439 | ▶ | use Params::Validate::Strict qw(validate_strict); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 439 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 439 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 439 | ▶ | use Sub::Private { mode => 'enforce' }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 439 | ▶ | use App::FileSync::I18N qw(message throw strip_location); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 439 | ▶ | use version; our $VERSION = version->declare('0.002.2'); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 439 | our @EXPORT_OK = qw(named positional with_options); | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | # The schema types whose min and max bound a value; true for whole numbers. | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 439 | Readonly my %NUMERIC => (integer => 1, number => 0); | |||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | # The schema types whose min and max bound a size, and how to measure it. | ||||||||||||||||||||||||||||||||||||||||||
| 30 | # A value of the wrong kind has no size: the validator's own text explains it. | ||||||||||||||||||||||||||||||||||||||||||
| 31 | Readonly my %SIZE_OF => ( | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 12 | ▶ | string => sub { ref $_[0] ? undef : length $_[0] }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 5 | ▶ | arrayref => sub { ref $_[0] eq 'ARRAY' ? scalar @{ $_[0] } : undef }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | hashref => sub { ref $_[0] eq 'HASH' ? scalar keys %{ $_[0] } : undef }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 439 | ); | |||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | # A whole number as the validator accepts one. | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 439 | Readonly my $INTEGER => qr/\A[-+]?\d+\z/; | |||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | # Parameter name => the command-line option it came from, while | ||||||||||||||||||||||||||||||||||||||||||
| 41 | # with_options() is running. Not part of the API: set it through | ||||||||||||||||||||||||||||||||||||||||||
| 42 | # with_options, which restores it. | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 439 | our %OPTION; | |||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | # Errors should point at the caller of the routine whose arguments were bad. | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 439 | $Carp::Internal{ (__PACKAGE__) }++; | |||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | App::FileSync::Args - validated arguments for App::FileSync's public routines | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | use App::FileSync::Args qw(named positional); | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | sub repo_create { | ||||||||||||||||||||||||||||||||||||||||||
| 59 | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 60 | my $args = named(\@_, { | ||||||||||||||||||||||||||||||||||||||||||
| 61 | name => { type => 'string', min => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 62 | trash_days => { type => 'integer', min => 0, optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 63 | }, 'repo_create'); | ||||||||||||||||||||||||||||||||||||||||||
| 64 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | sub load_repo { | ||||||||||||||||||||||||||||||||||||||||||
| 68 | my $args = positional(\@_, [qw(root repo)], { | ||||||||||||||||||||||||||||||||||||||||||
| 69 | root => { type => 'string', min => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 70 | repo => { type => 'string', min => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 71 | }, 'load_repo'); | ||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | =head2 named | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | Purpose: validate C<key =E<gt> value> arguments (or a single hashref). | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | Args: C<\@args>, C<\%schema> (L<Params::Validate::Strict> format), C<$routine> | ||||||||||||||||||||||||||||||||||||||||||
| 81 | (the name used in an error). | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | Returns: a hashref of validated (and coerced) arguments. An empty argument | ||||||||||||||||||||||||||||||||||||||||||
| 84 | list is an empty hash. | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | Side Effects: raises a catalogued C<args.*> message naming the parameter | ||||||||||||||||||||||||||||||||||||||||||
| 87 | (with the routine, or as the command-line option given to L</with_options>) | ||||||||||||||||||||||||||||||||||||||||||
| 88 | and the problem. | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | Usage: | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | my $args = named(\@_, $SCHEMA, 'verify'); | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | my $args = named([ deep => 1 ], { deep => { type => 'boolean', optional => 1 } }, 'verify'); | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | =head4 INPUT | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | [ | ||||||||||||||||||||||||||||||||||||||||||
| 103 | { name => 'args', type => 'arrayref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 104 | { name => 'schema', type => 'hashref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 105 | { name => 'routine', type => 'string', min => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 106 | ] | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | =head4 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | { type => 'hashref' } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | Key Meaning Resolution | ||||||||||||||||||||||||||||||||||||||||||
| 115 | args.unknown an argument the schema does not name remove it | ||||||||||||||||||||||||||||||||||||||||||
| 116 | args.missing a required argument is absent supply it | ||||||||||||||||||||||||||||||||||||||||||
| 117 | args.not_number a number argument is not numeric pass a number | ||||||||||||||||||||||||||||||||||||||||||
| 118 | args.not_integer an integer argument is not whole pass a whole number | ||||||||||||||||||||||||||||||||||||||||||
| 119 | args.too_small a number below the schema's min raise it | ||||||||||||||||||||||||||||||||||||||||||
| 120 | args.too_large a number above the schema's max lower it | ||||||||||||||||||||||||||||||||||||||||||
| 121 | args.too_short a string shorter than min characters lengthen it | ||||||||||||||||||||||||||||||||||||||||||
| 122 | args.too_long a string longer than max characters shorten it | ||||||||||||||||||||||||||||||||||||||||||
| 123 | args.too_few a list or hash with fewer than min add entries | ||||||||||||||||||||||||||||||||||||||||||
| 124 | args.too_many a list or hash with more than max remove entries | ||||||||||||||||||||||||||||||||||||||||||
| 125 | args.not_one_of a value outside memberof use a listed value | ||||||||||||||||||||||||||||||||||||||||||
| 126 | args.bad_form a string that fails matches fix its form | ||||||||||||||||||||||||||||||||||||||||||
| 127 | args.invalid any other failure, in the validator's fix the call | ||||||||||||||||||||||||||||||||||||||||||
| 128 | own words | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | =head3 FORMAL SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | named : seq VALUE × SCHEMA × NAME ↛ (KEY ⇸ VALUE) | ||||||||||||||||||||||||||||||||||||||||||
| 133 | pre pairs(args) satisfies schema | ||||||||||||||||||||||||||||||||||||||||||
| 134 | post result = coerce(pairs(args)) | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 8600 | ▶ | sub named { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
named: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 8600 | my ($args, $schema, $routine) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 8600 | ▶ | my $input = @{$args} ? get_params(undef, $args) : {}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 8600 | return _validate($input, $schema, $routine); | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | =head2 positional | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | Purpose: validate positional arguments by naming them. | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | Args: C<\@args>, C<\@names> (one per position), C<\%schema>, C<$routine>. | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | Returns: a hashref keyed by C<@names>; missing trailing arguments are | ||||||||||||||||||||||||||||||||||||||||||
| 152 | C<undef> and must be C<optional> in the schema. | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | Side Effects: raises C<args.invalid>. | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | Usage: | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | my $args = positional(\@_, [qw(root repo)], $SCHEMA, 'load_repo'); | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | my $args = positional([ '/srv', 'docs' ], [qw(root repo)], | ||||||||||||||||||||||||||||||||||||||||||
| 163 | { root => { type => 'string' }, repo => { type => 'string' } }, 'load_repo'); | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | =head4 INPUT | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | [ | ||||||||||||||||||||||||||||||||||||||||||
| 170 | { name => 'args', type => 'arrayref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 171 | { name => 'names', type => 'arrayref', min => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 172 | { name => 'schema', type => 'hashref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 173 | { name => 'routine', type => 'string', min => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 174 | ] | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | =head4 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | { type => 'hashref' } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | Key Meaning Resolution | ||||||||||||||||||||||||||||||||||||||||||
| 183 | args.invalid an argument is missing or invalid fix the call | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | =head3 FORMAL SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | positional(a, n, s, r) = named(zip(n, a), s, r) | ||||||||||||||||||||||||||||||||||||||||||
| 188 | pre #a ≤ #n | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | 1358 | ▶ | sub positional { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
positional: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 1358 | my ($args, $names, $schema, $routine) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | # Map by position directly: get_params treats a lone hashref argument as | ||||||||||||||||||||||||||||||||||||||||||
| 196 | # named pairs, which is wrong for a positional routine that takes one. | ||||||||||||||||||||||||||||||||||||||||||
| 197 | 1358 | my %input; | |||||||||||||||||||||||||||||||||||||||||
| 198 | 1358 | @input{ @{$names} } = @{$args}; | |||||||||||||||||||||||||||||||||||||||||
| 199 | 1358 | delete @input{ grep { !defined $input{$_} } keys %input }; | |||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | 1358 | return _validate(\%input, $schema, $routine); | |||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | =head2 with_options | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | Purpose: run code with argument errors naming command-line options. | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | Args: C<\%options> (parameter name =E<gt> option, such as | ||||||||||||||||||||||||||||||||||||||||||
| 209 | C<max_delete_percent =E<gt> '--max-delete-percent'>), C<$code>. | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | Returns: whatever C<$code> returns. | ||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | Side Effects: while C<$code> runs, an invalid argument with one of these | ||||||||||||||||||||||||||||||||||||||||||
| 214 | names is reported by its option (C<--max-delete-percent must be no more | ||||||||||||||||||||||||||||||||||||||||||
| 215 | than 100, not 150>) instead of by the routine and parameter | ||||||||||||||||||||||||||||||||||||||||||
| 216 | (C<Admin::repo_create: 'max_delete_percent' must be ...>). The mapping is | ||||||||||||||||||||||||||||||||||||||||||
| 217 | restored when C<$code> returns or dies. | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | Usage: | ||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | my $made = with_options({ trash_days => '--trash-days' }, sub { | ||||||||||||||||||||||||||||||||||||||||||
| 222 | $admin->repo_create(name => $name, trash_days => $days); | ||||||||||||||||||||||||||||||||||||||||||
| 223 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||
| 225 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | with_options({ role => '--role' }, sub { $admin->client_allow(%args) }); | ||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | =head4 INPUT | ||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | [ | ||||||||||||||||||||||||||||||||||||||||||
| 234 | { name => 'options', type => 'hashref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 235 | { name => 'code', type => 'coderef' }, | ||||||||||||||||||||||||||||||||||||||||||
| 236 | ] | ||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | =head4 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | { type => 'any' } | ||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | None of its own; see L</named>. | ||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | =head3 FORMAL SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | with_options(o, c) = c() where during c, label(p) = o(p) for p ∈ dom o | ||||||||||||||||||||||||||||||||||||||||||
| 249 | post OPTION' = OPTION | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | 4 | ▶ | sub with_options { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
with_options: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 254 | 4 | my ($options, $code) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | 4 | local %OPTION = (%OPTION, %{$options}); | |||||||||||||||||||||||||||||||||||||||||
| 257 | 4 | return $code->(); | |||||||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||
| 260 | # _validate | ||||||||||||||||||||||||||||||||||||||||||
| 261 | # Purpose: run Params::Validate::Strict and translate its failure. | ||||||||||||||||||||||||||||||||||||||||||
| 262 | # Entry: $input is a hashref; $schema a Params::Validate::Strict schema. | ||||||||||||||||||||||||||||||||||||||||||
| 263 | # Exit: returns the validated hashref, or raises a catalogued args.* | ||||||||||||||||||||||||||||||||||||||||||
| 264 | # message (args.invalid with the validator's text when the cause | ||||||||||||||||||||||||||||||||||||||||||
| 265 | # cannot be named). | ||||||||||||||||||||||||||||||||||||||||||
| 266 | # Side effects: none beyond the raise. | ||||||||||||||||||||||||||||||||||||||||||
| 267 | 9958 | ▶ | sub _validate :Private { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validate: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | 9958 | my ($input, $schema, $routine) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | 9958 | my $valid = eval { validate_strict(schema => $schema, input => $input) }; | |||||||||||||||||||||||||||||||||||||||||
| 271 | 9958 | ▶ | unless ($valid) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | 21 | my $error = $@; | |||||||||||||||||||||||||||||||||||||||||
| 273 | 21 | _explain($input, $schema, $routine); | |||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | # Drop the validator's own prefix and location; the routine name says | ||||||||||||||||||||||||||||||||||||||||||
| 276 | # where, and the rest says what. | ||||||||||||||||||||||||||||||||||||||||||
| 277 | 2 | my $reason = strip_location($error); | |||||||||||||||||||||||||||||||||||||||||
| 278 | 2 | $reason =~ s/\AParams::Validate::Strict line \d+: (?:validate_strict: )?//; | |||||||||||||||||||||||||||||||||||||||||
| 279 | 2 | $reason =~ s/ at \S+ line \d+\.?\z//; | |||||||||||||||||||||||||||||||||||||||||
| 280 | 2 | throw('args.invalid', { routine => $routine, reason => $reason }); | |||||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | 9937 | return $valid; | |||||||||||||||||||||||||||||||||||||||||
| 284 | 439 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | # _explain | ||||||||||||||||||||||||||||||||||||||||||
| 287 | # Purpose: name the cause of a validation failure in catalogue terms. | ||||||||||||||||||||||||||||||||||||||||||
| 288 | # Entry: the input, schema and routine name of a call the validator | ||||||||||||||||||||||||||||||||||||||||||
| 289 | # refused. The validator reports only English text, so the cause | ||||||||||||||||||||||||||||||||||||||||||
| 290 | # is found again from the schema rather than parsed from it. | ||||||||||||||||||||||||||||||||||||||||||
| 291 | # Exit: raises args.unknown, args.missing, args.not_number, | ||||||||||||||||||||||||||||||||||||||||||
| 292 | # args.not_integer, args.too_small, args.too_large, | ||||||||||||||||||||||||||||||||||||||||||
| 293 | # args.too_short, args.too_long, args.too_few, args.too_many, | ||||||||||||||||||||||||||||||||||||||||||
| 294 | # args.not_one_of or args.bad_form; returns when none applies | ||||||||||||||||||||||||||||||||||||||||||
| 295 | # (a rule this does not check, such as isa). | ||||||||||||||||||||||||||||||||||||||||||
| 296 | # Side effects: none beyond the raise. | ||||||||||||||||||||||||||||||||||||||||||
| 297 | 21 | ▶ | sub _explain :Private { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_explain: called CC=34 SCAR=36.0
| |||||||||||||||||||||||||||||||||||||||||||
| 298 | 21 | my ($input, $schema, $routine) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||
| 300 | 33 | ▶ | my $what = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | 33 | my $param = shift; | |||||||||||||||||||||||||||||||||||||||||
| 302 | 33 | ▶ | return $OPTION{$param} // message('args.param', { routine => $routine, param => $param }); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$App::FileSync::Args::OPTION{$param} // message("args.param", {"routine", $routine, "param", $param})
MC/DC0%0/2
$App::FileSync::Args::OPTION{$param} // message("args.param", {"routine", $routine, "param", $param})
$App::FileSync::Args::OPTION{$param}
message("args.param", {"routine", $routine, "param", $param})
Truth table66%2/3
A // B
A= $App::FileSync::Args::OPTION{$param}B= message("args.param", {"routine", $routine, "param", $param})
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | 21 | }; | |||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | 21 | for my $param (sort keys %{$input}) { | |||||||||||||||||||||||||||||||||||||||||
| 306 | 49 | ▶ | throw('args.unknown', { what => $what->($param) }) unless exists $schema->{$param}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | 20 | for my $param (sort keys %{$schema}) { | |||||||||||||||||||||||||||||||||||||||||
| 310 | 70 | my $rule = $schema->{$param}; | |||||||||||||||||||||||||||||||||||||||||
| 311 | 70 | ▶ | next unless ref $rule eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | 70 | ▶ | if (!exists $input->{$param}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 313 | 37 | ▶ | throw('args.missing', { what => $what->($param) }) unless $rule->{optional}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | 36 | next; | |||||||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | 33 | my $value = $input->{$param}; | |||||||||||||||||||||||||||||||||||||||||
| 318 | 33 | ▶ | next unless defined $value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 319 | 31 | ▶ | my $type = $rule->{type} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$rule->{"type"} // ""
MC/DC0%0/1
$rule->{"type"} // ""
$rule->{"type"}
Truth table50%1/2
A
A= $rule->{"type"}
| |||||||||||||||||||||||||||||||||||||||||||
| 320 | 31 | ▶ | my %args = (what => $what->($param), value => ref $value ? ref $value : $value); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | 31 | ▶ | if (exists $NUMERIC{$type}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 323 | 14 | ▶ | throw('args.not_integer', \%args) if $NUMERIC{$type} && (ref $value || $value !~ $INTEGER); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$NUMERIC{$type} and ref $value || !($value =~ /$INTEGER/)
ref $value or not $value =~ /$INTEGER/
MC/DC66%2/3
$NUMERIC{$type} and ref $value || !($value =~ /$INTEGER/)
$NUMERIC{$type}
ref $value
not $value =~ /$INTEGER/
Truth table75%3/4
A and B or not(C)
A= $NUMERIC{$type}B= ref $valueC= not $value =~ /$INTEGER/
| |||||||||||||||||||||||||||||||||||||||||||
| 324 | 12 | ▶ | throw('args.not_number', \%args) if ref $value || !looks_like_number($value); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $value or not &looks_like_number($value)
MC/DC50%1/2
ref $value or not &looks_like_number($value)
ref $value
not &looks_like_number($value)
Truth table66%2/3
A or not(B)
A= ref $valueB= not &looks_like_number($value)
| |||||||||||||||||||||||||||||||||||||||||||
| 325 | 11 | ▶ | throw('args.too_small', { %args, min => $rule->{min} }) if defined $rule->{min} && $value < $rule->{min}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $rule->{"min"} and $value < $rule->{"min"}
MC/DC50%1/2
defined $rule->{"min"} and $value < $rule->{"min"}
defined $rule->{"min"}
$value < $rule->{"min"}
Truth table66%2/3
A and B
A= defined $rule->{"min"}B= $value < $rule->{"min"}
| |||||||||||||||||||||||||||||||||||||||||||
| 326 | 9 | ▶ | throw('args.too_large', { %args, max => $rule->{max} }) if defined $rule->{max} && $value > $rule->{max}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $rule->{"max"} and $value > $rule->{"max"}
MC/DC50%1/2
defined $rule->{"max"} and $value > $rule->{"max"}
defined $rule->{"max"}
$value > $rule->{"max"}
Truth table66%2/3
A and B
A= defined $rule->{"max"}B= $value > $rule->{"max"}
| |||||||||||||||||||||||||||||||||||||||||||
| 327 | ▶ | } elsif (exists $SIZE_OF{$type} && defined(my $size = $SIZE_OF{$type}->($value))) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
exists $SIZE_OF{$type} and defined(my $size = $SIZE_OF{$type}->($value))
MC/DC50%1/2
exists $SIZE_OF{$type} and defined(my $size = $SIZE_OF{$type}->($value))
exists $SIZE_OF{$type}
defined(my $size = $SIZE_OF{$type}->($value))
Truth table66%2/3
A and B
A= exists $SIZE_OF{$type}B= defined(my $size = $SIZE_OF{$type}->($value))
| |||||||||||||||||||||||||||||||||||||||||||
| 328 | 15 | my $string = !ref $value; | |||||||||||||||||||||||||||||||||||||||||
| 329 | 15 | ▶ | if (defined $rule->{min} && $size < $rule->{min}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $rule->{"min"} and $size < $rule->{"min"}
MC/DC100%2/2
defined $rule->{"min"} and $size < $rule->{"min"}
defined $rule->{"min"}
$size < $rule->{"min"}
Truth table100%3/3
A and B
A= defined $rule->{"min"}B= $size < $rule->{"min"}
| |||||||||||||||||||||||||||||||||||||||||||
| 330 | 2 | ▶ | throw('args.too_short', { %args, count => $rule->{min} }) if $string; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 331 | 1 | throw('args.too_few', { %args, count => $rule->{min} }); | |||||||||||||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||||||||||||
| 333 | 13 | ▶ | if (defined $rule->{max} && $size > $rule->{max}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $rule->{"max"} and $size > $rule->{"max"}
MC/DC50%1/2
defined $rule->{"max"} and $size > $rule->{"max"}
defined $rule->{"max"}
$size > $rule->{"max"}
Truth table66%2/3
A and B
A= defined $rule->{"max"}B= $size > $rule->{"max"}
| |||||||||||||||||||||||||||||||||||||||||||
| 334 | 1 | ▶ | throw('args.too_long', { %args, count => $rule->{max} }) if $string; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 335 | 0 | throw('args.too_many', { %args, count => $rule->{max} }); | |||||||||||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||
| 339 | 16 | ▶ | if (ref $rule->{memberof} eq 'ARRAY' && !ref $value && !grep { $_ eq $value } @{ $rule->{memberof} }) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
ref $rule->{"memberof"} eq "ARRAY" and not ref $value and not grep({$_ eq $value;} @{$$rule{"memberof"};})
ref $rule->{"memberof"} eq "ARRAY" and not ref $value
MC/DC33%1/3
ref $rule->{"memberof"} eq "ARRAY" and not ref $value and not grep({$_ eq $value;} @{$$rule{"memberof"};})
ref $rule->{"memberof"} eq "ARRAY"
not ref $value
not grep({$_ eq $value;} @{$$rule{"memberof"};})
Truth table50%2/4
A and not(B) and not(C)
A= ref $rule->{"memberof"} eq "ARRAY"B= not ref $valueC= not grep({$_ eq $value;} @{$$rule{"memberof"};})
| |||||||||||||||||||||||||||||||||||||||||||
| 340 | 1 | throw('args.not_one_of', { %args, list => join(', ', @{ $rule->{memberof} }) }); | |||||||||||||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||||||||||||
| 342 | 15 | ▶ | if (defined $rule->{matches} && !ref $value && $value !~ $rule->{matches}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $rule->{"matches"} and not ref $value and not $value =~ /$rule->{"matches"}/
defined $rule->{"matches"} and not ref $value
MC/DC33%1/3
defined $rule->{"matches"} and not ref $value and not $value =~ /$rule->{"matches"}/
defined $rule->{"matches"}
not ref $value
not $value =~ /$rule->{"matches"}/
Truth table50%2/4
A and not(B) and not(C)
A= defined $rule->{"matches"}B= not ref $valueC= not $value =~ /$rule->{"matches"}/
| |||||||||||||||||||||||||||||||||||||||||||
| 343 | 1 | throw('args.bad_form', \%args); | |||||||||||||||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||
| 347 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||
| 348 | 439 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | 439 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | __END__ | ||||||||||||||||||||||||||||||||||||||||||