Skip to content

Commit 2f12813

Browse files
committed
Refactor Rx types into their own modules and share amongst test files
1 parent 9d62f3a commit 2f12813

File tree

9 files changed

+411
-462
lines changed

9 files changed

+411
-462
lines changed

xt/lib/Local/Rx.pm

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package # hide from PAUSE
2+
Local::Rx;
3+
4+
use Data::Rx;
5+
6+
use Local::Rx::Type::CommitID;
7+
use Local::Rx::Type::CVE;
8+
use Local::Rx::Type::GHSA;
9+
use Local::Rx::Type::URL;
10+
use Local::Rx::Type::VCS_URL;
11+
use Local::Rx::Type::YYYYMMDD;
12+
13+
sub new {
14+
Data::Rx->new({
15+
type_plugins => [qw(
16+
Local::Rx::Type::YYYYMMDD
17+
Local::Rx::Type::GHSA
18+
Local::Rx::Type::CVE
19+
Local::Rx::Type::URL
20+
Local::Rx::Type::VCS_URL
21+
Local::Rx::Type::CommitID
22+
)],
23+
});
24+
}
25+
26+
__PACKAGE__;

xt/lib/Local/Rx/Type/CVE.pm

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package # hide from PAUSE
2+
Local::Rx::Type::CVE;
3+
use parent 'Data::Rx::CommonType::EasyNew';
4+
5+
sub type_uri {
6+
'tag:example.com,EXAMPLE:rx/cve',
7+
}
8+
9+
sub assert_valid {
10+
my ($self, $value) = @_;
11+
$value =~ /\ACVE-\d+-\d+\z/a or $self->fail({
12+
error => [ qw(type) ],
13+
message => "value <$value> is not a valid CVE identifier",
14+
value => $value,
15+
})
16+
}
17+
18+
__PACKAGE__;

xt/lib/Local/Rx/Type/CommitID.pm

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package # hide from PAUSE
2+
Local::Rx::Type::CommitID;
3+
use parent 'Data::Rx::CommonType::EasyNew';
4+
5+
sub type_uri {
6+
'tag:example.com,EXAMPLE:rx/commit-id',
7+
}
8+
9+
sub assert_valid {
10+
my ($self, $value) = @_;
11+
if( ! defined $value ) {
12+
return $self->fail({
13+
error => [ qw(type) ],
14+
message => "URL value is not defined",
15+
value => $value,
16+
})
17+
}
18+
elsif( $value !~ m{\A[\da-f]{40}\z}ia ) {
19+
$self->fail({
20+
error => [ qw(type) ],
21+
message => "Commit ID <$value> is not 40 hexadecimal characters",
22+
value => $value,
23+
})
24+
}
25+
return 1;
26+
}
27+
28+
__PACKAGE__;

xt/lib/Local/Rx/Type/GHSA.pm

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package # hide from PAUSE
2+
Local::Rx::Type::GHSA;
3+
use parent 'Data::Rx::CommonType::EasyNew';
4+
5+
sub type_uri {
6+
'tag:example.com,EXAMPLE:rx/ghsa',
7+
}
8+
9+
sub assert_valid { # GHSA-6wjc-jvcr-hcxw
10+
my ($self, $value) = @_;
11+
$value =~ /\AGHSA(?:-[a-z0-9]{4}){3}\z/a or $self->fail({
12+
error => [ qw(type) ],
13+
message => "value <$value> is not a valid GitHub Advisory Database identifier",
14+
value => $value,
15+
})
16+
}
17+
18+
__PACKAGE__;

xt/lib/Local/Rx/Type/URL.pm

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package # hide from PAUSE
2+
Local::Rx::Type::URL;
3+
use parent 'Data::Rx::CommonType::EasyNew';
4+
5+
sub type_uri {
6+
'tag:example.com,EXAMPLE:rx/url',
7+
}
8+
9+
sub assert_valid {
10+
my ($self, $value) = @_;
11+
if( ! defined $value ) {
12+
return $self->fail({
13+
error => [ qw(type) ],
14+
message => "URL value is not defined",
15+
value => $value,
16+
})
17+
}
18+
elsif( $value !~ m{\A(?:https?|ftp)://}ia ) {
19+
$self->fail({
20+
error => [ qw(type) ],
21+
message => "URL value <$value> is not valid",
22+
value => $value,
23+
})
24+
}
25+
return 1;
26+
}
27+
28+
__PACKAGE__;

xt/lib/Local/Rx/Type/VCS_URL.pm

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package # hide from PAUSE
2+
Local::Rx::Type::VCS_URL;
3+
use parent 'Data::Rx::CommonType::EasyNew';
4+
5+
sub type_uri {
6+
'tag:example.com,EXAMPLE:rx/vcs-url',
7+
}
8+
9+
sub assert_valid {
10+
my ($self, $value) = @_;
11+
if( ! defined $value ) {
12+
return $self->fail({
13+
error => [ qw(type) ],
14+
message => "URL value is not defined",
15+
value => $value,
16+
})
17+
}
18+
elsif( $value !~ m{\A(?:https?|git|svn)://}ia ) {
19+
$self->fail({
20+
error => [ qw(type) ],
21+
message => "URL value <$value> is not valid",
22+
value => $value,
23+
})
24+
}
25+
return 1;
26+
}
27+
28+
__PACKAGE__;

xt/lib/Local/Rx/Type/YYYYMMDD.pm

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package # hide from PAUSE
2+
Local::Rx::Type::YYYYMMDD;
3+
use parent 'Data::Rx::CommonType::EasyNew';
4+
5+
sub type_uri {
6+
'tag:example.com,EXAMPLE:rx/cpansa-date',
7+
}
8+
9+
sub assert_valid {
10+
my ($self, $value) = @_;
11+
return 1 unless defined $value;
12+
$value =~ /\A(?:19[6789]\d|20[012]\d)-\d\d-\d\d\z/a or $self->fail({
13+
error => [ qw(type) ],
14+
message => "date value is not YYYY-MM-DD",
15+
value => $value,
16+
})
17+
}
18+
19+
__PACKAGE__;

0 commit comments

Comments
 (0)