File Coverage

blib/lib/WebService/Mattermost/V4/API/Role/RequireID.pm
Criterion Covered Total %
statement 15 15 100.0
branch 4 4 100.0
condition 8 9 88.8
subroutine 4 4 100.0
pod 2 2 100.0
total 33 34 97.0


line stmt bran cond sub pod time code
1             package WebService::Mattermost::V4::API::Role::RequireID;
2:

3: # ABSTRACT: ID validation. 4:
5: use Moo::Role;
6: use Types::Standard 'RegexpRef';
7:
8: requires 'error_return';
9:
10: ################################################################################
11:
12: has id_validation_regexp => (is => 'ro', isa => RegexpRef, default => sub { qr{(?i)^[a-z0-9\-]+$} });
13:
14: ################################################################################
15:
16: sub validate_id {
17: my $self = shift;
18: my $next = shift;
19: my $id = shift || $self->id;
20:
21: if ($self->validate_id_no_next($id)) {
22: return $self->$next($id, @_);
23: }
24:
25: return $self->error_return('Invalid or missing ID parameter');
26: }
27:
28: sub validate_id_no_next {
29: my $self = shift;
30: my $id = shift || $self->id;
31:
32: return ($id && $id =~ $self->id_validation_regexp) ? 1 : 0;
33: }
34:
35: ################################################################################
36:
37: 1;
38:
39: __END__
40:
41: =pod
42:
43: =encoding UTF-8
44:
45: =head1 NAME
46:
47: WebService::Mattermost::V4::API::Role::RequireID - ID validation.
48:
49: =head1 VERSION
50:
51: version 0.28
52:
53: =head1 DESCRIPTION
54:
55: Validate that the first parameter passed to a subroutine is a valid UUID for
56: Mattermost.
57:
58: =head2 METHODS
59:
60: =over 4
61:
62: =item C<validate_id()>
63:
64: Validate the ID and run the next subroutine.
65:
66: =item C<validate_id_no_next()>
67:
68: Validate the ID but do not run the next subroutine.
69:
70: =back
71:
72: =head2 ATTRIBUTES
73:
74: =over 4
75:
76: =item C<id_validation_regexp>
77:
78: Basic UUID matching regular expression.
79:
80: =back
81:
82: =head1 AUTHOR
83:
84: Mike Jones <mike@netsplit.org.uk>
85:
86: =head1 COPYRIGHT AND LICENSE
87:
88: This software is Copyright (c) 2020 by Mike Jones.
89:
90: This is free software, licensed under:
91:
92: The MIT (X11) License
93:
94: =cut
95: