Yancy/Model/Schema.pm

filter: Statementstmt 96.3% 106 / 110 Branchbran 94.1% 32 / 34 Conditioncond 88.1% 37 / 42 MC/DCmcdc 69.5% 16 / 23 Subroutinesub 84.2% 16 / 19 Podpod 100% 9 / 9 totaltotal 91.1% 216 / 237 CC 36 SCAR 23.8
CC
36
Cov
94%
validate
21.2
_check_json_schema
10.0
create
3.0
CRAP 10.8
« Yancy/Backend/Memory.pm ↑ Summary Yancy/Plugin/File.pm »
1 package Yancy::Model::Schema;
2 21 our $VERSION = '1.088';
3 # ABSTRACT: Interface to a single schema
4  
5 #pod =head1 SYNOPSIS
6 #pod
7 #pod my $schema = $app->model->schema( 'foo' );
8 #pod
9 #pod my $id = $schema->create( $data );
10 #pod my $item = $schema->get( $id );
11 #pod my $count = $schema->delete( $id );
12 #pod my $count = $schema->delete( $where );
13 #pod my $count = $schema->set( $id, $data );
14 #pod my $count = $schema->set( $where, $data );
15 #pod
16 #pod my $res = $schema->list( $where, $opts );
17 #pod for my $item ( @{ $res->{items} } ) { ... }
18 #pod
19 #pod =head1 DESCRIPTION
20 #pod
21 #pod B<NOTE>: This module is experimental and its API may change before
22 #pod Yancy v2!
23 #pod
24 #pod For information on how to extend this module to add your own schema
25 #pod and item methods, see L<Yancy::Guides::Model>.
26 #pod
27 #pod =head1 SEE ALSO
28 #pod
29 #pod L<Yancy::Guides::Model>, L<Yancy::Model>
30 #pod
31 #pod =cut
32  
33 21 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 21 ▶use Mojo::JSON qw( true false );
Subroutine BEGIN: called CC=1 SCAR=0.0
35 21 ▶use Yancy::Util qw( json_validator is_type derp );
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 #pod =attr model
38 #pod
39 #pod The L<Yancy::Model> object that created this schema object.
40 #pod
41 #pod =cut
42  
43 0 ▶has model => sub { die 'model is required' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
44  
45 #pod =attr name
46 #pod
47 #pod The name of the schema.
48 #pod
49 #pod =cut
50  
51 0 ▶has name => sub { die 'name is required' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
52  
53 #pod =attr json_schema
54 #pod
55 #pod The JSON Schema for this schema.
56 #pod
57 #pod =cut
58  
59 0 ▶has json_schema => sub { die 'json_schema is required' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
60  
61 275 ▶sub _backend { shift->model->backend };
Subroutine _backend: called CC=1 SCAR=0.0
62 26 ▶has _item_class => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
63 26 my $self = shift;
64 26 return $self->model->find_class( Item => $self->name );
65 21 };
66 8 ▶sub _log { shift->model->log };
Subroutine _log: called CC=1 SCAR=0.0
67  
68 575 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
69 575 my ( $class, @args ) = @_;
70 575 my $self = $class->SUPER::new( @args );
71 575 $self->_check_json_schema;
72 573 return $self;
73 }
74  
75 #pod =method id_field
76 #pod
77 #pod The ID field for this schema. Either a single string, or an arrayref of
78 #pod strings (for composite keys).
79 #pod
80 #pod =cut
81  
82 1359 ▶sub id_field {
Subroutine id_field: called CC=2 SCAR=6.9
Pod documented
83 1359 my ( $self ) = @_;
84 1359 ▶ return $self->json_schema->{'x-id-field'} // 'id';
Condition100%2/2
$self->json_schema->{"x-id-field"} // "id"
l!l
847512
MC/DC100%1/1
$self->json_schema->{"x-id-field"} // "id"
$self->json_schema->{"x-id-field"}
Truth table100%2/2
A
A= $self->json_schema->{"x-id-field"}
Aresult
11
00
85 }
86  
87 #pod =method build_item
88 #pod
89 #pod Turn a hashref of row data into a L<Yancy::Model::Item> object using
90 #pod L<Yancy::Model/find_class> to find the correct class.
91 #pod
92 #pod =cut
93  
94 235 ▶sub build_item {
Subroutine build_item: called CC=1 SCAR=0.0
Pod documented
95 235 my ( $self, $data ) = @_;
96 235 return $self->_item_class->new( { data => $data, schema => $self } );
97 }
98  
99 #pod =method validate
100 #pod
101 #pod Validate an item. Returns a list of errors (if any).
102 #pod
103 #pod =cut
104  
105 76 ▶sub validate {
Subroutine validate: called CC=21 SCAR=30.5
Pod documented
106 76 my ( $self, $item, %opt ) = @_;
107 76 my $schema = $self->json_schema;
108  
109 76 ▶ if ( $opt{ properties } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"properties"}) 35 41 76
110 # Only validate these properties
111 $schema = {
112 type => 'object',
113 required => [
114 135 ▶ grep { my $f = $_; grep { $_ eq $f } @{ $schema->{required} || [] } }
Condition50%1/2
$schema->{'required'} || []
l!l
1350
MC/DC0%0/1
$schema->{'required'} || []
$schema->{'required'}
Truth table50%1/2
A
A= $schema->{'required'}
Aresult
11
00
115 35 @{ $opt{ properties } }
116 ],
117 properties => {
118 134 map { $_ => $schema->{properties}{$_} }
119 135 grep { exists $schema->{properties}{$_} }
120 35 @{ $opt{ properties } }
121 },
122 additionalProperties => 0, # Disallow any other properties
123 };
124 }
125  
126 76 my $v = json_validator();
127 76 $v->schema( $schema );
128  
129 76 my @errors;
130 # This is a shallow copy of the item that we will change to pass
131 # Yancy-specific additions to schema validation
132 76 my %check_item = %$item;
133 76 for my $prop_name ( keys %{ $schema->{properties} } ) {
134 438 my $prop = $schema->{properties}{ $prop_name };
135  
136 # These blocks fix problems with validation only. If the
137 # problem is the database understanding the value, it must be
138 # fixed in the backend class.
139  
140 # Pre-filter booleans
141 438 ▶ if ( is_type( $prop->{type}, 'boolean' ) && defined $check_item{ $prop_name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_type($prop->{"type"}, "boolean") and defined $check_item{$prop_name}) 20 418 438
Condition100%3/3
is_type($prop->{"type"}, "boolean") and defined $check_item{$prop_name}
!ll&&!rl&&r
413520
MC/DC100%2/2
is_type($prop->{"type"}, "boolean") and defined $check_item{$prop_name}
is_type($prop->{"type"}, "boolean") defined $check_item{$prop_name}
Truth table100%3/3
A and B
A= is_type($prop->{"type"}, "boolean")B= defined $check_item{$prop_name}
ABresult
0X0
100
111
142 20 my $value = $check_item{ $prop_name };
143 20 ▶ if ( $value eq 'false' or !$value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq 'false' or not $value) { } 12 8 20
Condition100%3/3
$value eq "false" or not $value
l!l&&r!l&&!r
2108
MC/DC100%2/2
$value eq "false" or not $value
$value eq "false" not $value
Truth table100%3/3
A or not(B)
A= $value eq "false"B= not $value
ABresult
1X1
011
000
144 12 $value = false;
145 } else {
146 8 $value = true;
147 }
148 20 $check_item{ $prop_name } = $value;
149 }
150 # An empty date-time, date, or time must become undef: The empty
151 # string will never pass the format check, but properties that
152 # are allowed to be null can be validated.
153 438 ▶ if ( is_type( $prop->{type}, 'string' ) && $prop->{format} && $prop->{format} =~ /^(?:date-time|date|time)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_type($prop->{"type"}, "string") and $prop->{"format"} and $prop->{"format"} =~ /^(?:date-time|date|time)$/u) 24 414 438
Condition100%6/6
is_type($prop->{"type"}, "string") and $prop->{"format"} and $prop->{"format"} =~ /^(?:date-time|date|time)$/u
!ll&&!rl&&r
3357924
is_type($prop->{"type"}, "string") and $prop->{"format"}
!ll&&!rl&&r
117218103
MC/DC100%3/3
is_type($prop->{"type"}, "string") and $prop->{"format"} and $prop->{"format"} =~ /^(?:date-time|date|time)$/u
is_type($prop->{"type"}, "string") $prop->{"format"} $prop->{"format"} =~ /^(?:date-time|date|time)$/u
Truth table100%4/4
A and B and C
A= is_type($prop->{"type"}, "string")B= $prop->{"format"}C= $prop->{"format"} =~ /^(?:date-time|date|time)$/u
ABCresult
0XX0
10X0
1100
1111
154 24 ▶ if ( exists $check_item{ $prop_name } && !$check_item{ $prop_name } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $check_item{$prop_name} and not $check_item{$prop_name}) { } 1 23 24
elsif ((($check_item{$prop_name} // $prop->{'default'}) // '') eq 'now') { } 20 3 23
Condition100%3/3
exists $check_item{$prop_name} and not $check_item{$prop_name}
!ll&&!rl&&r
1491
MC/DC100%2/2
exists $check_item{$prop_name} and not $check_item{$prop_name}
exists $check_item{$prop_name} not $check_item{$prop_name}
Truth table100%3/3
A and not(B)
A= exists $check_item{$prop_name}B= not $check_item{$prop_name}
ABresult
0X0
100
111
155 1 $check_item{ $prop_name } = undef;
156 }
157 # The "now" special value will not validate yet, but will be
158 # replaced by the Backend with something useful
159 ▶ elsif ( ($check_item{ $prop_name }//$prop->{default}//'') eq 'now' ) {
Condition60%3/5
($check_item{$prop_name} // $prop->{"default"}) // ""
l!l
230
$check_item{$prop_name} // $prop->{"default"}
l!l&&r!l&&!r
9140
MC/DC0%0/2
($check_item{$prop_name} // $prop->{"default"}) // ""
$check_item{$prop_name} $prop->{"default"}
Truth table66%2/3
A // B
A= $check_item{$prop_name}B= $prop->{"default"}
ABresult
1X1
011
000
160 20 $check_item{ $prop_name } = '2021-01-01 00:00:00';
161 }
162 }
163 # Always add dummy passwords to pass required checks
164 438 ▶ if ( $prop->{format} && $prop->{format} eq 'password' && !$check_item{ $prop_name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prop->{"format"} and $prop->{"format"} eq "password" and not $check_item{$prop_name}) 0 438 438
Condition83%5/6
$prop->{"format"} and $prop->{"format"} eq "password" and not $check_item{$prop_name}
!ll&&!rl&&r
419190
$prop->{"format"} and $prop->{"format"} eq "password"
!ll&&!rl&&r
3358419
MC/DC0%0/3
$prop->{"format"} and $prop->{"format"} eq "password" and not $check_item{$prop_name}
$prop->{"format"} $prop->{"format"} eq "password" not $check_item{$prop_name}
Truth table75%3/4
A and B and not(C)
A= $prop->{"format"}B= $prop->{"format"} eq "password"C= not $check_item{$prop_name}
ABCresult
0XX0
10X0
1100
1111
165 0 $check_item{ $prop_name } = '<PASSWORD>';
166 }
167  
168 # XXX: JSON::Validator 4 moved support for readOnly/writeOnly to
169 # the OpenAPI schema classes, but we use JSON Schema internally,
170 # so we need to make support ourselves for now...
171 438 ▶ if ( $prop->{readOnly} && exists $check_item{ $prop_name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prop->{"readOnly"} and exists $check_item{$prop_name}) 1 437 438
Condition100%3/3
$prop->{"readOnly"} and exists $check_item{$prop_name}
!ll&&!rl&&r
399381
MC/DC100%2/2
$prop->{"readOnly"} and exists $check_item{$prop_name}
$prop->{"readOnly"} exists $check_item{$prop_name}
Truth table100%3/3
A and B
A= $prop->{"readOnly"}B= exists $check_item{$prop_name}
ABresult
0X0
100
111
172 1 push @errors, JSON::Validator::Error->new(
173 "/$prop_name", "Read-only.",
174 );
175 }
176 }
177  
178 76 push @errors, $v->validate( \%check_item );
179 76 return @errors;
180 }
181  
182 #pod =method get
183 #pod
184 #pod Get an item by its ID. Returns a L<Yancy::Model::Item> object.
185 #pod
186 #pod =cut
187  
188 117 ▶sub get {
Subroutine get: called CC=2 SCAR=6.9
Pod documented
189 117 my ( $self, $id, %opt ) = @_;
190 117 ▶ return $self->build_item( $self->_backend->get( $self->name, $id, %opt ) // return undef );
Condition100%2/2
$self->_backend->get($self->name, $id, %opt) // (return undef)
l!l
10611
MC/DC100%1/1
$self->_backend->get($self->name, $id, %opt) // (return undef)
$self->_backend->get($self->name, $id, %opt)
Truth table100%2/2
A
A= $self->_backend->get($self->name, $id, %opt)
Aresult
11
00
191 }
192  
193 #pod =method list
194 #pod
195 #pod List items. Returns a hash reference with C<items> and C<total> keys. The C<items> is
196 #pod an array ref of L<Yancy::Model::Item> objects. C<total> is the total number of items
197 #pod that would be returned without any C<offset> or C<limit> options.
198 #pod
199 #pod =cut
200  
201 68 ▶sub list {
Subroutine list: called CC=1 SCAR=0.0
Pod documented
202 68 my ( $self, $where, $opt ) = @_;
203 68 my $res = $self->_backend->list( $self->name, $where, $opt );
204 68 return { items => [ map { $self->build_item( $_ ) } @{ $res->{items} } ], total => $res->{total} };
205 }
206  
207 #pod =method create
208 #pod
209 #pod Create a new item. Returns the ID of the created item.
210 #pod
211 #pod =cut
212  
213 41 ▶sub create {
Subroutine create: called CC=3 SCAR=11.0
Pod documented
214 41 my ( $self, $data ) = @_;
215 41 ▶ if ( my @errors = $self->validate( $data ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@errors) = $self->validate($data)) 2 39 41
216 2 $self->_log->error(
217 sprintf 'Error validating new item in schema "%s": %s',
218 $self->name,
219 join ', ', @errors
220 );
221 2 die \@errors; # XXX: Throw an exception instead that can stringify to something useful
222 }
223 39 my $retval = eval { $self->_backend->create( $self->name, $data ) };
224 39 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 2 37 39
225 2 $self->_log->error(
226 sprintf 'Error creating item in schema "%s": %s',
227 $self->name, $error,
228 );
229 2 die $error;
230 }
231 37 return $retval;
232 }
233  
234 #pod =method set
235 #pod
236 #pod Set the given fields in an item. See also L<Yancy::Model::Item/set>.
237 #pod
238 #pod =cut
239  
240 35 ▶sub set {
Subroutine set: called CC=3 SCAR=11.0
Pod documented
241 35 my ( $self, $id, $data ) = @_;
242 35 ▶ if ( my @errors = $self->validate( $data, properties => [ keys %$data ] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@errors) = $self->validate($data, "properties", [keys %$data])) 2 33 35
243 2 $self->_log->error(
244 sprintf 'Error validating item with ID "%s" in schema "%s": %s',
245 $id, $self->name,
246 join ', ', @errors
247 );
248 2 die \@errors; # XXX: Throw an exception instead that can stringify to something useful
249 }
250 33 my $retval = eval { $self->_backend->set( $self->name, $id, $data ) };
251 33 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 2 31 33
252 2 $self->_log->error(
253 sprintf 'Error setting item with ID "%s" in schema "%s": %s',
254 $id, $self->name, $error,
255 );
256 2 die $error;
257 }
258 31 return $retval;
259 }
260  
261 #pod =method delete
262 #pod
263 #pod Delete an item. See also L<Yancy::Model::Item/delete>.
264 #pod
265 #pod =cut
266  
267 18 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
268 18 my ( $self, $id ) = @_;
269 # XXX: Use get() to get the item instance first? Then they could
270 # override delete() to do things...
271 18 return $self->_backend->delete( $self->name, $id );
272 }
273  
274 867 ▶sub _check_json_schema {
Subroutine _check_json_schema: called CC=10 SCAR=23.0
275 867 my ( $self ) = @_;
276 867 my $name = $self->name;
277 867 my $json_schema = $self->json_schema;
278  
279 # Deprecate x-view. Yancy::Model is a much better
280 # solution to that.
281 derp q{x-view is deprecated and will be removed in v2. }
282 . q{Use Yancy::Model or your database's CREATE VIEW instead}
283 867 ▶ if $json_schema->{'x-view'};
Branch: 1 branch
BranchTrueFalseTotal
if $json_schema->{"x-view"} 232 635 867
284  
285 867 ▶ $json_schema->{ type } //= 'object';
Condition100%2/2
$json_schema->{"type"} //= "object"
l!l
8598
MC/DC100%1/1
$json_schema->{"type"} //= "object"
$json_schema->{"type"}
Truth table100%2/2
A
A= $json_schema->{"type"}
Aresult
11
00
286 867 my $props = $json_schema->{properties};
287 867 ▶ if ( $json_schema->{'x-view'} && !$props ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json_schema->{"x-view"} and not $props) 116 751 867
Condition100%3/3
$json_schema->{"x-view"} and not $props
!ll&&!rl&&r
635116116
MC/DC100%2/2
$json_schema->{"x-view"} and not $props
$json_schema->{"x-view"} not $props
Truth table100%3/3
A and not(B)
A= $json_schema->{"x-view"}B= not $props
ABresult
0X0
100
111
288 116 my $real_name = $json_schema->{'x-view'}->{schema};
289 116 ▶ my $real_schema = $self->model->schema( $real_name )
Condition50%1/2
$self->model->schema($real_name) // die(qq[Could not find x-view schema "$real_name" for schema "$name"])
l!l
1160
MC/DC0%0/1
$self->model->schema($real_name) // die(qq[Could not find x-view schema "$real_name" for schema "$name"])
$self->model->schema($real_name)
Truth table50%1/2
A
A= $self->model->schema($real_name)
Aresult
11
00
290 // die qq{Could not find x-view schema "$real_name" for schema "$name"};
291 116 $props = $real_schema->json_schema->{properties};
292 }
293 867 ▶ die qq{Schema "$name" has no properties. Does it exist?} if !$props;
Branch: 1 branch
BranchTrueFalseTotal
unless $props 0 867 867
294  
295 867 my $id_field = $self->id_field;
296 867 ▶ my @id_fields = ref $id_field eq 'ARRAY' ? @$id_field : ( $id_field );
Branch: 1 branch
BranchTrueFalseTotal
ref $id_field eq 'ARRAY' ? : 48 819 867
297 # ; say "$name ID field: @id_fields";
298 # ; use Data::Dumper;
299 # ; say Dumper $props;
300  
301 867 for my $field ( @id_fields ) {
302 915 ▶ if ( !$props->{ $field } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($props->{$field}) 2 913 915
303 2 die sprintf "ID field missing in properties for schema '%s', field '%s'."
304 . " Add x-id-field to configure the correct ID field name, or"
305 . " add x-ignore to ignore this schema.",
306 $name, $field;
307 }
308 }
309 }
310  
311 21 1;
312  
313 __END__
« Yancy/Backend/Memory.pm ↑ Summary Yancy/Plugin/File.pm »