| 1 | package Valiant::Errors; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 40 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 40 | ▶ | use Data::Perl::Collection::Array; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 40 | ▶ | use Valiant::NestedError; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 40 | ▶ | use Valiant::Util 'throw_exception'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 40 | ▶ | use overload ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 36 | ▶ | bool => sub { shift->size ? 1:0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 10 | 40 | ); | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 40 | has 'object' => ( | |||||||||||||||||||||||||||
| 13 | is => 'ro', | ||||||||||||||||||||||||||||
| 14 | required => 1, | ||||||||||||||||||||||||||||
| 15 | weak_ref => 1, | ||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | has errors => ( | ||||||||||||||||||||||||||||
| 19 | is => 'ro', | ||||||||||||||||||||||||||||
| 20 | init_arg => undef, | ||||||||||||||||||||||||||||
| 21 | lazy => 1, | ||||||||||||||||||||||||||||
| 22 | required => 1, | ||||||||||||||||||||||||||||
| 23 | 121 | ▶ | default => sub { Data::Perl::Collection::Array->new() }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 40 | handles => { | |||||||||||||||||||||||||||
| 25 | size => 'count', | ||||||||||||||||||||||||||||
| 26 | count => 'count', | ||||||||||||||||||||||||||||
| 27 | clear => 'clear', | ||||||||||||||||||||||||||||
| 28 | blank => 'is_empty', | ||||||||||||||||||||||||||||
| 29 | empty => 'is_empty', | ||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 3 | ▶ | sub i18n_class { 'Valiant::I18N' } | ||||||||||||||||||||||||||
|
Subroutine
i18n_class: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | has 'i18n' => ( | ||||||||||||||||||||||||||||
| 36 | is => 'ro', | ||||||||||||||||||||||||||||
| 37 | required => 1, | ||||||||||||||||||||||||||||
| 38 | 3 | ▶ | default => sub { Module::Runtime::use_module(shift->i18n_class) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 40 | ); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 266 | ▶ | sub error_class { 'Valiant::Error' } | ||||||||||||||||||||||||||
|
Subroutine
error_class: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # Return a flat list of all errors with duplicates removed, comparing errors | ||||||||||||||||||||||||||||
| 44 | # with Valiant::Error's equals method (object + attribute + type + options). | ||||||||||||||||||||||||||||
| 45 | 1 | ▶ | sub uniq { | ||||||||||||||||||||||||||
|
Subroutine
uniq: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 47 | 1 | my @uniq; | |||||||||||||||||||||||||||
| 48 | 1 | foreach my $error ($self->errors->all) { | |||||||||||||||||||||||||||
| 49 | 3 | ▶ | push @uniq, $error unless grep { $_->equals($error) } @uniq; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 1 | return @uniq; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 4 | ▶ | sub any { | ||||||||||||||||||||||||||
|
Subroutine
any: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 4 | my ($self, $code) = @_; | |||||||||||||||||||||||||||
| 56 | 0 | ▶ | $code ||= sub { $_ }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
$code ||= sub {
$_;
}
MC/DC0%0/2
$code ||= sub {
$_;
}
$code
sub {
$_;
}
Truth table33%1/3
A ||= B
A= $codeB= sub {
$_;
}
| |||||||||||||||||||||||||||||
| 57 | 4 | foreach my $error ($self->errors->all) { | |||||||||||||||||||||||||||
| 58 | 14 | local $_ = $error; | |||||||||||||||||||||||||||
| 59 | 14 | ▶ | return 1 if $code->($error); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 1 | return 0; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | ▶ | sub copy { | ||||||||||||||||||||||||||
|
Subroutine
copy: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 1 | my ($self, $other) = @_; | |||||||||||||||||||||||||||
| 66 | my $errors = $other | ||||||||||||||||||||||||||||
| 67 | ->errors | ||||||||||||||||||||||||||||
| 68 | 8 | ▶ | ->map(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | 8 | my $class = ref $_; | |||||||||||||||||||||||||||
| 70 | 8 | my $new = $class->new( | |||||||||||||||||||||||||||
| 71 | object => $self->object, | ||||||||||||||||||||||||||||
| 72 | attribute => $_->attribute, | ||||||||||||||||||||||||||||
| 73 | type => $_->type, | ||||||||||||||||||||||||||||
| 74 | i18n => $_->i18n, | ||||||||||||||||||||||||||||
| 75 | options => $_->options, | ||||||||||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||||||||||
| 77 | 8 | return $new; | |||||||||||||||||||||||||||
| 78 | 1 | }); | |||||||||||||||||||||||||||
| 79 | 1 | $self->errors->clear; | |||||||||||||||||||||||||||
| 80 | 1 | $self->errors->push($errors->all); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 5 | ▶ | sub import_error { | ||||||||||||||||||||||||||
|
Subroutine
import_error: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | 5 | my ($self, $error, $options) = @_; | |||||||||||||||||||||||||||
| 85 | $self->errors->push( | ||||||||||||||||||||||||||||
| 86 | my $nested_err = Valiant::NestedError->new( | ||||||||||||||||||||||||||||
| 87 | inner_error => $error, | ||||||||||||||||||||||||||||
| 88 | object => $error->object, | ||||||||||||||||||||||||||||
| 89 | attribute => $error->attribute, | ||||||||||||||||||||||||||||
| 90 | type => $error->type, | ||||||||||||||||||||||||||||
| 91 | 5 | ▶ | %{ $options||+{} }, | ||||||||||||||||||||||||||
|
Condition50%1/2
$options || {}
MC/DC0%0/1
$options || {}
$options
Truth table50%1/2
A
A= $options
| |||||||||||||||||||||||||||||
| 92 | ) | ||||||||||||||||||||||||||||
| 93 | ); | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 1 | ▶ | sub merge { | ||||||||||||||||||||||||||
|
Subroutine
merge: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | 1 | my ($self, $other) = @_; | |||||||||||||||||||||||||||
| 98 | 1 | foreach my $error ($other->errors->all) { | |||||||||||||||||||||||||||
| 99 | 4 | $self->import_error($error); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 290 | ▶ | sub where { | ||||||||||||||||||||||||||
|
Subroutine
where: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | 290 | my $self = shift; | |||||||||||||||||||||||||||
| 105 | 290 | my ($attribute, $type, $options) = $self->_normalize_arguments(@_); | |||||||||||||||||||||||||||
| 106 | 2609 | ▶ | return $self->errors->grep(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 107 | 2609 | $_->match($attribute, $type, $options); | |||||||||||||||||||||||||||
| 108 | 290 | })->all; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 561 | ▶ | sub _normalize_arguments { | ||||||||||||||||||||||||||
|
Subroutine
_normalize_arguments: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||
| 112 | 561 | my ($self, $attribute, $type, $options) = @_; | |||||||||||||||||||||||||||
| 113 | 561 | ▶ | if(ref($type) && ref($type) eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $type and ref $type eq "CODE"
MC/DC0%0/2
ref $type and ref $type eq "CODE"
ref $type
ref $type eq "CODE"
Truth table66%2/3
A and B
A= ref $typeB= ref $type eq "CODE"
| |||||||||||||||||||||||||||||
| 114 | 0 | $type = $type->($self->object, $options); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | return ( | ||||||||||||||||||||||||||||
| 117 | 561 | $attribute, | |||||||||||||||||||||||||||
| 118 | $type, | ||||||||||||||||||||||||||||
| 119 | $options, | ||||||||||||||||||||||||||||
| 120 | ); | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # Returns +true+ if the error messages include an error for the given key | ||||||||||||||||||||||||||||
| 124 | # +attribute+, +false+ otherwise. | ||||||||||||||||||||||||||||
| 125 | 0 | ▶ | sub include { | ||||||||||||||||||||||||||
|
Subroutine
include: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 126 | 0 | my ($self, $attribute) = @_; | |||||||||||||||||||||||||||
| 127 | 0 | ▶ | return scalar($self->any(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 128 | 0 | $_->match($attribute); | |||||||||||||||||||||||||||
| 129 | 0 | })); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | 40 | *has_key = \&include; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # Delete messages for +key+. Returns the deleted messages. | ||||||||||||||||||||||||||||
| 134 | 1 | ▶ | sub delete { | ||||||||||||||||||||||||||
|
Subroutine
delete: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 136 | 1 | my ($attribute, $type, $options) = $self->_normalize_arguments(@_); | |||||||||||||||||||||||||||
| 137 | 1 | my @deleted = (); | |||||||||||||||||||||||||||
| 138 | 1 | my $idx = 0; | |||||||||||||||||||||||||||
| 139 | 1 | foreach my $error($self->errors->all) { | |||||||||||||||||||||||||||
| 140 | 8 | ▶ | if($error->match($attribute, $type, $options)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 4 | push @deleted, $self->errors->delete($idx); | |||||||||||||||||||||||||||
| 142 | } else { | ||||||||||||||||||||||||||||
| 143 | 4 | $idx++ | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | 1 | return @deleted; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 17 | ▶ | sub each { | ||||||||||||||||||||||||||
|
Subroutine
each: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 150 | 17 | my ($self, $block) = @_; | |||||||||||||||||||||||||||
| 151 | 17 | foreach my $error($self->errors->all) { | |||||||||||||||||||||||||||
| 152 | 39 | ▶ | $block->((defined($error->attribute) ? $error->attribute : '*'), $error->message); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 9 | ▶ | sub model_errors { | ||||||||||||||||||||||||||
|
Subroutine
model_errors: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 157 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 158 | 9 | my @errors; | |||||||||||||||||||||||||||
| 159 | 9 | foreach my $error($self->errors->all) { | |||||||||||||||||||||||||||
| 160 | 138 | ▶ | push @errors, $error if !$error->has_attribute || !defined($error->attribute); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$error->has_attribute and defined $error->attribute
MC/DC50%1/2
$error->has_attribute and defined $error->attribute
$error->has_attribute
defined $error->attribute
Truth table66%2/3
A and B
A= $error->has_attributeB= defined $error->attribute
| |||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | 9 | return @errors; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 9 | ▶ | sub model_messages { | ||||||||||||||||||||||||||
|
Subroutine
model_messages: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 166 | 9 | my ($self, $full_messages_flag) = @_; | |||||||||||||||||||||||||||
| 167 | return map { | ||||||||||||||||||||||||||||
| 168 | # AFAIK the full_messages_flag does nothing for model errors | ||||||||||||||||||||||||||||
| 169 | 9 | ▶ | $full_messages_flag ? $_->full_message : $_->message | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | } $self->model_errors; | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 4 | ▶ | sub attribute_errors { | ||||||||||||||||||||||||||
|
Subroutine
attribute_errors: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 174 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 175 | 4 | my @errors; | |||||||||||||||||||||||||||
| 176 | 4 | foreach my $error($self->errors->all) { | |||||||||||||||||||||||||||
| 177 | 33 | ▶ | push @errors, $error if $error->has_attribute and defined($error->attribute); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$error->has_attribute and defined $error->attribute
MC/DC50%1/2
$error->has_attribute and defined $error->attribute
$error->has_attribute
defined $error->attribute
Truth table66%2/3
A and B
A= $error->has_attributeB= defined $error->attribute
| |||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | 4 | return @errors; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 1 | ▶ | sub attribute_messages { | ||||||||||||||||||||||||||
|
Subroutine
attribute_messages: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 183 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 184 | return map { | ||||||||||||||||||||||||||||
| 185 | 1 | $_->message; | |||||||||||||||||||||||||||
| 186 | } $self->attribute_errors; | ||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 3 | ▶ | sub full_attribute_messages { | ||||||||||||||||||||||||||
|
Subroutine
full_attribute_messages: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 190 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 191 | return map { | ||||||||||||||||||||||||||||
| 192 | 3 | $_->full_message; | |||||||||||||||||||||||||||
| 193 | } $self->attribute_errors; | ||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 80 | ▶ | sub group_by_attribute { | ||||||||||||||||||||||||||
|
Subroutine
group_by_attribute: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 198 | 80 | my $self = shift; | |||||||||||||||||||||||||||
| 199 | 80 | my %attributes; | |||||||||||||||||||||||||||
| 200 | 80 | foreach my $error($self->errors->all) { | |||||||||||||||||||||||||||
| 201 | 216 | ▶ | next unless $error->has_attribute; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | 216 | ▶ | push @{$attributes{$error->attribute||'*'}}, $error; | ||||||||||||||||||||||||||
|
Condition100%2/2
$error->attribute || '*'
MC/DC100%1/1
$error->attribute || '*'
$error->attribute
Truth table100%2/2
A
A= $error->attribute
| |||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | 80 | return %attributes; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | # Returns a Hash of attributes with their error messages. If +full_messages+ | ||||||||||||||||||||||||||||
| 208 | # is +true+, it will contain full messages (see +full_message+). | ||||||||||||||||||||||||||||
| 209 | 79 | ▶ | sub to_hash { | ||||||||||||||||||||||||||
|
Subroutine
to_hash: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 210 | 79 | my ($self, $full_messages_flag) = @_; | |||||||||||||||||||||||||||
| 211 | 79 | my %hash = (); | |||||||||||||||||||||||||||
| 212 | 79 | my %grouped = $self->group_by_attribute; | |||||||||||||||||||||||||||
| 213 | 79 | foreach my $attr (keys %grouped) { | |||||||||||||||||||||||||||
| 214 | $hash{$attr} = [ | ||||||||||||||||||||||||||||
| 215 | map { | ||||||||||||||||||||||||||||
| 216 | 213 | ▶ | $full_messages_flag ? $_->full_message : $_->message | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | 155 | ▶ | } @{ $grouped{$attr}||[] } | ||||||||||||||||||||||||||
|
Condition50%1/2
$grouped{$attr} || []
MC/DC0%0/1
$grouped{$attr} || []
$grouped{$attr}
Truth table50%1/2
A
A= $grouped{$attr}
| |||||||||||||||||||||||||||||
| 218 | ]; | ||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | 79 | return %hash; | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | # Returns a Hash of attributes with their error details (see Error->detail), | ||||||||||||||||||||||||||||
| 224 | # grouped the same way as to_hash. | ||||||||||||||||||||||||||||
| 225 | 1 | ▶ | sub details { | ||||||||||||||||||||||||||
|
Subroutine
details: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 226 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 227 | 1 | my %grouped = $self->group_by_attribute; | |||||||||||||||||||||||||||
| 228 | 1 | my %details; | |||||||||||||||||||||||||||
| 229 | 1 | foreach my $attr (keys %grouped) { | |||||||||||||||||||||||||||
| 230 | 2 | ▶ | $details{$attr} = [ map { $_->detail } @{ $grouped{$attr}||[] } ]; | ||||||||||||||||||||||||||
|
Condition50%1/2
$grouped{$attr} || []
MC/DC0%0/1
$grouped{$attr} || []
$grouped{$attr}
Truth table50%1/2
A
A= $grouped{$attr}
| |||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | 1 | return %details; | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 0 | ▶ | sub as_json { | ||||||||||||||||||||||||||
|
Subroutine
as_json: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 236 | 0 | my ($self, @args) = @_; | |||||||||||||||||||||||||||
| 237 | 0 | ▶ | return $self->object->errors_as_json($self) if $self->object->can('errors_as_json'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | 0 | return $self->as_rfc_7807; | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 0 | ▶ | sub as_rfc_7807 { | ||||||||||||||||||||||||||
|
Subroutine
as_rfc_7807: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 242 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 243 | 0 | my %hash = $self->to_hash(1); | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 0 | my %rfc = (fields => {}, general => []); | |||||||||||||||||||||||||||
| 246 | 0 | foreach my $field (keys %hash) { | |||||||||||||||||||||||||||
| 247 | 0 | ▶ | if ($field eq '*') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 248 | # General errors | ||||||||||||||||||||||||||||
| 249 | 0 | push @{ $rfc{general} }, @{ $hash{$field} }; | |||||||||||||||||||||||||||
| 250 | } else { | ||||||||||||||||||||||||||||
| 251 | # Field-specific errors | ||||||||||||||||||||||||||||
| 252 | 0 | $rfc{fields}->{$field} = $hash{$field}; | |||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | 0 | return \%rfc; | |||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 0 | ▶ | sub TO_JSON { shift->as_json } | ||||||||||||||||||||||||||
|
Subroutine
TO_JSON: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | # Adds +message+ to the error messages and used validator type to +details+ on +attribute+. | ||||||||||||||||||||||||||||
| 261 | # More than one error can be added to the same +attribute+. | ||||||||||||||||||||||||||||
| 262 | 266 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 263 | 266 | my ($self, $attribute, $type, $options) = @_; | |||||||||||||||||||||||||||
| 264 | 266 | ▶ | unless(defined($type)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 265 | 1 | $type = $self->i18n->make_tag('invalid'); | |||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | 266 | ▶ | $options ||= +{}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options ||= {}
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
| |||||||||||||||||||||||||||||
| 268 | 266 | ($attribute, $type, $options) = $self->_normalize_arguments($attribute, $type, $options); | |||||||||||||||||||||||||||
| 269 | 266 | my $error = $self->build_error($attribute, $type, $options); | |||||||||||||||||||||||||||
| 270 | 266 | ▶ | if(my $exception = $options->{strict}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | 3 | my $message = $error->full_message; | |||||||||||||||||||||||||||
| 272 | 3 | ▶ | throw_exception('Strict' => (msg=>$message)) if $exception =~m/^\d+$/ && $exception == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$exception =~ /^\d+$/ and $exception == 1
MC/DC50%1/2
$exception =~ /^\d+$/ and $exception == 1
$exception =~ /^\d+$/
$exception == 1
Truth table66%2/3
A and B
A= $exception =~ /^\d+$/B= $exception == 1
| |||||||||||||||||||||||||||||
| 273 | 2 | ▶ | $exception->($self->object, $message) if( (ref($exception)||'') eq 'CODE'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
ref $exception || ''
MC/DC100%1/1
ref $exception || ''
ref $exception
Truth table100%2/2
A
A= ref $exception
| |||||||||||||||||||||||||||||
| 274 | 1 | $exception->throw($message); # If not 1 then assume its a package name or exception object. | |||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 263 | $self->errors->push($error); | |||||||||||||||||||||||||||
| 278 | 263 | return $error; | |||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | 266 | ▶ | sub build_error { | ||||||||||||||||||||||||||
|
Subroutine
build_error: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 282 | 266 | my ($self, $attribute, $type, $options) = @_; | |||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 266 | return my $error = $self->error_class | |||||||||||||||||||||||||||
| 285 | ->new( | ||||||||||||||||||||||||||||
| 286 | object => $self->object, | ||||||||||||||||||||||||||||
| 287 | attribute => $attribute, | ||||||||||||||||||||||||||||
| 288 | type => $type, | ||||||||||||||||||||||||||||
| 289 | i18n => $self->i18n, | ||||||||||||||||||||||||||||
| 290 | options => $options, | ||||||||||||||||||||||||||||
| 291 | ); | ||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | # Returns +true+ if an error on the attribute with the given message is | ||||||||||||||||||||||||||||
| 295 | # present, or +false+ otherwise. +message+ is treated the same as for +add+. ~ | ||||||||||||||||||||||||||||
| 296 | 1 | ▶ | sub added { | ||||||||||||||||||||||||||
|
Subroutine
added: called CC=4 SCAR=17.3
Pod
documented
| |||||||||||||||||||||||||||||
| 297 | 1 | my ($self, $attribute, $type, $options) = @_; | |||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | ## TODO ok so if the $attribute refers to an object which can->errors maybe we | ||||||||||||||||||||||||||||
| 300 | ## need to call $self->$attribute->errors->add(undef, $type, $options) instead | ||||||||||||||||||||||||||||
| 301 | ## so that any global errors to a nested object end in in the right place? | ||||||||||||||||||||||||||||
| 302 | ## Afterwards we need to associate the nested object errors to $self so that | ||||||||||||||||||||||||||||
| 303 | ## we know errors exist (for stuff like to_hash and all_errors, etc. | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 1 | ▶ | $type ||= $self->i18n->make_tag('invalid'); | ||||||||||||||||||||||||||
|
Condition33%1/3
$type ||= $self->i18n->make_tag("invalid")
MC/DC0%0/2
$type ||= $self->i18n->make_tag("invalid")
$type
$self->i18n->make_tag("invalid")
Truth table33%1/3
A ||= B
A= $typeB= $self->i18n->make_tag("invalid")
| |||||||||||||||||||||||||||||
| 306 | 1 | ($attribute, $type, $options) = $self->_normalize_arguments($attribute, $type, $options); | |||||||||||||||||||||||||||
| 307 | 1 | ▶ | if($self->i18n->is_i18n_tag($type)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 308 | 2 | ▶ | return $self->any(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 309 | 2 | $_->strict_match($attribute, $type, $options); | |||||||||||||||||||||||||||
| 310 | 1 | }); | |||||||||||||||||||||||||||
| 311 | } else { | ||||||||||||||||||||||||||||
| 312 | return scalar(grep { | ||||||||||||||||||||||||||||
| 313 | 0 | ▶ | $_ eq $type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 314 | } $self->messages_for($attribute)) ? 1:0 | ||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | # Similar to ->added except we don't care about options | ||||||||||||||||||||||||||||
| 319 | 3 | ▶ | sub of_kind { | ||||||||||||||||||||||||||
|
Subroutine
of_kind: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 320 | 3 | my ($self, $attribute, $type) = @_; | |||||||||||||||||||||||||||
| 321 | 3 | ▶ | $type ||= $self->i18n->make_tag('invalid'); | ||||||||||||||||||||||||||
|
Condition33%1/3
$type ||= $self->i18n->make_tag("invalid")
MC/DC0%0/2
$type ||= $self->i18n->make_tag("invalid")
$type
$self->i18n->make_tag("invalid")
Truth table33%1/3
A ||= B
A= $typeB= $self->i18n->make_tag("invalid")
| |||||||||||||||||||||||||||||
| 322 | 3 | ($attribute, $type) = $self->_normalize_arguments($attribute, $type); | |||||||||||||||||||||||||||
| 323 | 3 | ▶ | if($self->i18n->is_i18n_tag($type)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | 2 | ▶ | return $self->any(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 325 | 2 | $_->strict_match($attribute, $type); | |||||||||||||||||||||||||||
| 326 | 1 | }); | |||||||||||||||||||||||||||
| 327 | } else { | ||||||||||||||||||||||||||||
| 328 | return scalar(grep { | ||||||||||||||||||||||||||||
| 329 | 2 | ▶ | $_ eq $type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 330 | } $self->messages_for($attribute)) ? 1:0 | ||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 1 | ▶ | sub messages { map { $_->message } shift->errors->all } | ||||||||||||||||||||||||||
|
Subroutine
messages: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | 3 | ▶ | sub full_messages { | ||||||||||||||||||||||||||
|
Subroutine
full_messages: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 337 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 338 | 3 | $self->full_messages_collection->all; | |||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 3 | ▶ | sub full_messages_collection { | ||||||||||||||||||||||||||
|
Subroutine
full_messages_collection: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 342 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 343 | 3 | ▶ | return $self->errors->map(sub { $_->full_message }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | 22 | ▶ | sub full_messages_for { | ||||||||||||||||||||||||||
|
Subroutine
full_messages_for: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 348 | 22 | my ($self, $attribute) = @_; | |||||||||||||||||||||||||||
| 349 | return map { | ||||||||||||||||||||||||||||
| 350 | 22 | $_->full_message | |||||||||||||||||||||||||||
| 351 | } $self->where($attribute); | ||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 4 | ▶ | sub messages_for { | ||||||||||||||||||||||||||
|
Subroutine
messages_for: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 355 | 4 | my ($self, $attribute) = @_; | |||||||||||||||||||||||||||
| 356 | return map { | ||||||||||||||||||||||||||||
| 357 | 4 | $_->message | |||||||||||||||||||||||||||
| 358 | } $self->where($attribute) | ||||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | 0 | ▶ | sub full_message { | ||||||||||||||||||||||||||
|
Subroutine
full_message: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 362 | 0 | my ($self, $attribute, $message) = @_; | |||||||||||||||||||||||||||
| 363 | 0 | $self->error_class->full_message( | |||||||||||||||||||||||||||
| 364 | $attribute, | ||||||||||||||||||||||||||||
| 365 | $message, | ||||||||||||||||||||||||||||
| 366 | $self->object, | ||||||||||||||||||||||||||||
| 367 | $self->i18n); | ||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | 0 | ▶ | sub generate_message { | ||||||||||||||||||||||||||
|
Subroutine
generate_message: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 371 | 0 | my ($self, $attribute, $type, $options) = @_; | |||||||||||||||||||||||||||
| 372 | 0 | ▶ | $type ||= $self->i18n->make_tag('invalid'); | ||||||||||||||||||||||||||
|
Condition0%0/3
$type ||= $self->i18n->make_tag("invalid")
MC/DC0%0/2
$type ||= $self->i18n->make_tag("invalid")
$type
$self->i18n->make_tag("invalid")
Truth table0%0/3
A ||= B
A= $typeB= $self->i18n->make_tag("invalid")
| |||||||||||||||||||||||||||||
| 373 | 0 | return $self->error_class->generate_message( | |||||||||||||||||||||||||||
| 374 | $attribute, | ||||||||||||||||||||||||||||
| 375 | $type, | ||||||||||||||||||||||||||||
| 376 | $self->object, | ||||||||||||||||||||||||||||
| 377 | $options, | ||||||||||||||||||||||||||||
| 378 | $self->i18n); | ||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | 0 | ▶ | sub _dump { | ||||||||||||||||||||||||||
|
Subroutine
_dump: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 382 | 0 | require Data::Dumper; | |||||||||||||||||||||||||||
| 383 | 0 | return Data::Dumper::Dumper( +{shift->to_hash(full_messages=>1)} ); | |||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | 40 | 1; | |||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | =head1 NAME | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | Valiant::Errors - A collection of errors associated with an object | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | A collection of errors (each instances of L<Valiant::Error>) associated with attributes | ||||||||||||||||||||||||||||
| 397 | or a model. This class provides methods for adding, retrieving and introspecting | ||||||||||||||||||||||||||||
| 398 | error, typically via a L<Valiant::Validator> or L<Valiant::Validator::Each> subclass. | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | The goal of this class is to make it as easy as possible to work with and understand | ||||||||||||||||||||||||||||
| 401 | errors that have been added to your instance. In general you will never make an instance | ||||||||||||||||||||||||||||
| 402 | of this directly since it will be used via the L<Valiant::Validates> role. | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | This class defined the following attributes | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | =head2 object | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | This is a weak reference to the object which the errors belong to. | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | =head2 errors | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | This is an instance of L<Data::Perl::Collection::Array> which in a collection of | ||||||||||||||||||||||||||||
| 415 | L<Valiant::Error> objects added by validators. | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | =head2 i18n | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | The internationalization and translation class. Generally this is an instance of | ||||||||||||||||||||||||||||
| 420 | L<Valiant::I18N>. You won't need to supply this as it normally is built automatically. | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | =head1 METHODS | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | The class defines the following methods | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | =head2 count | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | =head2 size | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | The number of errors collected. If there are no errors then the size is 0. | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | =head2 empty | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | =head2 blank | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | Returns true if there are no errors collected. | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | =head2 any(\&code) | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | Accepts a coderef that will receive each error object in the collect and return true | ||||||||||||||||||||||||||||
| 441 | if any of the coderef calls return true. Used to determine if the errors collection | ||||||||||||||||||||||||||||
| 442 | contains at least one type of error. | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | my $has_invalids = $user1->errors->any(sub { | ||||||||||||||||||||||||||||
| 445 | ${\$_->type} eq 'invalid'; | ||||||||||||||||||||||||||||
| 446 | }); | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | =head2 uniq | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | Returns a flat list of the error objects in the collection with duplicates | ||||||||||||||||||||||||||||
| 451 | removed. Two errors are considered duplicates when L<Valiant::Error/equals> | ||||||||||||||||||||||||||||
| 452 | reports them equal (same object, attribute, type, and options). The first | ||||||||||||||||||||||||||||
| 453 | occurrence of each distinct error is kept. | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | my @distinct = $user1->errors->uniq; | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | =head2 copy | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | Copy an errors collection into the current (replacing any existing). The copies are | ||||||||||||||||||||||||||||
| 460 | new instances of L<Valiant::Error>, not references to the original objects. | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | =head2 import_error ($error) | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | Given a single L<Valiant::Error> import it into the current errors collection | ||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | =head2 merge ($collectio) | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | Given a L<Valiant::Errors> collection, merge it into the current one. | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | =head2 where ($attribute, $message, \%options) | ||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||
| 472 | return all the L<Valiant::Error> objects in the current collection which match criteria. | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | =head2 include ($attribute) | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | Returns +true+ if the error messages include an error for the given key | ||||||||||||||||||||||||||||
| 477 | +attribute+, +false+ otherwise. | ||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | =head2 delete ($attribute) | ||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | Delete messages for +key+. Returns the deleted messages. | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | =head2 each ($coderef) | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | Iterates through each error key, value pair in the error messages hash. | ||||||||||||||||||||||||||||
| 486 | Yields the attribute and the error for that attribute. If the attribute | ||||||||||||||||||||||||||||
| 487 | has more than one error message, yields once for each error message. | ||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | $object->errors->each(sub { | ||||||||||||||||||||||||||||
| 490 | my ($attribute, $message) = @_; | ||||||||||||||||||||||||||||
| 491 | }); | ||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | If the error is a model error then C<$attribute> will be '*'. | ||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | =head2 model_errors | ||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | Returns an array of the L<Valiant::Error> objects associated with the model as | ||||||||||||||||||||||||||||
| 498 | a whole (rather than with any single attribute). See L</model_messages> for | ||||||||||||||||||||||||||||
| 499 | the rendered message version. | ||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | =head2 model_messages | ||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||
| 503 | Returns an array of all the errors that are associated with the model (Localized | ||||||||||||||||||||||||||||
| 504 | if needed). | ||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | =head2 attribute_errors | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | Returns an array of the L<Valiant::Error> objects associated with attributes | ||||||||||||||||||||||||||||
| 509 | (rather than with the model as a whole). See L</attribute_messages> for the | ||||||||||||||||||||||||||||
| 510 | rendered message version. | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | =head2 attribute_messages | ||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | Returns an array of all the errors that are associated with attributes (localized if needed). | ||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | =head2 full_attribute_messages | ||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | Returns an array of the full messages of all attributes (localized if needed). | ||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||
| 520 | =head2 group_by_attribute | ||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||
| 522 | Returns a hash where each key is an attribute name (or '*' for the model) and | ||||||||||||||||||||||||||||
| 523 | each value is an arrayref of the L<Valiant::Error> objects for that attribute. | ||||||||||||||||||||||||||||
| 524 | This is the object-returning primitive underneath L</to_hash>. | ||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||
| 526 | =head2 to_hash (?$flag) | ||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||
| 528 | Returns a hash where each key is an attribute (or '*' for the model) and | ||||||||||||||||||||||||||||
| 529 | each value is an arrayref of errors. C<?$flag> when true will return | ||||||||||||||||||||||||||||
| 530 | the full messages for each error. | ||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | =head2 details | ||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | Returns a hash grouped the same way as L</to_hash>, but each value is an | ||||||||||||||||||||||||||||
| 535 | arrayref of the raw error details (see L<Valiant::Error/detail>) rather than | ||||||||||||||||||||||||||||
| 536 | rendered messages. Each detail is a hashref of the form | ||||||||||||||||||||||||||||
| 537 | C<< { error => $type, %options } >>, useful for programmatic inspection or | ||||||||||||||||||||||||||||
| 538 | building a machine-readable error response. | ||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||
| 540 | my %details = $object->errors->details; | ||||||||||||||||||||||||||||
| 541 | # ( | ||||||||||||||||||||||||||||
| 542 | # name => [ { error => 'too_short', count => 3 } ], | ||||||||||||||||||||||||||||
| 543 | # ) | ||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||
| 545 | =head2 add ($attribute|undef, $message, \%opts) | ||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||
| 547 | Add a new error message to the object. Error can be associated with an attribute | ||||||||||||||||||||||||||||
| 548 | or with the object itself. C<$message> can be one of: | ||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | =over 14 | ||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | =item A string | ||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||
| 554 | If a string this is the error message recorded. | ||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||
| 556 | validates name => ( | ||||||||||||||||||||||||||||
| 557 | format => 'alphabetic', | ||||||||||||||||||||||||||||
| 558 | length => [3,20], | ||||||||||||||||||||||||||||
| 559 | message => 'Unacceptable Name', | ||||||||||||||||||||||||||||
| 560 | ); | ||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||
| 562 | =item A translation tag | ||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||
| 564 | use Valiant::I18N; | ||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||
| 566 | validates name => ( | ||||||||||||||||||||||||||||
| 567 | format => 'alphabetic', | ||||||||||||||||||||||||||||
| 568 | length => [3,20], | ||||||||||||||||||||||||||||
| 569 | message => _t('bad_name'), | ||||||||||||||||||||||||||||
| 570 | ); | ||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||
| 572 | This will look up a translated version of the tag. See L<Valiant::I18N> for more details. | ||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||
| 574 | =item A scalar reference | ||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||
| 576 | validates name => ( | ||||||||||||||||||||||||||||
| 577 | format => 'alphabetic', | ||||||||||||||||||||||||||||
| 578 | length => [3,20], | ||||||||||||||||||||||||||||
| 579 | message => \'Unacceptable {{attribute}}', | ||||||||||||||||||||||||||||
| 580 | ); | ||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||
| 582 | Similar to string but we will expand any placeholder variables which are indicated by the | ||||||||||||||||||||||||||||
| 583 | '{{' and '}}' tokens (which are removed from the final string). You can use any placeholder | ||||||||||||||||||||||||||||
| 584 | that is a key in the options hash (and you can pass additional values when you add an error). | ||||||||||||||||||||||||||||
| 585 | By default the following placeholder expansions are available attribute (the attribute name), | ||||||||||||||||||||||||||||
| 586 | value (the current attribute value), model (the human name of the model containing the | ||||||||||||||||||||||||||||
| 587 | attribute and object (the actual object instance that has the error). | ||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||
| 589 | =item A subroutine reference | ||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||
| 591 | validates name => ( | ||||||||||||||||||||||||||||
| 592 | format => 'alphabetic', | ||||||||||||||||||||||||||||
| 593 | length => [3,20], | ||||||||||||||||||||||||||||
| 594 | message => sub { | ||||||||||||||||||||||||||||
| 595 | my ($self, $attr, $value, $opts) = @_; | ||||||||||||||||||||||||||||
| 596 | return "Unacceptable $attr!"; | ||||||||||||||||||||||||||||
| 597 | } | ||||||||||||||||||||||||||||
| 598 | ); | ||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||
| 600 | Similar to the scalar reference option just more flexible since you can write custom code | ||||||||||||||||||||||||||||
| 601 | to build the error message. For example you could return different error messages based on | ||||||||||||||||||||||||||||
| 602 | the identity of a person. Also if you return a translation tag instead of a simple string | ||||||||||||||||||||||||||||
| 603 | we will attempt to resolve it to a translated string (see L<Valiant::I18N>). | ||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||
| 605 | =back | ||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | =head2 added ($attribute|undef, $message, \%opts) | ||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||
| 609 | Return true if the error has already been created. | ||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||
| 611 | =head2 of_kind ($attribute|undef, $message) | ||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||
| 613 | Similar to <added> expect we don't need to match options | ||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | =head2 messages | ||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||
| 617 | An array of the error messages. All translation tags will be translated to strings in the | ||||||||||||||||||||||||||||
| 618 | expected local langauge. | ||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||
| 620 | =head2 full_messages | ||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||
| 622 | An array of the full messages (localized if needed). | ||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||
| 624 | =head2 messages_for ($attribute) | ||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||
| 626 | An array of all the messages for the given attribute (localized if needed). | ||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||
| 628 | =head2 full_messages_for ($attribute) | ||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||
| 630 | An array of all the full messages for the given attribute (localized if needed). | ||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||
| 632 | =head2 as_rfc_7807 | ||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||
| 634 | Returns a hashref suitable for contstructing an error response compatible with | ||||||||||||||||||||||||||||
| 635 | RFC-7807, an open standard for returning errors formated in JSON from web APIs. | ||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||
| 637 | Example response: | ||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||
| 639 | { | ||||||||||||||||||||||||||||
| 640 | fields => { | ||||||||||||||||||||||||||||
| 641 | attendees => [ | ||||||||||||||||||||||||||||
| 642 | "Attendees Are Invalid", | ||||||||||||||||||||||||||||
| 643 | ], | ||||||||||||||||||||||||||||
| 644 | "attendees[0].role" => [ | ||||||||||||||||||||||||||||
| 645 | "Attendees Role is too short (minimum is 2 characters)", | ||||||||||||||||||||||||||||
| 646 | ], | ||||||||||||||||||||||||||||
| 647 | "attendees[1].role" => [ | ||||||||||||||||||||||||||||
| 648 | "Attendees Role is too short (minimum is 2 characters)", | ||||||||||||||||||||||||||||
| 649 | ], | ||||||||||||||||||||||||||||
| 650 | }, | ||||||||||||||||||||||||||||
| 651 | general => [ | ||||||||||||||||||||||||||||
| 652 | "A model error", | ||||||||||||||||||||||||||||
| 653 | ], | ||||||||||||||||||||||||||||
| 654 | } | ||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||
| 656 | You can use this to construct the C<errors> field of a compliant response. Here's how | ||||||||||||||||||||||||||||
| 657 | A full response might look like encoded in JSON. | ||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||
| 659 | { | ||||||||||||||||||||||||||||
| 660 | "type": "https://example.com/probs/validation-error", | ||||||||||||||||||||||||||||
| 661 | "title": "Validation Error", | ||||||||||||||||||||||||||||
| 662 | "status": 400, | ||||||||||||||||||||||||||||
| 663 | "detail": "There are validation errors in your request.", | ||||||||||||||||||||||||||||
| 664 | "errors": { | ||||||||||||||||||||||||||||
| 665 | "fields": { | ||||||||||||||||||||||||||||
| 666 | "attendees": [ | ||||||||||||||||||||||||||||
| 667 | "Attendees Are Invalid" | ||||||||||||||||||||||||||||
| 668 | ], | ||||||||||||||||||||||||||||
| 669 | "attendees[0].role": [ | ||||||||||||||||||||||||||||
| 670 | "Attendees Role is too short (minimum is 2 characters)" | ||||||||||||||||||||||||||||
| 671 | ], | ||||||||||||||||||||||||||||
| 672 | "attendees[1].role": [ | ||||||||||||||||||||||||||||
| 673 | "Attendees Role is too short (minimum is 2 characters)" | ||||||||||||||||||||||||||||
| 674 | ] | ||||||||||||||||||||||||||||
| 675 | }, | ||||||||||||||||||||||||||||
| 676 | "general": [ | ||||||||||||||||||||||||||||
| 677 | "A model error" | ||||||||||||||||||||||||||||
| 678 | ] | ||||||||||||||||||||||||||||
| 679 | } | ||||||||||||||||||||||||||||
| 680 | } | ||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||
| 682 | =head1 JSONification | ||||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||
| 684 | This class provides a C<TO_JSON> method suitable for use in some of the common | ||||||||||||||||||||||||||||
| 685 | JSON serializers. When supported it will delegate the job of turning the object | ||||||||||||||||||||||||||||
| 686 | into a hash that can be serialized to JSON to the C<as_rfc_7807> method. | ||||||||||||||||||||||||||||
| 687 | |||||||||||||||||||||||||||||
| 688 | If you wish to overide this, you can have your model object provide a method | ||||||||||||||||||||||||||||
| 689 | C<errors_as_json> which will recieve the errors object as an argument and then | ||||||||||||||||||||||||||||
| 690 | you can format it as you wish. This method should return a hash or array reference | ||||||||||||||||||||||||||||
| 691 | that can be passed to one of the standard C<JSON> encoders. | ||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||
| 693 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 694 | |||||||||||||||||||||||||||||
| 695 | L<Valiant>, L<Valiant::Error>. | ||||||||||||||||||||||||||||
| 696 | |||||||||||||||||||||||||||||
| 697 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||
| 699 | See L<Valiant> | ||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||
| 701 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 702 | |||||||||||||||||||||||||||||
| 703 | See L<Valiant> | ||||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||
| 705 | =cut | ||||||||||||||||||||||||||||
| 706 | |||||||||||||||||||||||||||||
| 707 | 40 | 1; | |||||||||||||||||||||||||||