| 1 | package Validator::Lazy::Role::Check::IP; | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | 2 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||
| 7 | Validator::Lazy::Role::Check::IP | ||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||
| 10 | =head1 VERSION | ||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||
| 12 | Version 0.03 | ||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||
| 17 | use Validator::Lazy; | ||||||||||||||||||||||||||||||||||
| 18 | my $v = Validator::Lazy->new( { ip => { IP => { v => 4, type => 'Public' } } } ); # empty, or list, or scalar with 2letters countrycode | ||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | my $ok = $v->check( ip => '192.1.1.1' ); # ok is false | ||||||||||||||||||||||||||||||||||
| 21 | say Dumper $v->errors; # [ { code => 'IP_PUBLIC_TYPE_REQUIRED', field => 'ip', data => { } } ] | ||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||
| 26 | An internal Role for Validator::Lazy, part of Validator::Lazy package. | ||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||
| 28 | Provides "IP" type for Validator::Lazy config. | ||||||||||||||||||||||||||||||||||
| 29 | Allows to check ip addresses. | ||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||
| 31 | When called without param - performs simple check as any valid ip | ||||||||||||||||||||||||||||||||||
| 32 | When param is passed - performs additional check as value-in-list | ||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||
| 34 | =head1 METHODS | ||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||
| 36 | =head2 C<check> | ||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||
| 38 | Called from inside if Validator::Lazy->check process | ||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||
| 40 | Temporary overrides internal Validator::Lazy::check method like this: | ||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||
| 42 | $validator->check( $value, $param ); | ||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||
| 44 | $param - hash, that can contain this optional keys: | ||||||||||||||||||||||||||||||||||
| 45 | 'version' or 'ver' or 'v' = v4 v6 4 6 | ||||||||||||||||||||||||||||||||||
| 46 | 'type' = Public|Private|Reserved|Loopback|... as described in Net::IP package | ||||||||||||||||||||||||||||||||||
| 47 | 'mask' = 32,24,16,... | ||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||
| 49 | $value - your value to check | ||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||
| 53 | =head1 SUPPORT AND DOCUMENTATION | ||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||
| 55 | After installing, you can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||
| 57 | perldoc Validator::Lazy | ||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||
| 59 | You can also look for information at: | ||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||
| 61 | RT, CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||||||||
| 62 | http://rt.cpan.org/NoAuth/Bugs.html?Dist=Validator-Lazy | ||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||
| 64 | AnnoCPAN, Annotated CPAN documentation | ||||||||||||||||||||||||||||||||||
| 65 | http://annocpan.org/dist/Validator-Lazy | ||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||
| 67 | CPAN Ratings | ||||||||||||||||||||||||||||||||||
| 68 | http://cpanratings.perl.org/d/Validator-Lazy | ||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||
| 70 | Search CPAN | ||||||||||||||||||||||||||||||||||
| 71 | http://search.cpan.org/dist/Validator-Lazy/ | ||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||
| 74 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||
| 76 | ANTONC <antonc@cpan.org> | ||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||
| 78 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||
| 80 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||
| 81 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||||||||||||||||||||||||||
| 82 | copy of the full license at: | ||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||
| 84 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||
| 88 | 2 | ▶ | use v5.14.0; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 89 | 2 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 90 | 2 | ▶ | use Modern::Perl; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 91 | 2 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 92 | 2 | ▶ | use Net::IP; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||
| 95 | 12 | ▶ | sub check { | ||||||||||||||||||||||||||||||||
|
Subroutine
check: called CC=21 SCAR=32.3
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 96 | 12 | my ( $self, $value, $param ) = @_; | |||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||
| 98 | 12 | ▶ | return $value unless $value; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||
| 100 | 10 | my $ip = Net::IP->new( $value ); | |||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||
| 102 | 10 | ▶ | unless ( $ip ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 103 | 2 | $self->add_error(); | |||||||||||||||||||||||||||||||||
| 104 | 2 | return $value; | |||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||
| 107 | 8 | ▶ | if ( my $err = $ip->error() ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 108 | 0 | $self->add_error( { error_code => $err } ); | |||||||||||||||||||||||||||||||||
| 109 | 0 | return $value; | |||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||
| 111 | # ver => [ 4, 6 ] | ||||||||||||||||||||||||||||||||||
| 112 | 8 | ▶ | my $v = $param->{version} || $param->{ver} || $param->{v}; | ||||||||||||||||||||||||||||||||
|
Condition66%4/6
$param->{'version'} || $param->{'ver'} || $param->{'v'}
$param->{"version"} or $param->{"ver"}
MC/DC33%1/3
$param->{'version'} || $param->{'ver'} || $param->{'v'}
$param->{"version"}
$param->{"ver"}
$param->{'v'}
Truth table50%2/4
A or B || C
A= $param->{"version"}B= $param->{"ver"}C= $param->{'v'}
| |||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||
| 114 | 8 | ▶ | if ( $v ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||
| 116 | 3 | $v =~ s/^v//i; | |||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||
| 118 | 3 | ▶ | confess 'Bad version. ver = 4 or 6 allowed' unless $v =~ /^[46]$/; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||
| 120 | 3 | ▶ | if ( $ip->version ne $v ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 121 | 1 | $self->add_error( 'IP_V' . $v . '_REQUIRED' ); | |||||||||||||||||||||||||||||||||
| 122 | 1 | return $value; | |||||||||||||||||||||||||||||||||
| 123 | }; | ||||||||||||||||||||||||||||||||||
| 124 | }; | ||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||
| 126 | 7 | ▶ | if ( my $tt = $param->{type} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||
| 128 | 2 | ▶ | my @t = | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
ref $tt and ref $tt eq "ARRAY"
MC/DC0%0/2
ref $tt and ref $tt eq "ARRAY"
ref $tt
ref $tt eq "ARRAY"
Truth table33%1/3
A and B
A= ref $ttB= ref $tt eq "ARRAY"
| |||||||||||||||||||||||||||||||||||
| 129 | ref $tt && ref $tt eq 'ARRAY' ? @$tt : | ||||||||||||||||||||||||||||||||||
| 130 | ! ref $tt ? [ $tt ] : | ||||||||||||||||||||||||||||||||||
| 131 | (); | ||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||
| 133 | 2 | my $t_ok = scalar @t; | |||||||||||||||||||||||||||||||||
| 134 | 2 | ▶ | $t_ok &&= /^[A-Z]+$/i for @t; | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
$t_ok &&= /^[A-Z]+$/iu
MC/DC0%0/2
$t_ok &&= /^[A-Z]+$/iu
$t_ok
/^[A-Z]+$/iu
Truth table33%1/3
A &&= B
A= $t_okB= /^[A-Z]+$/iu
| |||||||||||||||||||||||||||||||||||
| 135 | 2 | ▶ | confess 'Bad type. type = [Public|Private|Reserved|Loopback|...] allowed' unless $t_ok; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||
| 137 | 2 | for my $t ( @t ) { | |||||||||||||||||||||||||||||||||
| 138 | # type => [ Public, Private, Reserved, Loopback,... ] | ||||||||||||||||||||||||||||||||||
| 139 | 2 | ▶ | if ( $ip->iptype ne uc($t) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 140 | 1 | $self->add_error( 'IP_' . uc($t) . '_TYPE_REQUIRED' ); | |||||||||||||||||||||||||||||||||
| 141 | 1 | return $value; | |||||||||||||||||||||||||||||||||
| 142 | }; | ||||||||||||||||||||||||||||||||||
| 143 | }; | ||||||||||||||||||||||||||||||||||
| 144 | }; | ||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||
| 146 | # mask => 32/24/... | ||||||||||||||||||||||||||||||||||
| 147 | 6 | ▶ | if ( my $m = $param->{mask} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 148 | 2 | ▶ | confess 'Bad mask. mask = dd allowed' unless $m =~ /^\d{1,2}$/; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||
| 150 | 2 | ▶ | if ( $ip->prefixlen ne $m ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 151 | 1 | $self->add_error( 'MASK_ERROR', { required => $m } ); | |||||||||||||||||||||||||||||||||
| 152 | 1 | return $value; | |||||||||||||||||||||||||||||||||
| 153 | }; | ||||||||||||||||||||||||||||||||||
| 154 | }; | ||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||
| 156 | 5 | return $value; | |||||||||||||||||||||||||||||||||
| 157 | }; | ||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||
| 159 | 2 | 1; | |||||||||||||||||||||||||||||||||