File Coverage

blib/lib/Valiemon/Attributes/Required.pm
Criterion Covered Total %
statement 29 29 100.0
branch 5 6 83.3
condition 5 6 83.3
subroutine 9 9 100.0
pod 0 2 0.0
total 48 52 92.3


line stmt bran cond sub pod time code
1             package Valiemon::Attributes::Required;
2 5     5   3909 use strict;
  5         9  
  5         193  
3 5     5   30 use warnings;
  5         7  
  5         204  
4 5     5   30 use utf8;
  5         9  
  5         34  
5 5     5   944 use parent qw(Valiemon::Attributes);
  5         373  
  5         35  
6              
7 5     5   385 use Carp qw(croak);
  5         11  
  5         474  
8 5     5   770 use List::MoreUtils qw(all);
  5         14179  
  5         41  
9              
10 29     29 0 134 sub attr_name { 'required' }
11              
12             sub is_valid {
13 29     29 0 64 my ($class, $context, $schema, $data) = @_;
14              
15 29 50       147 return 1 unless ref $data eq 'HASH'; # ignore
16              
17 29         59 my $required = $schema->{required};
18             $context->in_attr($class, sub {
19 29 100 100 29   198 if (ref $required ne 'ARRAY' || scalar @$required < 1) {
20 2         7 croak sprintf '`required` must be an array and have at leas one element at %s', $context->position
21             }
22             all {
23 47         96 my $prop_def = $schema->{properties}->{$_};
24 47   66     122 my $has_default = $prop_def && do {
25             # resolve $ref TODO refactor
26             my $definition = $prop_def->{'$ref'} ? $context->rv->resolve_ref($prop_def->{'$ref'}) : $prop_def;
27             $definition->{default};
28             };
29 47 100       221 $has_default || exists $data->{$_}
30 27         236 } @$required;
31 29         211 });
32             }
33              
34             1;