| 1 | package Valiemon::Attributes::Format; | ||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use parent qw(Valiemon::Attributes); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 3 | ▶ | use Carp qw(croak); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use Data::Validate::URI qw(); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 40 | ▶ | sub attr_name { 'format' } | ||||||||
|
Subroutine
attr_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 11 | |||||||||||
| 12 | sub is_valid { | ||||||||||
| 13 | 41 | ▶ | my ($class, $context, $schema, $data) = @_; | ||||||||
|
Subroutine
is_valid: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 14 | |||||||||||
| 15 | 41 | ▶ | return 1 unless $context->prims->is_string($data); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | |||||||||||
| 17 | $context->in_attr($class, sub { | ||||||||||
| 18 | 40 | ▶ | my $format = $schema->{format}; | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 19 | |||||||||||
| 20 | 40 | ▶ | if ($format eq 'date-time') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | 10 | return _validate_date_time($data); | |||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | 30 | ▶ | if ($format eq 'uri') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 16 | return _validate_uri($data); | |||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | # TODO: email, hostname, ipv4, ipv6 | ||||||||||
| 29 | |||||||||||
| 30 | 14 | croak sprintf 'invalid format: `%s`', $format; | |||||||||
| 31 | 40 | }); | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | sub _validate_date_time { | ||||||||||
| 35 | 10 | ▶ | my ($data) = @_; | ||||||||
|
Subroutine
_validate_date_time: called CC=2 SCAR=6.9
| |||||||||||
| 36 | # TODO: check range of value (date-month must be `01-12` etc...) | ||||||||||
| 37 | 10 | ▶ | $data =~ /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})/ ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | sub _validate_uri { | ||||||||||
| 41 | 16 | ▶ | my ($data) = @_; | ||||||||
|
Subroutine
_validate_uri: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 16 | Data::Validate::URI::is_uri($data); | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 3 | 1; | |||||||||