| 1 | package JSON::Validator::Error; | ||||||||||||||||||||||||||||
| 2 | 82 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 82 | ▶ | use overload q("") => \&to_string, bool => sub {1}, fallback => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 82 | our $MESSAGES = { | |||||||||||||||||||||||||||
| 7 | allOf => {type => '/allOf Expected %3 - got %4.'}, | ||||||||||||||||||||||||||||
| 8 | anyOf => {type => '/anyOf Expected %3 - got %4.'}, | ||||||||||||||||||||||||||||
| 9 | array => { | ||||||||||||||||||||||||||||
| 10 | additionalItems => 'Invalid number of items: %3/%4.', | ||||||||||||||||||||||||||||
| 11 | maxContains => 'Contains too many items: %3/%4.', | ||||||||||||||||||||||||||||
| 12 | maxItems => 'Too many items: %3/%4.', | ||||||||||||||||||||||||||||
| 13 | minContains => 'Contains not enough items: %3/%4.', | ||||||||||||||||||||||||||||
| 14 | minItems => 'Not enough items: %3/%4.', | ||||||||||||||||||||||||||||
| 15 | uniqueItems => 'Unique items required.', | ||||||||||||||||||||||||||||
| 16 | contains => 'No items contained.', | ||||||||||||||||||||||||||||
| 17 | }, | ||||||||||||||||||||||||||||
| 18 | const => {const => 'Does not match const: %3.'}, | ||||||||||||||||||||||||||||
| 19 | enum => {enum => 'Not in enum list: %3.'}, | ||||||||||||||||||||||||||||
| 20 | integer => { | ||||||||||||||||||||||||||||
| 21 | ex_maximum => '%3 >= maximum(%4)', | ||||||||||||||||||||||||||||
| 22 | ex_minimum => '%3 <= minimum(%4)', | ||||||||||||||||||||||||||||
| 23 | maximum => '%3 > maximum(%4)', | ||||||||||||||||||||||||||||
| 24 | minimum => '%3 < minimum(%4)', | ||||||||||||||||||||||||||||
| 25 | multipleOf => 'Not multiple of %3.', | ||||||||||||||||||||||||||||
| 26 | }, | ||||||||||||||||||||||||||||
| 27 | not => {not => 'Should not match.'}, | ||||||||||||||||||||||||||||
| 28 | null => {type => 'Not null.'}, | ||||||||||||||||||||||||||||
| 29 | number => { | ||||||||||||||||||||||||||||
| 30 | ex_maximum => '%3 >= maximum(%4)', | ||||||||||||||||||||||||||||
| 31 | ex_minimum => '%3 <= minimum(%4)', | ||||||||||||||||||||||||||||
| 32 | maximum => '%3 > maximum(%4)', | ||||||||||||||||||||||||||||
| 33 | minimum => '%3 < minimum(%4)', | ||||||||||||||||||||||||||||
| 34 | multipleOf => 'Not multiple of %3.', | ||||||||||||||||||||||||||||
| 35 | }, | ||||||||||||||||||||||||||||
| 36 | object => { | ||||||||||||||||||||||||||||
| 37 | additionalProperties => 'Properties not allowed: %3.', | ||||||||||||||||||||||||||||
| 38 | maxProperties => 'Too many properties: %3/%4.', | ||||||||||||||||||||||||||||
| 39 | minProperties => 'Not enough properties: %3/%4.', | ||||||||||||||||||||||||||||
| 40 | required => 'Missing property.', | ||||||||||||||||||||||||||||
| 41 | dependencies => 'Missing property. Dependee: %3.', | ||||||||||||||||||||||||||||
| 42 | }, | ||||||||||||||||||||||||||||
| 43 | oneOf => { | ||||||||||||||||||||||||||||
| 44 | all_rules_match => 'All of the oneOf rules match.', | ||||||||||||||||||||||||||||
| 45 | n_rules_match => 'oneOf rules %3 match.', | ||||||||||||||||||||||||||||
| 46 | type => '/oneOf Expected %3 - got %4.', | ||||||||||||||||||||||||||||
| 47 | }, | ||||||||||||||||||||||||||||
| 48 | string => { | ||||||||||||||||||||||||||||
| 49 | pattern => 'String does not match %3.', | ||||||||||||||||||||||||||||
| 50 | maxLength => 'String is too long: %3/%4.', | ||||||||||||||||||||||||||||
| 51 | minLength => 'String is too short: %3/%4.', | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | }; | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | has details => sub { [qw(generic generic)] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 456 | ▶ | has message => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=23.7
| |||||||||||||||||||||||||||||
| 58 | 456 | my $self = shift; | |||||||||||||||||||||||||||
| 59 | 456 | my $details = $self->details; | |||||||||||||||||||||||||||
| 60 | 456 | my $message; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 456 | ▶ | if (($details->[0] || '') eq 'format') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
Condition50%1/2
$details->[0] || ''
MC/DC0%0/1
$details->[0] || ''
$details->[0]
Truth table50%1/2
A
A= $details->[0]
| |||||||||||||||||||||||||||||
| 63 | 67 | $message = '%3'; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | ▶ | elsif (($details->[1] || '') eq 'type' and @$details == 3) { | |||||||||||||||||||||||||||
|
Condition80%4/5
($details->[1] || "") eq "type" and @$details == 3
$details->[1] || ''
MC/DC50%1/2
($details->[1] || "") eq "type" and @$details == 3
$details->[1]
@$details == 3
Truth table66%2/3
A and B
A= $details->[1]B= @$details == 3
| |||||||||||||||||||||||||||||
| 66 | 99 | $message = 'Expected %1 - got %3.'; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | elsif (my $group = $MESSAGES->{$details->[0]}) { | ||||||||||||||||||||||||||||
| 69 | 290 | ▶ | $message = $group->{$details->[1] || 'default'}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$details->[1] || 'default'
MC/DC0%0/1
$details->[1] || 'default'
$details->[1]
Truth table50%1/2
A
A= $details->[1]
| |||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 456 | ▶ | return join ' ', Failed => @$details unless defined $message; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 456 | ▶ | $message =~ s!\%(\d)\b!{$details->[$1 - 1] // ''}!ge; | ||||||||||||||||||||||||||
|
Condition50%1/2
$$details[$1 - 1] // ""
MC/DC0%0/1
$$details[$1 - 1] // ""
$$details[$1 - 1]
Truth table50%1/2
A
A= $$details[$1 - 1]
| |||||||||||||||||||||||||||||
| 75 | 456 | return $message; | |||||||||||||||||||||||||||
| 76 | 82 | }; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 82 | has path => '/'; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 5206 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 5206 | my $class = shift; | |||||||||||||||||||||||||||
| 82 | 5206 | ▶ | return $class->SUPER::new unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # Constructed with attributes | ||||||||||||||||||||||||||||
| 85 | 5204 | ▶ | return $class->SUPER::new($_[0]) if ref $_[0] eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # Constructed with ($path, ...) | ||||||||||||||||||||||||||||
| 88 | 5086 | my $self = $class->SUPER::new; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 5086 | ▶ | my $path = ref $_[0] ? join '/', '', map { s!~!~0!g; s!/!~1!g; $_ } @{shift(@_)} : shift || '/'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
shift() or "/"
MC/DC0%0/1
shift() or "/"
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 91 | 5086 | ▶ | $self->{path} = $path || '/'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$path || '/'
MC/DC100%1/1
$path || '/'
$path
Truth table100%2/2
A
A= $path
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # Constructed with ($path, $message) or ($path, \@details) | ||||||||||||||||||||||||||||
| 94 | 5086 | ▶ | return !@_ ? $self : ref $_[0] ? $self->details(shift) : $self->message(shift); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 464 | ▶ | sub to_string { sprintf '%s: %s', $_[0]->path, $_[0]->message } | ||||||||||||||||||||||||||
|
Subroutine
to_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 753 | ▶ | sub TO_JSON { {message => $_[0]->message, path => $_[0]->path} } | ||||||||||||||||||||||||||
|
Subroutine
TO_JSON: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 82 | 1; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =encoding utf8 | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head1 NAME | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | JSON::Validator::Error - JSON::Validator error object | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | use JSON::Validator::Error; | ||||||||||||||||||||||||||||
| 111 | my $err = JSON::Validator::Error->new($path, $message); | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | L<JSON::Validator::Error> is a class representing validation errors from | ||||||||||||||||||||||||||||
| 116 | L<JSON::Validator>. | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =head2 details | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | my $error = $error->details(["generic", "generic"]); | ||||||||||||||||||||||||||||
| 123 | my $error = $error->details([qw(array type object)]); | ||||||||||||||||||||||||||||
| 124 | my $error = $error->details([qw(format date-time Invalid)]); | ||||||||||||||||||||||||||||
| 125 | my $array_ref = $error->details; | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | Details about the error: | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =over 2 | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =item 1. | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | Often the category of tests that was run. Example values: allOf, anyOf, array, | ||||||||||||||||||||||||||||
| 134 | const, enum, format, integer, not, null, number, object, oneOf and string. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =item 2. | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | Often the test that failed. Example values: additionalItems, | ||||||||||||||||||||||||||||
| 139 | additionalProperties, const, enum, maxItems, maxLength, maxProperties, maximum, | ||||||||||||||||||||||||||||
| 140 | minItems, minLength. minProperties, minimum, multipleOf, not, null, pattern, | ||||||||||||||||||||||||||||
| 141 | required, type and uniqueItems, | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =item 3. | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | The rest of the list contains parameters for the test that failed. It can be a | ||||||||||||||||||||||||||||
| 146 | plain human-readable string or numbers indicating things such as max/min | ||||||||||||||||||||||||||||
| 147 | values. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =back | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =head2 message | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | my $str = $error->message; | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | A human readable description of the error. Defaults to being being constructed | ||||||||||||||||||||||||||||
| 156 | from L</details>. See the C<$MESSAGES> variable in the source code for more | ||||||||||||||||||||||||||||
| 157 | details. | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | As an EXPERIMENTAL hack you can localize C<$JSON::Validator::Error::MESSAGES> | ||||||||||||||||||||||||||||
| 160 | to get i18n support. Example: | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | sub validate_i18n { | ||||||||||||||||||||||||||||
| 163 | local $JSON::Validator::Error::MESSAGES = { | ||||||||||||||||||||||||||||
| 164 | allOf => {type => '/allOf Forventet %3 - fikk %4.'}, | ||||||||||||||||||||||||||||
| 165 | }; | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | my @error_norwegian = $jv->validate({age => 42}); | ||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | Note that the error messages might contain a mix of English and the local | ||||||||||||||||||||||||||||
| 171 | language. Run some tests to see how it looks. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head2 path | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | my $str = $error->path; | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | A JSON pointer to where the error occurred. Defaults to "/". | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head1 METHODS | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =head2 new | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | my $error = JSON::Validator::Error->new(\%attributes); | ||||||||||||||||||||||||||||
| 184 | my $error = JSON::Validator::Error->new($path, \@details); | ||||||||||||||||||||||||||||
| 185 | my $error = JSON::Validator::Error->new($path, \@details); | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | Object constructor. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =head2 to_string | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | my $str = $error->to_string; | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | Returns the "path" and "message" part as a string: "$path: $message". | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =head1 OPERATORS | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | L<JSON::Validator::Error> overloads the following operators: | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =head2 bool | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | my $bool = !!$error; | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | Always true. | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =head2 stringify | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | my $str = "$error"; | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | Alias for L</to_string>. | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | L<JSON::Validator>. | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =cut | ||||||||||||||||||||||||||||