Mojolicious/Plugin/Yancy.pm

filter: Statementstmt 43.6% 123 / 282 Branchbran 21.8% 21 / 96 Conditioncond 37.6% 32 / 85 MC/DCmcdc 16.0% 8 / 50 Subroutinesub 46.8% 15 / 32 Podpod 100% 1 / 1 totaltotal 36.6% 200 / 546 CC 99 SCAR 45.9
CC
99
Cov
38%
_helper_validate
420.0
_helper_filter_apply
132.0
register
72.5
CRAP 98.9
« Yancy/Plugin/File.pm ↑ Summary Yancy/Backend.pm »
1 package Mojolicious::Plugin::Yancy;
2 20 our $VERSION = '1.078';
3 # ABSTRACT: Embed a simple admin CMS into your Mojolicious application
4  
5 #pod =head1 SYNOPSIS
6 #pod
7 #pod use Mojolicious::Lite;
8 #pod plugin Yancy => backend => 'sqlite:myapp.db'; # mysql, pg, dbic...
9 #pod app->start;
10 #pod
11 #pod =head1 DESCRIPTION
12 #pod
13 #pod This plugin allows you to add a simple content management system (CMS)
14 #pod to administrate content on your L<Mojolicious> site. This includes
15 #pod a JavaScript web application to edit the content and a REST API to help
16 #pod quickly build your own application.
17 #pod
18 #pod =head1 CONFIGURATION
19 #pod
20 #pod For getting started with a configuration for Yancy, see
21 #pod the L<"Yancy Guides"|Yancy::Guides>.
22 #pod
23 #pod Additional configuration keys accepted by the plugin are:
24 #pod
25 #pod =over
26 #pod
27 #pod =item backend
28 #pod
29 #pod In addition to specifying the backend as a single URL (see L<"Database
30 #pod Backend"|Yancy::Guides::Schema/Database Backend>), you can specify it as
31 #pod a hashref of C<< class => $db >>. This allows you to share database
32 #pod connections.
33 #pod
34 #pod use Mojolicious::Lite;
35 #pod use Mojo::Pg;
36 #pod helper pg => sub { state $pg = Mojo::Pg->new( 'postgres:///myapp' ) };
37 #pod plugin Yancy => { backend => { Pg => app->pg } };
38 #pod
39 #pod =item route
40 #pod
41 #pod A base route to add the Yancy editor to. This allows you to customize
42 #pod the URL and add authentication or authorization. Defaults to allowing
43 #pod access to the Yancy web application under C</yancy>, and the REST API
44 #pod under C</yancy/api>.
45 #pod
46 #pod This can be a string or a L<Mojolicious::Routes::Route> object.
47 #pod
48 #pod # These are equivalent
49 #pod use Mojolicious::Lite;
50 #pod plugin Yancy => { route => app->routes->any( '/admin' ) };
51 #pod plugin Yancy => { route => '/admin' };
52 #pod
53 #pod =item return_to
54 #pod
55 #pod The URL to use for the "Back to Application" link. Defaults to C</>.
56 #pod
57 #pod =item filters
58 #pod
59 #pod A hash of C<< name => subref >> pairs of filters to make available.
60 #pod See L</yancy.filter.add> for how to create a filter subroutine.
61 #pod
62 #pod =back
63 #pod
64 #pod =head1 HELPERS
65 #pod
66 #pod This plugin adds some helpers for use in routes, templates, and plugins.
67 #pod
68 #pod =head2 yancy.config
69 #pod
70 #pod my $config = $c->yancy->config;
71 #pod
72 #pod The current configuration for Yancy. Through this, you can edit the
73 #pod C<schema> configuration as needed.
74 #pod
75 #pod =head2 yancy.backend
76 #pod
77 #pod my $be = $c->yancy->backend;
78 #pod
79 #pod Get the Yancy backend object. By default, gets the backend configured
80 #pod while loading the Yancy plugin. Requests can override the backend by
81 #pod setting the C<backend> stash value. See L<Yancy::Backend> for the
82 #pod methods you can call on a backend object and their purpose.
83 #pod
84 #pod =head2 yancy.plugin
85 #pod
86 #pod Add a Yancy plugin. Yancy plugins are Mojolicious plugins that require
87 #pod Yancy features and are found in the L<Yancy::Plugin> namespace.
88 #pod
89 #pod use Mojolicious::Lite;
90 #pod plugin 'Yancy';
91 #pod app->yancy->plugin( 'Auth::Basic', { schema => 'users' } );
92 #pod
93 #pod You can also add the Yancy::Plugin namespace into the default plugin
94 #pod lookup locations. This allows you to treat them like any other
95 #pod Mojolicious plugin.
96 #pod
97 #pod # Lite app
98 #pod use Mojolicious::Lite;
99 #pod plugin 'Yancy', ...;
100 #pod unshift @{ app->plugins->namespaces }, 'Yancy::Plugin';
101 #pod plugin 'Auth::Basic', ...;
102 #pod
103 #pod # Full app
104 #pod use Mojolicious;
105 #pod sub startup {
106 #pod my ( $app ) = @_;
107 #pod $app->plugin( 'Yancy', ... );
108 #pod unshift @{ $app->plugins->namespaces }, 'Yancy::Plugin';
109 #pod $app->plugin( 'Auth::Basic', ... );
110 #pod }
111 #pod
112 #pod Yancy does not do this for you to avoid namespace collisions.
113 #pod
114 #pod =head2 yancy.list
115 #pod
116 #pod my @items = $c->yancy->list( $schema, \%param, \%opt );
117 #pod
118 #pod Get a list of items from the backend. C<$schema> is a schema
119 #pod name. C<\%param> is a L<SQL::Abstract where clause
120 #pod structure|SQL::Abstract/WHERE CLAUSES>. Some basic examples:
121 #pod
122 #pod # All people named exactly 'Turanga Leela'
123 #pod $c->yancy->list( people => { name => 'Turanga Leela' } );
124 #pod
125 #pod # All people with "Wong" in their name
126 #pod $c->yancy->list( people => { name => { like => '%Wong%' } } );
127 #pod
128 #pod C<\%opt> is a hash of options with the following keys:
129 #pod
130 #pod =over
131 #pod
132 #pod =item * limit - The number of items to return
133 #pod
134 #pod =item * offset - The number of items to skip before returning items
135 #pod
136 #pod =back
137 #pod
138 #pod See L<the backend documentation for more information about the list
139 #pod method's arguments|Yancy::Backend/list>. This helper only returns the list
140 #pod of items, not the total count of items or any other value.
141 #pod
142 #pod This helper will also filter out any password fields in the returned
143 #pod data. To get all the data, use the L<backend|/yancy.backend> helper to
144 #pod access the backend methods directly.
145 #pod
146 #pod =head2 yancy.get
147 #pod
148 #pod my $item = $c->yancy->get( $schema, $id );
149 #pod
150 #pod Get an item from the backend. C<$schema> is the schema name.
151 #pod C<$id> is the ID of the item to get. See L<Yancy::Backend/get>.
152 #pod
153 #pod This helper will filter out password values in the returned data. To get
154 #pod all the data, use the L<backend|/yancy.backend> helper to access the
155 #pod backend directly.
156 #pod
157 #pod =head2 yancy.set
158 #pod
159 #pod $c->yancy->set( $schema, $id, $item_data, %opt );
160 #pod
161 #pod Update an item in the backend. C<$schema> is the schema name.
162 #pod C<$id> is the ID of the item to update. C<$item_data> is a hash of data
163 #pod to update. See L<Yancy::Backend/set>. C<%opt> is a list of options with
164 #pod the following keys:
165 #pod
166 #pod =over
167 #pod
168 #pod =item * properties - An arrayref of properties to validate, for partial updates
169 #pod
170 #pod =back
171 #pod
172 #pod This helper will validate the data against the configuration and run any
173 #pod filters as needed. If validation fails, this helper will throw an
174 #pod exception with an array reference of L<JSON::Validator::Error> objects.
175 #pod See L<the validate helper|/yancy.validate> and L<the filter apply
176 #pod helper|/yancy.filter.apply>. To bypass filters and validation, use the
177 #pod backend object directly via L<the backend helper|/yancy.backend>.
178 #pod
179 #pod # A route to update a comment
180 #pod put '/comment/:id' => sub {
181 #pod eval { $c->yancy->set( "comment", $c->stash( 'id' ), $c->req->json ) };
182 #pod if ( $@ ) {
183 #pod return $c->render( status => 400, errors => $@ );
184 #pod }
185 #pod return $c->render( status => 200, text => 'Success!' );
186 #pod };
187 #pod
188 #pod =head2 yancy.create
189 #pod
190 #pod my $item = $c->yancy->create( $schema, $item_data );
191 #pod
192 #pod Create a new item. C<$schema> is the schema name. C<$item_data>
193 #pod is a hash of data for the new item. See L<Yancy::Backend/create>.
194 #pod
195 #pod This helper will validate the data against the configuration and run any
196 #pod filters as needed. If validation fails, this helper will throw an
197 #pod exception with an array reference of L<JSON::Validator::Error> objects.
198 #pod See L<the validate helper|/yancy.validate> and L<the filter apply
199 #pod helper|/yancy.filter.apply>. To bypass filters and validation, use the
200 #pod backend object directly via L<the backend helper|/yancy.backend>.
201 #pod
202 #pod # A route to create a comment
203 #pod post '/comment' => sub {
204 #pod eval { $c->yancy->create( "comment", $c->req->json ) };
205 #pod if ( $@ ) {
206 #pod return $c->render( status => 400, errors => $@ );
207 #pod }
208 #pod return $c->render( status => 200, text => 'Success!' );
209 #pod };
210 #pod
211 #pod =head2 yancy.delete
212 #pod
213 #pod $c->yancy->delete( $schema, $id );
214 #pod
215 #pod Delete an item from the backend. C<$schema> is the schema name.
216 #pod C<$id> is the ID of the item to delete. See L<Yancy::Backend/delete>.
217 #pod
218 #pod =head2 yancy.validate
219 #pod
220 #pod my @errors = $c->yancy->validate( $schema, $item, %opt );
221 #pod
222 #pod Validate the given C<$item> data against the configuration for the
223 #pod C<$schema>. If there are any errors, they are returned as an array
224 #pod of L<JSON::Validator::Error> objects. C<%opt> is a list of options with
225 #pod the following keys:
226 #pod
227 #pod =over
228 #pod
229 #pod =item * properties - An arrayref of properties to validate, for partial updates
230 #pod
231 #pod =back
232 #pod
233 #pod See L<JSON::Validator/validate> for more details.
234 #pod
235 #pod =head2 yancy.form
236 #pod
237 #pod By default, the L<Yancy::Plugin::Form::Bootstrap4> form plugin is
238 #pod loaded. You can override this with your own form plugin. See
239 #pod L<Yancy::Plugin::Form> for more information.
240 #pod
241 #pod =head2 yancy.file
242 #pod
243 #pod By default, the L<Yancy::Plugin::File> plugin is loaded to handle file
244 #pod uploading and file management. The default path for file uploads is
245 #pod C<$MOJO_HOME/public/uploads>. You can override this with your own file
246 #pod plugin. See L<Yancy::Plugin::File> for more information.
247 #pod
248 #pod =head2 yancy.filter.add
249 #pod
250 #pod my $filter_sub = sub { my ( $field_name, $field_value, $field_conf, @params ) = @_; ... }
251 #pod $c->yancy->filter->add( $name => $filter_sub );
252 #pod
253 #pod Create a new filter. C<$name> is the name of the filter to give in the
254 #pod field's configuration. C<$subref> is a subroutine reference that accepts
255 #pod at least three arguments:
256 #pod
257 #pod =over
258 #pod
259 #pod =item * $name - The name of the schema/field being filtered
260 #pod
261 #pod =item * $value - The value to filter, either the entire item, or a single field
262 #pod
263 #pod =item * $conf - The configuration for the schema/field
264 #pod
265 #pod =item * @params - Other parameters if configured
266 #pod
267 #pod =back
268 #pod
269 #pod For example, here is a filter that will run a password through a one-way hash
270 #pod digest:
271 #pod
272 #pod use Digest;
273 #pod my $digest = sub {
274 #pod my ( $field_name, $field_value, $field_conf ) = @_;
275 #pod my $type = $field_conf->{ 'x-digest' }{ type };
276 #pod Digest->new( $type )->add( $field_value )->b64digest;
277 #pod };
278 #pod $c->yancy->filter->add( 'digest' => $digest );
279 #pod
280 #pod And you configure this on a field using C<< x-filter >> and C<< x-digest >>:
281 #pod
282 #pod # mysite.conf
283 #pod {
284 #pod schema => {
285 #pod users => {
286 #pod properties => {
287 #pod username => { type => 'string' },
288 #pod password => {
289 #pod type => 'string',
290 #pod format => 'password',
291 #pod 'x-filter' => [ 'digest' ], # The name of the filter
292 #pod 'x-digest' => { # Filter configuration
293 #pod type => 'SHA-1',
294 #pod },
295 #pod },
296 #pod },
297 #pod },
298 #pod },
299 #pod }
300 #pod
301 #pod The same filter, but also configurable with extra parameters:
302 #pod
303 #pod my $digest = sub {
304 #pod my ( $field_name, $field_value, $field_conf, @params ) = @_;
305 #pod my $type = ( $params[0] || $field_conf->{ 'x-digest' } )->{ type };
306 #pod Digest->new( $type )->add( $field_value )->b64digest;
307 #pod $field_value . $params[0];
308 #pod };
309 #pod $c->yancy->filter->add( 'digest' => $digest );
310 #pod
311 #pod The alternative configuration:
312 #pod
313 #pod # mysite.conf
314 #pod {
315 #pod schema => {
316 #pod users => {
317 #pod properties => {
318 #pod username => { type => 'string' },
319 #pod password => {
320 #pod type => 'string',
321 #pod format => 'password',
322 #pod 'x-filter' => [ [ digest => { type => 'SHA-1' } ] ],
323 #pod },
324 #pod },
325 #pod },
326 #pod },
327 #pod }
328 #pod
329 #pod Schemas can also have filters. A schema filter will get the
330 #pod entire hash reference as its value. For example, here's a filter that
331 #pod updates the C<last_updated> field with the current time:
332 #pod
333 #pod $c->yancy->filter->add( 'timestamp' => sub {
334 #pod my ( $schema_name, $item, $schema_conf ) = @_;
335 #pod $item->{last_updated} = time;
336 #pod return $item;
337 #pod } );
338 #pod
339 #pod And you configure this on the schema using C<< x-filter >>:
340 #pod
341 #pod # mysite.conf
342 #pod {
343 #pod schema => {
344 #pod people => {
345 #pod 'x-filter' => [ 'timestamp' ],
346 #pod properties => {
347 #pod name => { type => 'string' },
348 #pod address => { type => 'string' },
349 #pod last_updated => { type => 'datetime' },
350 #pod },
351 #pod },
352 #pod },
353 #pod }
354 #pod
355 #pod You can configure filters on OpenAPI operations' inputs. These will
356 #pod probably want to operate on hash-refs as in the schema-level filters
357 #pod above. The config passed will be an empty hash. The filter can be applied
358 #pod to either or both of the path, or the individual operation, and will be
359 #pod executed in that order. E.g.:
360 #pod
361 #pod # mysite.conf
362 #pod {
363 #pod openapi => {
364 #pod definitions => {
365 #pod people => {
366 #pod properties => {
367 #pod name => { type => 'string' },
368 #pod address => { type => 'string' },
369 #pod last_updated => { type => 'datetime' },
370 #pod },
371 #pod },
372 #pod },
373 #pod paths => {
374 #pod "/people" => {
375 #pod # could also have x-filter here
376 #pod "post" => {
377 #pod 'x-filter' => [ 'timestamp' ],
378 #pod # ...
379 #pod },
380 #pod },
381 #pod }
382 #pod },
383 #pod }
384 #pod
385 #pod You can also configure filters on OpenAPI operations' outputs, this time
386 #pod with the key C<x-filter-output>. Again, the config passed will be an empty
387 #pod hash. The filter can be applied to either or both of the path, or the
388 #pod individual operation, and will be executed in that order. E.g.:
389 #pod
390 #pod # mysite.conf
391 #pod {
392 #pod openapi => {
393 #pod paths => {
394 #pod "/people" => {
395 #pod 'x-filter-output' => [ 'timestamp' ],
396 #pod # ...
397 #pod },
398 #pod }
399 #pod },
400 #pod }
401 #pod
402 #pod =head3 Supplied filters
403 #pod
404 #pod These filters are always installed.
405 #pod
406 #pod =head4 yancy.from_helper
407 #pod
408 #pod The first configured parameter is the name of an installed Mojolicious
409 #pod helper. That helper will be called, with any further supplied parameters,
410 #pod and the return value will be used as the value of that field /
411 #pod item. E.g. with this helper:
412 #pod
413 #pod $app->helper( 'current_time' => sub { scalar gmtime } );
414 #pod
415 #pod This configuration will achieve the same as the above with C<last_updated>:
416 #pod
417 #pod # mysite.conf
418 #pod {
419 #pod schema => {
420 #pod people => {
421 #pod properties => {
422 #pod name => { type => 'string' },
423 #pod address => { type => 'string' },
424 #pod last_updated => {
425 #pod type => 'datetime',
426 #pod 'x-filter' => [ [ 'yancy.from_helper' => 'current_time' ] ],
427 #pod },
428 #pod },
429 #pod },
430 #pod },
431 #pod }
432 #pod
433 #pod =head4 yancy.overlay_from_helper
434 #pod
435 #pod Intended to be used for "items" rather than individual fields, as it
436 #pod will only work when the "value" parameter is a hash-ref.
437 #pod
438 #pod The configured parameters are supplied in pairs. The first item in the
439 #pod pair is the string key in the hash-ref. The second is either the name of
440 #pod a helper, or an array-ref with the first entry as such a helper-name,
441 #pod followed by parameters to pass that helper. For each pair, the helper
442 #pod will be called, and its return value set as the relevant key's value.
443 #pod E.g. with this helper:
444 #pod
445 #pod $app->helper( 'current_time' => sub { scalar gmtime } );
446 #pod
447 #pod This configuration will achieve the same as the above with C<last_updated>:
448 #pod
449 #pod # mysite.conf
450 #pod {
451 #pod schema => {
452 #pod people => {
453 #pod 'x-filter' => [
454 #pod [ 'yancy.overlay_from_helper' => 'last_updated', 'current_time' ]
455 #pod ],
456 #pod properties => {
457 #pod name => { type => 'string' },
458 #pod address => { type => 'string' },
459 #pod last_updated => { type => 'datetime' },
460 #pod },
461 #pod },
462 #pod },
463 #pod }
464 #pod
465 #pod =head4 yancy.wrap
466 #pod
467 #pod The configured parameters are a list of strings. For each one, the
468 #pod original value will be wrapped in a hash with that string as the key,
469 #pod and the previous value as the value. E.g. with this config:
470 #pod
471 #pod 'x-filter-output' => [
472 #pod [ 'yancy.wrap' => qw(user login) ],
473 #pod ],
474 #pod
475 #pod The original value of say C<{ user => 'bob', password => 'h12' }>
476 #pod will become:
477 #pod
478 #pod {
479 #pod login => {
480 #pod user => { user => 'bob', password => 'h12' }
481 #pod }
482 #pod }
483 #pod
484 #pod The utility of this comes from being able to expressively translate to
485 #pod and from a simple database structure to a situation where simple values
486 #pod or JSON objects need to be wrapped in objects one or two deep.
487 #pod
488 #pod =head4 yancy.unwrap
489 #pod
490 #pod This is the converse of the above. The configured parameters are a
491 #pod list of strings. For each one, the original value (a hash-ref) will be
492 #pod "unwrapped" by looking in the given hash and extracting the value whose
493 #pod key is that string. E.g. with this config:
494 #pod
495 #pod 'x-filter' => [
496 #pod [ 'yancy.unwrap' => qw(login user) ],
497 #pod ],
498 #pod
499 #pod This will achieve the reverse of the transformation given in
500 #pod L</yancy.wrap> above. Note that obviously the order of arguments is
501 #pod inverted, since this operates outside-inward, while C<yancy.wrap>
502 #pod operates inside-outward.
503 #pod
504 #pod =head4 yancy.mask
505 #pod
506 #pod Mask part of a field's value by replacing a regular expression match
507 #pod with the given character. The first parameter is a regular expression to
508 #pod match. The second parameter is the character to replace each matched
509 #pod character with.
510 #pod
511 #pod # Replace all text before the @ with *
512 #pod 'x-filter' => [
513 #pod [ 'yancy.mask' => '^[^@]+', '*' ]
514 #pod ],
515 #pod # Replace all but the last two characters before the @
516 #pod 'x-filter' => [
517 #pod [ 'yancy.mask' => '^[^@]+(?=[^@]{2}@)', '*' ]
518 #pod ],
519 #pod
520 #pod =head2 yancy.filter.apply
521 #pod
522 #pod my $filtered_data = $c->yancy->filter->apply( $schema, $item_data );
523 #pod
524 #pod Run the configured filters on the given C<$item_data>. C<$schema> is
525 #pod a schema name. Returns the hash of C<$filtered_data>.
526 #pod
527 #pod The property-level filters will run before any schema-level filter,
528 #pod so that schema-level filters can take advantage of any values set by
529 #pod the inner filters.
530 #pod
531 #pod =head2 yancy.filters
532 #pod
533 #pod Returns a hash-ref of all configured helpers, mapping the names to
534 #pod the code-refs.
535 #pod
536 #pod =head2 yancy.schema
537 #pod
538 #pod my $schema = $c->yancy->schema( $name );
539 #pod $c->yancy->schema( $name => $schema );
540 #pod my $schemas = $c->yancy->schema;
541 #pod
542 #pod Get or set the JSON schema for the given schema C<$name>. If no
543 #pod schema name is given, returns a hashref of all the schema.
544 #pod
545 #pod =head2 log_die
546 #pod
547 #pod Raise an exception with L<Mojo::Exception/raise>, first logging
548 #pod using L<Mojo::Log/fatal> (through the L<C<log> helper|Mojolicious::Plugin::DefaultHelpers/log>.
549 #pod
550 #pod =head1 TEMPLATES
551 #pod
552 #pod This plugin uses the following templates. To override these templates
553 #pod with your own theme, provide a template with the same name. Remember to
554 #pod add your template paths to the beginning of the list of paths to be sure
555 #pod your templates are found first:
556 #pod
557 #pod # Mojolicious::Lite
558 #pod unshift @{ app->renderer->paths }, 'template/directory';
559 #pod unshift @{ app->renderer->classes }, __PACKAGE__;
560 #pod
561 #pod # Mojolicious
562 #pod sub startup {
563 #pod my ( $app ) = @_;
564 #pod unshift @{ $app->renderer->paths }, 'template/directory';
565 #pod unshift @{ $app->renderer->classes }, __PACKAGE__;
566 #pod }
567 #pod
568 #pod =over
569 #pod
570 #pod =item layouts/yancy.html.ep
571 #pod
572 #pod This layout template surrounds all other Yancy templates. Like all
573 #pod Mojolicious layout templates, a replacement should use the C<content>
574 #pod helper to display the page content. Additionally, a replacement should
575 #pod use C<< content_for 'head' >> to add content to the C<head> element.
576 #pod
577 #pod =back
578 #pod
579 #pod =head1 SEE ALSO
580 #pod
581 #pod =cut
582  
583 20 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
584 20 ▶use Yancy;
Subroutine BEGIN: called CC=1 SCAR=0.0
585 20 ▶use Mojo::JSON qw( true false decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
586 20 ▶use Mojo::File qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
587 20 ▶use Mojo::Loader qw( load_class );
Subroutine BEGIN: called CC=1 SCAR=0.0
588 20 ▶use Yancy::Util qw( load_backend curry copy_inline_refs derp is_type json_validator );
Subroutine BEGIN: called CC=1 SCAR=0.0
589 20 ▶use Storable qw( dclone );
Subroutine BEGIN: called CC=1 SCAR=0.0
590 20 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
591  
592 19 ▶has _filters => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
593  
594 sub register {
595 19 ▶ my ( $self, $app, $config ) = @_;
Subroutine register: called CC=31 SCAR=42.8
Pod documented
596  
597 # XXX: Move editor, auth, schema to attributes of this object.
598 # That allows for easier extending/replacing of them.
599 # XXX: Deprecate direct access to the backend. Backend should be
600 # accessed through the schema, if needed.
601  
602 # New default for read_schema is on, since it mostly should be
603 # on. Any real-world database is going to be painstakingly tedious
604 # to type out in JSON schema...
605 19 ▶ $config->{read_schema} //= !exists $config->{openapi};
Condition66%2/3
$config->{"read_schema"} //= !exists $config->{"openapi"}
l!l&&r!l&&!r
5140
MC/DC0%0/2
$config->{"read_schema"} //= !exists $config->{"openapi"}
$config->{"read_schema"} !exists $config->{"openapi"}
Truth table66%2/3
A //= not(B)
A= $config->{"read_schema"}B= !exists $config->{"openapi"}
ABresult
1X1
011
000
606  
607 19 ▶ if ( $config->{collections} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"collections"}) 0 19 19
608 0 derp '"collections" stash key is now "schema" in Yancy configuration';
609 0 $config->{schema} = $config->{collections};
610 }
611 die "Cannot pass both openapi AND (schema or read_schema)"
612 if $config->{openapi}
613 19 ▶ && ( $config->{schema} || $config->{read_schema} );
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"openapi"} and $config->{"schema"} || $config->{"read_schema"} 0 19 19
Condition16%1/6
$config->{"openapi"} and $config->{"schema"} || $config->{"read_schema"}
!ll&&!rl&&r
1900
$config->{"schema"} or $config->{"read_schema"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$config->{"openapi"} and $config->{"schema"} || $config->{"read_schema"}
$config->{"openapi"} $config->{"schema"} $config->{"read_schema"}
Truth table25%1/4
A and B or C
A= $config->{"openapi"}B= $config->{"schema"}C= $config->{"read_schema"}
ABCresult
0XX0
1000
11X1
1011
614  
615 # Load the backend and schema
616 19 $config = { %$config };
617 $app->helper( 'yancy.backend' => sub {
618 19 ▶ my ( $c ) = @_;
Subroutine __ANON__: called CC=3 SCAR=13.5
619 19 ▶ state $default_backend = load_backend( $config->{backend}, $config->{schema} || $config->{openapi}{definitions} );
Condition33%1/3
$config->{'schema'} || $config->{'openapi'}{'definitions'}
l!l&&r!l&&!r
1900
MC/DC0%0/2
$config->{'schema'} || $config->{'openapi'}{'definitions'}
$config->{'schema'} $config->{'openapi'}{'definitions'}
Truth table33%1/3
A || B
A= $config->{'schema'}B= $config->{'openapi'}{'definitions'}
ABresult
1X1
011
000
620 19 ▶ if ( my $backend = $c->stash( 'backend' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $backend = $c->stash("backend")) 0 19 19
621 0 $c->log->debug( 'Using override backend from stash: ' . ref $backend );
622 0 return $backend;
623 }
624 19 return $default_backend;
625 19 } );
626 # XXX: Move this to Yancy::Schema
627 19 ▶ if ( $config->{schema} || $config->{read_schema} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($config->{'schema'} or $config->{'read_schema'}) { } 19 0 19
elsif ($config->{'openapi'}) { } 0 0 0
Condition66%2/3
$config->{"schema"} or $config->{"read_schema"}
l!l&&r!l&&!r
1720
MC/DC0%0/2
$config->{"schema"} or $config->{"read_schema"}
$config->{"schema"} $config->{"read_schema"}
Truth table66%2/3
A or B
A= $config->{"schema"}B= $config->{"read_schema"}
ABresult
1X1
011
000
628 19 ▶ $config->{schema} = $config->{schema} ? dclone( $config->{schema} ) : {};
Branch: 1 branch
BranchTrueFalseTotal
$config->{'schema'} ? : 17 2 19
629  
630 19 ▶ if ( $config->{read_schema} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"read_schema"}) 19 0 19
631 19 my $schema = $app->yancy->backend->read_schema;
632 # ; use Data::Dumper;
633 # ; say 'Read schema: ' . Dumper $schema;
634 19 for my $c ( keys %$schema ) {
635 83 ▶ _merge_schema( $config->{schema}{ $c } ||= {}, $schema->{ $c } );
Condition100%2/2
$config->{"schema"}{$c} ||= {}
l!l
7013
MC/DC100%1/1
$config->{"schema"}{$c} ||= {}
$config->{"schema"}{$c}
Truth table100%2/2
A
A= $config->{"schema"}{$c}
Aresult
11
00
636 }
637 }
638 # read_schema on schema
639 19 for my $schema_name ( keys %{ $config->{schema} } ) {
640 117 my $schema = $config->{schema}{ $schema_name };
641 117 ▶ if ( delete $schema->{read_schema} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $schema->{"read_schema"}) 0 117 117
642 0 _merge_schema( $schema, $app->yancy->backend->read_schema( $schema_name ) );
643 }
644 }
645  
646 # ; warn 'Merged Schema';
647 # ; use Data::Dumper;
648 # ; warn Dumper $config->{schema};
649  
650 # Sanity check for the schema.
651 19 for my $schema_name ( keys %{ $config->{schema} } ) {
652 117 my $schema = $config->{schema}{ $schema_name };
653 117 ▶ next if $schema->{ 'x-ignore' }; # XXX Should we just delete x-ignore schema?
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"x-ignore"} 19 98 117
654  
655 # Deprecate x-view. Yancy::Model is a much better
656 # solution to that.
657 derp q{x-view is deprecated and will be removed in v2. }
658 . q{Use Yancy::Model or your database's CREATE VIEW instead}
659 98 ▶ if $schema->{'x-view'};
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"x-view"} 34 64 98
660  
661 98 ▶ $schema->{ type } //= 'object';
Condition100%2/2
$schema->{"type"} //= "object"
l!l
962
MC/DC100%1/1
$schema->{"type"} //= "object"
$schema->{"type"}
Truth table100%2/2
A
A= $schema->{"type"}
Aresult
11
00
662 98 ▶ my $real_schema_name = ( $schema->{'x-view'} || {} )->{schema} // $schema_name;
Condition80%4/5
+($schema->{"x-view"} || {})->{"schema"} // $schema_name
l!l&&r!l&&!r
34640
$schema->{'x-view'} || {}
l!l
3464
MC/DC0%0/2
+($schema->{"x-view"} || {})->{"schema"} // $schema_name
$schema->{'x-view'} $schema_name
Truth table66%2/3
A // B
A= $schema->{'x-view'}B= $schema_name
ABresult
1X1
011
000
663 my $props = $schema->{properties}
664 98 ▶ || $config->{schema}{ $real_schema_name }{properties};
Condition66%2/3
$schema->{'properties'} || $config->{'schema'}{$real_schema_name}{'properties'}
l!l&&r!l&&!r
81170
MC/DC0%0/2
$schema->{'properties'} || $config->{'schema'}{$real_schema_name}{'properties'}
$schema->{'properties'} $config->{'schema'}{$real_schema_name}{'properties'}
Truth table66%2/3
A || B
A= $schema->{'properties'}B= $config->{'schema'}{$real_schema_name}{'properties'}
ABresult
1X1
011
000
665 98 ▶ my $id_field = $schema->{ 'x-id-field' } // 'id';
Condition100%2/2
$schema->{"x-id-field"} // "id"
l!l
6038
MC/DC100%1/1
$schema->{"x-id-field"} // "id"
$schema->{"x-id-field"}
Truth table100%2/2
A
A= $schema->{"x-id-field"}
Aresult
11
00
666 98 ▶ my @id_fields = ref $id_field eq 'ARRAY' ? @$id_field : ( $id_field );
Branch: 1 branch
BranchTrueFalseTotal
ref $id_field eq 'ARRAY' ? : 3 95 98
667  
668 98 for my $field ( @id_fields ) {
669 101 ▶ if ( !$props->{ $field } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($props->{$field}) 0 101 101
670 0 die sprintf "ID field missing in properties for schema '%s', field '%s'."
671 . " Add x-id-field to configure the correct ID field name, or"
672 . " add x-ignore to ignore this schema.",
673 $schema_name, $field;
674 }
675 }
676 }
677 }
678 elsif ( $config->{openapi} ) {
679 0 $config->{openapi} = _ensure_json_data( $app, $config->{openapi} );
680 0 $config->{schema} = dclone( $config->{openapi}{definitions} );
681 }
682  
683 # Resources and templates
684 19 my $share = path( __FILE__ )->sibling( 'Yancy' )->child( 'resources' );
685 19 push @{ $app->static->paths }, $share->child( 'public' )->to_string;
686 19 push @{ $app->renderer->paths }, $share->child( 'templates' )->to_string;
687 19 push @{$app->routes->namespaces}, 'Yancy::Controller';
688 19 push @{ $app->commands->namespaces }, 'Yancy::Command';
689 19 $app->plugin( 'I18N', { namespace => 'Yancy::I18N' } );
690  
691 # Helpers
692 19 ▶ $app->helper( 'yancy.config' => sub { return $config } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
693 19 $app->helper( 'yancy.plugin' => \&_helper_plugin );
694 19 $app->helper( 'yancy.schema' => \&_helper_schema );
695 19 $app->helper( 'yancy.list' => \&_helper_list );
696 19 $app->helper( 'yancy.get' => \&_helper_get );
697 19 $app->helper( 'yancy.delete' => \&_helper_delete );
698 19 $app->helper( 'yancy.set' => \&_helper_set );
699 19 $app->helper( 'yancy.create' => \&_helper_create );
700 19 $app->helper( 'yancy.validate' => \&_helper_validate );
701 19 $app->helper( 'yancy.routify' => \&_helper_routify );
702 19 $app->helper( 'log_die' => \&_helper_log_die );
703  
704 # Default form is Bootstrap4. Any form plugin added after this will
705 # override this one
706 19 $app->yancy->plugin( 'Form::Bootstrap4' );
707 19 $app->yancy->plugin( File => {
708 path => $app->home->child( 'public/uploads' ),
709 } );
710  
711 $self->_helper_filter_add( undef, 'yancy.from_helper' => sub {
712 0 ▶ my ( $field_name, $field_value, $field_conf, @params ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
713 0 my $which_helper = shift @params;
714 0 my $helper = $app->renderer->get_helper( $which_helper );
715 0 $helper->( @params );
716 19 } );
717 $self->_helper_filter_add( undef, 'yancy.overlay_from_helper' => sub {
718 0 ▶ my ( $field_name, $field_value, $field_conf, @params ) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
719 0 my %new_item = %$field_value;
720 0 ▶ while ( my ( $key, $helper ) = splice @params, 0, 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@params, 0, 2)) 0 0 0
721 0 ▶ ( $helper, my @this_params ) = @$helper if ref $helper eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $helper eq "ARRAY" 0 0 0
722 0 my $v = $app->renderer->get_helper( $helper )->( @this_params );
723 0 $new_item{ $key } = $v;
724 }
725 0 \%new_item;
726 19 } );
727 $self->_helper_filter_add( undef, 'yancy.wrap' => sub {
728 0 ▶ my ( $field_name, $field_value, $field_conf, @params ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
729 0 $field_value = { $_ => $field_value } for @params;
730 0 $field_value;
731 19 } );
732 $self->_helper_filter_add( undef, 'yancy.unwrap' => sub {
733 0 ▶ my ( $field_name, $field_value, $field_conf, @params ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
734 0 $field_value = $field_value->{$_} for @params;
735 0 $field_value;
736 19 } );
737 $self->_helper_filter_add( undef, 'yancy.mask' => sub {
738 0 ▶ my ( $field_name, $field_value, $field_conf, $regex, $replace ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
739 0 $field_value =~ s/($regex)/$replace x length $1/e;
740 0 $field_value;
741 19 } );
742 19 for my $name ( keys %{ $config->{filters} } ) {
743 0 $self->_helper_filter_add( undef, $name, $config->{filters}{$name} );
744 }
745 19 $app->helper( 'yancy.filter.add' => curry( \&_helper_filter_add, $self ) );
746 19 $app->helper( 'yancy.filter.apply' => curry( \&_helper_filter_apply, $self ) );
747 $app->helper( 'yancy.filters' => sub {
748 0 ▶ state $filters = $self->_filters;
Subroutine __ANON__: not called CC=1 SCAR=6.9
749 19 } );
750  
751 # Some keys we used to allow on the top level configuration, but are
752 # now on the editor plugin
753 19 my @_moved_to_editor_keys = qw( api_controller info host return_to );
754 19 ▶ if ( my @moved_keys = grep exists $config->{$_}, @_moved_to_editor_keys ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@moved_keys) = grep(exists $config->{$_}, @_moved_to_editor_keys)) 0 19 19
755 0 derp 'Editor configuration keys should be in the `editor` configuration hash ref: '
756 . join ', ', @moved_keys;
757 }
758  
759 # Add the default editor unless the user explicitly disables it
760 19 ▶ if ( !exists $config->{editor} || defined $config->{editor} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $config->{"editor"} or defined $config->{"editor"}) 19 0 19
Condition66%2/3
not exists $config->{"editor"} or defined $config->{"editor"}
l!l&&r!l&&!r
1540
MC/DC0%0/2
not exists $config->{"editor"} or defined $config->{"editor"}
not exists $config->{"editor"} defined $config->{"editor"}
Truth table66%2/3
not(A) or B
A= not exists $config->{"editor"}B= defined $config->{"editor"}
ABresult
1X1
011
000
761 $app->yancy->plugin( 'Editor' => {
762 (
763 19 map { $_ => $config->{ $_ } }
764 133 grep { defined $config->{ $_ } }
765 qw( openapi schema route ),
766 @_moved_to_editor_keys,
767 ),
768 19 ▶ %{ $config->{editor} // {} },
Condition100%2/2
$config->{"editor"} // {}
l!l
415
MC/DC100%1/1
$config->{"editor"} // {}
$config->{"editor"}
Truth table100%2/2
A
A= $config->{"editor"}
Aresult
11
00
769 } );
770 }
771 }
772  
773 # if false or a ref, just returns same
774 # if non-ref, treat as JSON-containing file, load and decode
775 sub _ensure_json_data {
776 0 ▶ my ( $app, $data ) = @_;
Subroutine _ensure_json_data: not called CC=3 SCAR=24.8
777 0 ▶ return $data if !$data or ref $data;
Branch: 1 branch
BranchTrueFalseTotal
if not $data or ref $data 0 0 0
Condition0%0/3
not $data or ref $data
l!l&&r!l&&!r
000
MC/DC0%0/2
not $data or ref $data
not $data ref $data
Truth table0%0/3
not(A) or B
A= not $dataB= ref $data
ABresult
1X1
011
000
778 # assume a file in JSON format: load and parse it
779 0 decode_json $app->home->child( $data )->slurp;
780 }
781  
782 sub _helper_plugin {
783 57 ▶ my ( $c, $name, @args ) = @_;
Subroutine _helper_plugin: called CC=3 SCAR=12.7
784 57 my $class = 'Yancy::Plugin::' . $name;
785 57 ▶ if ( my $e = load_class( $class ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = load_class($class)) 0 57 57
786 0 ▶ die ref $e ? "Could not load class $class: $e" : "Could not find class $class";
Branch: 1 branch
BranchTrueFalseTotal
ref $e ? : 0 0 0
787 }
788 57 my $plugin = $class->new;
789 57 $plugin->register( $c->app, @args );
790 }
791  
792 sub _helper_schema {
793 0 ▶ my ( $c, $name, $schema ) = @_;
Subroutine _helper_schema: not called CC=3 SCAR=24.8
794 0 ▶ if ( !$name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 0 0 0
795 0 return $c->yancy->backend->schema;
796 }
797 0 ▶ if ( $schema ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema) 0 0 0
798 0 $c->yancy->backend->schema->{ $name } = $schema;
799 0 return;
800 }
801 0 return copy_inline_refs( $c->yancy->backend->schema, "/$name" );
802 }
803  
804 sub _helper_list {
805 0 ▶ my ( $c, $schema_name, @args ) = @_;
Subroutine _helper_list: not called CC=5 SCAR=34.0
806 0 my @items = @{ $c->yancy->backend->list( $schema_name, @args )->{items} };
807 0 my $schema = $c->yancy->schema( $schema_name );
808 0 for my $prop_name ( keys %{ $schema->{properties} } ) {
809 0 my $prop = $schema->{properties}{ $prop_name };
810 0 ▶ if ( $prop->{format} && $prop->{format} eq 'password' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prop->{"format"} and $prop->{"format"} eq "password") 0 0 0
Condition0%0/3
$prop->{"format"} and $prop->{"format"} eq "password"
!ll&&!rl&&r
000
MC/DC0%0/2
$prop->{"format"} and $prop->{"format"} eq "password"
$prop->{"format"} $prop->{"format"} eq "password"
Truth table0%0/3
A and B
A= $prop->{"format"}B= $prop->{"format"} eq "password"
ABresult
0X0
100
111
811 0 delete $_->{ $prop_name } for @items;
812 }
813 }
814 0 return map { $c->yancy->filter->apply( $schema_name, $_, 'x-filter-output' ) } @items;
815 }
816  
817 sub _helper_get {
818 0 ▶ my ( $c, $schema_name, $id, @args ) = @_;
Subroutine _helper_get: not called CC=4 SCAR=30.0
819 0 my $item = $c->yancy->backend->get( $schema_name, $id, @args );
820 0 my $schema = $c->yancy->schema( $schema_name );
821 0 for my $prop_name ( keys %{ $schema->{properties} } ) {
822 0 my $prop = $schema->{properties}{ $prop_name };
823 0 ▶ if ( $prop->{format} && $prop->{format} eq 'password' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prop->{"format"} and $prop->{"format"} eq "password") 0 0 0
Condition0%0/3
$prop->{"format"} and $prop->{"format"} eq "password"
!ll&&!rl&&r
000
MC/DC0%0/2
$prop->{"format"} and $prop->{"format"} eq "password"
$prop->{"format"} $prop->{"format"} eq "password"
Truth table0%0/3
A and B
A= $prop->{"format"}B= $prop->{"format"} eq "password"
ABresult
0X0
100
111
824 0 delete $item->{ $prop_name };
825 }
826 }
827 0 $item = $c->yancy->filter->apply( $schema_name, $item, 'x-filter-output' );
828 0 return $item;
829 }
830  
831 sub _helper_delete {
832 0 ▶ my ( $c, @args ) = @_;
Subroutine _helper_delete: not called CC=1 SCAR=6.9
833 0 return $c->yancy->backend->delete( @args );
834 }
835  
836 sub _helper_set {
837 0 ▶ my ( $c, $schema, $id, $item, %opt ) = @_;
Subroutine _helper_set: not called CC=3 SCAR=24.8
838 my %validate_opt =
839 0 map { $_ => $opt{ $_ } }
840 0 grep { exists $opt{ $_ } }
841 qw( properties );
842 0 ▶ if ( my @errors = $c->yancy->validate( $schema, $item, %validate_opt ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@errors) = $c->yancy->validate($schema, $item, %validate_opt)) 0 0 0
843 0 $c->app->log->error(
844 sprintf 'Error validating item with ID "%s" in schema "%s": %s',
845 $id, $schema,
846 join ', ', @errors
847 );
848 0 die \@errors;
849 }
850 0 $item = $c->yancy->filter->apply( $schema, $item );
851 0 my $ret = eval { $c->yancy->backend->set( $schema, $id, $item ) };
852 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
853 0 $c->app->log->error(
854 sprintf 'Error setting item with ID "%s" in schema "%s": %s',
855 $id, $schema, $@,
856 );
857 0 die $@;
858 }
859 0 return $ret;
860 }
861  
862 sub _helper_create {
863 0 ▶ my ( $c, $schema, $item ) = @_;
Subroutine _helper_create: not called CC=5 SCAR=34.0
864  
865 0 my $props = $c->yancy->schema( $schema )->{properties};
866 $item->{ $_ } = $props->{ $_ }{default}
867 0 ▶ for grep !exists $item->{ $_ } && exists $props->{ $_ }{default},
Condition0%0/3
!exists $item->{$_} && exists $props->{$_}{'default'}
!ll&&!rl&&r
000
MC/DC0%0/2
!exists $item->{$_} && exists $props->{$_}{'default'}
!exists $item->{$_} exists $props->{$_}{'default'}
Truth table0%0/3
not(A) && B
A= !exists $item->{$_}B= exists $props->{$_}{'default'}
ABresult
0X0
100
111
868 keys %$props;
869  
870 0 ▶ if ( my @errors = $c->yancy->validate( $schema, $item ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@errors) = $c->yancy->validate($schema, $item)) 0 0 0
871 0 $c->app->log->error(
872 sprintf 'Error validating new item in schema "%s": %s',
873 $schema,
874 join ', ', @errors
875 );
876 0 die \@errors;
877 }
878  
879 0 $item = $c->yancy->filter->apply( $schema, $item );
880 0 my $ret = eval { $c->yancy->backend->create( $schema, $item ) };
881 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
882 0 $c->app->log->error(
883 sprintf 'Error creating item in schema "%s": %s',
884 $schema, $@,
885 );
886 0 die $@;
887 }
888 0 return $ret;
889 }
890  
891 sub _helper_validate {
892 0 ▶ my ( $c, $schema_name, $input_item, %opt ) = @_;
Subroutine _helper_validate: not called CC=20 SCAR=60.4
893 0 my $schema = $c->yancy->schema( $schema_name );
894 0 my $v = json_validator();
895  
896 0 ▶ if ( $opt{ properties } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"properties"}) 0 0 0
897 # Only validate these properties
898 $schema = {
899 type => 'object',
900 required => [
901 0 ▶ grep { my $f = $_; grep { $_ eq $f } @{ $schema->{required} || [] } }
Branch: 1 branch
BranchTrueFalseTotal
$schema->{"required"} or [] 0 0 0
902 0 @{ $opt{ properties } }
903 ],
904 properties => {
905 0 map { $_ => $schema->{properties}{$_} }
906 0 grep { exists $schema->{properties}{$_} }
907 0 @{ $opt{ properties } }
908 },
909 additionalProperties => 0, # Disallow any other properties
910 };
911 }
912 0 $v->schema( $schema );
913  
914 0 my @errors;
915 0 my %check_item = %$input_item;
916 0 for my $prop_name ( keys %{ $schema->{properties} } ) {
917 0 my $prop = $schema->{properties}{ $prop_name };
918  
919 # These blocks fix problems with validation only. If the
920 # problem is the database understanding the value, it must be
921 # fixed in the backend class.
922  
923 # Pre-filter booleans
924 0 ▶ 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}) 0 0 0
Condition0%0/3
is_type($prop->{"type"}, "boolean") and defined $check_item{$prop_name}
!ll&&!rl&&r
000
MC/DC0%0/2
is_type($prop->{"type"}, "boolean") and defined $check_item{$prop_name}
is_type($prop->{"type"}, "boolean") defined $check_item{$prop_name}
Truth table0%0/3
A and B
A= is_type($prop->{"type"}, "boolean")B= defined $check_item{$prop_name}
ABresult
0X0
100
111
925 0 my $value = $check_item{ $prop_name };
926 0 ▶ if ( $value eq 'false' or !$value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq 'false' or not $value) { } 0 0 0
Condition0%0/3
$value eq "false" or not $value
l!l&&r!l&&!r
000
MC/DC0%0/2
$value eq "false" or not $value
$value eq "false" not $value
Truth table0%0/3
A or not(B)
A= $value eq "false"B= not $value
ABresult
1X1
011
000
927 0 $value = false;
928 } else {
929 0 $value = true;
930 }
931 0 $check_item{ $prop_name } = $value;
932 }
933 # An empty date-time, date, or time must become undef: The empty
934 # string will never pass the format check, but properties that
935 # are allowed to be null can be validated.
936 0 ▶ 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) 0 0 0
Condition0%0/6
is_type($prop->{"type"}, "string") and $prop->{"format"} and $prop->{"format"} =~ /^(?:date-time|date|time)$/u
!ll&&!rl&&r
000
is_type($prop->{"type"}, "string") and $prop->{"format"}
!ll&&!rl&&r
000
MC/DC0%0/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 table0%0/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
937 0 ▶ 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}) { } 0 0 0
elsif (($check_item{$prop_name} // '') eq 'now') { } 0 0 0
Condition0%0/3
exists $check_item{$prop_name} and not $check_item{$prop_name}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $check_item{$prop_name} and not $check_item{$prop_name}
exists $check_item{$prop_name} not $check_item{$prop_name}
Truth table0%0/3
A and not(B)
A= exists $check_item{$prop_name}B= not $check_item{$prop_name}
ABresult
0X0
100
111
938 0 $check_item{ $prop_name } = undef;
939 }
940 # The "now" special value will not validate yet, but will be
941 # replaced by the Backend with something useful
942 ▶ elsif ( ($check_item{ $prop_name }//'') eq 'now' ) {
Condition0%0/2
$check_item{$prop_name} // ""
l!l
00
MC/DC0%0/1
$check_item{$prop_name} // ""
$check_item{$prop_name}
Truth table0%0/2
A
A= $check_item{$prop_name}
Aresult
11
00
943 0 delete $check_item{ $prop_name };
944 }
945 }
946 # Always add dummy passwords to pass required checks
947 0 ▶ 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 0 0
Condition0%0/6
$prop->{"format"} and $prop->{"format"} eq "password" and not $check_item{$prop_name}
!ll&&!rl&&r
000
$prop->{"format"} and $prop->{"format"} eq "password"
!ll&&!rl&&r
000
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 table0%0/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
948 # Add to a new copy of the item so we don't actually change
949 # the item
950 0 %check_item = ( %check_item, $prop_name => '<PASSWORD>' );
951 }
952  
953 # XXX: JSON::Validator 4 moved support for readOnly/writeOnly to
954 # the OpenAPI schema classes, but we use JSON Schema internally,
955 # so we need to make support ourselves for now...
956 0 ▶ if ( $prop->{readOnly} && exists $check_item{ $prop_name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prop->{"readOnly"} and exists $check_item{$prop_name}) 0 0 0
Condition0%0/3
$prop->{"readOnly"} and exists $check_item{$prop_name}
!ll&&!rl&&r
000
MC/DC0%0/2
$prop->{"readOnly"} and exists $check_item{$prop_name}
$prop->{"readOnly"} exists $check_item{$prop_name}
Truth table0%0/3
A and B
A= $prop->{"readOnly"}B= exists $check_item{$prop_name}
ABresult
0X0
100
111
957 0 push @errors, JSON::Validator::Error->new(
958 "/$prop_name", "Read-only.",
959 );
960 }
961 }
962  
963 0 push @errors, $v->validate( \%check_item );
964 0 return @errors;
965 }
966  
967 sub _helper_filter_apply {
968 0 ▶ my ( $self, $c, $schema_name, $item, $output ) = @_;
Subroutine _helper_filter_apply: not called CC=11 SCAR=48.8
969 0 ▶ my $filter_type = $output ? 'x-filter-output' : 'x-filter';
Branch: 1 branch
BranchTrueFalseTotal
$output ? : 0 0 0
970 0 my $schema = $c->yancy->schema( $schema_name );
971 0 my $filters = $self->_filters;
972 0 for my $key ( keys %{ $schema->{properties} } ) {
973 0 ▶ next unless my $prop_filters = $schema->{properties}{ $key }{ $filter_type };
Branch: 1 branch
BranchTrueFalseTotal
unless my $prop_filters = $schema->{"properties"}{$key}{$filter_type} 0 0 0
974 0 for my $filter ( @{ $prop_filters } ) {
975 0 ▶ ( $filter, my @params ) = @$filter if ref $filter eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $filter eq "ARRAY" 0 0 0
976 0 my $sub = $filters->{ $filter };
977 0 ▶ $c->log_die( "Unknown filter: $filter (schema: $schema_name, field: $key)" )
Branch: 1 branch
BranchTrueFalseTotal
unless $sub 0 0 0
978 unless $sub;
979 $item = { %$item, $key => $sub->(
980 0 $key, $item->{ $key }, $schema->{properties}{ $key }, @params
981 ) };
982 }
983 }
984 0 ▶ if ( my $schema_filters = $schema->{$filter_type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $schema_filters = $schema->{$filter_type}) 0 0 0
985 0 for my $filter ( @{ $schema_filters } ) {
986 0 ▶ ( $filter, my @params ) = @$filter if ref $filter eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $filter eq "ARRAY" 0 0 0
987 0 my $sub = $filters->{ $filter };
988 0 ▶ $c->log_die( "Unknown filter: $filter (schema: $schema_name)" )
Branch: 1 branch
BranchTrueFalseTotal
unless $sub 0 0 0
989 unless $sub;
990 0 $item = $sub->( $schema_name, $item, $schema, @params );
991 }
992 }
993 0 return $item;
994 }
995  
996 sub _helper_filter_add {
997 95 ▶ my ( $self, $c, $name, $sub ) = @_;
Subroutine _helper_filter_add: called CC=1 SCAR=0.0
998 95 $self->_filters->{ $name } = $sub;
999 }
1000  
1001 # _merge_schema( $keep, $merge );
1002 #
1003 # Merge the given $merge schema into the given $keep schema. $keep is
1004 # modified in-place (but also returned)
1005 sub _merge_schema {
1006 83 ▶ my ( $keep, $merge ) = @_;
Subroutine _merge_schema: called CC=8 SCAR=20.8
1007 83 ▶ my $keep_props = $keep->{properties} ||= {};
Condition100%2/2
$keep->{"properties"} ||= {}
l!l
7013
MC/DC100%1/1
$keep->{"properties"} ||= {}
$keep->{"properties"}
Truth table100%2/2
A
A= $keep->{"properties"}
Aresult
11
00
1008 83 my $merge_props = delete $merge->{properties};
1009 83 for my $k ( keys %$merge ) {
1010 236 ▶ $keep->{ $k } ||= $merge->{ $k };
Condition66%2/3
$keep->{$k} ||= $merge->{$k}
l!l&&r!l&&!r
201350
MC/DC0%0/2
$keep->{$k} ||= $merge->{$k}
$keep->{$k} $merge->{$k}
Truth table66%2/3
A ||= B
A= $keep->{$k}B= $merge->{$k}
ABresult
1X1
011
000
1011 }
1012 83 for my $p ( keys %{ $merge_props } ) {
1013 509 ▶ my $keep_prop = $keep_props->{ $p } ||= {};
Condition100%2/2
$keep_props->{$p} ||= {}
l!l
43376
MC/DC100%1/1
$keep_props->{$p} ||= {}
$keep_props->{$p}
Truth table100%2/2
A
A= $keep_props->{$p}
Aresult
11
00
1014 509 my $merge_prop = $merge_props->{ $p };
1015 509 for my $k ( keys %$merge_prop ) {
1016 1301 ▶ $keep_prop->{ $k } ||= $merge_prop->{ $k };
Condition100%3/3
$keep_prop->{$k} ||= $merge_prop->{$k}
l!l&&r!l&&!r
104818865
MC/DC100%2/2
$keep_prop->{$k} ||= $merge_prop->{$k}
$keep_prop->{$k} $merge_prop->{$k}
Truth table100%3/3
A ||= B
A= $keep_prop->{$k}B= $merge_prop->{$k}
ABresult
1X1
011
000
1017 }
1018 }
1019 83 return $keep;
1020 }
1021  
1022 sub _helper_routify {
1023 19 ▶ my ( $self, @args ) = @_;
Subroutine _helper_routify: called CC=5 SCAR=17.1
1024 19 for my $maybe_route ( @args ) {
1025 38 ▶ next unless defined $maybe_route;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $maybe_route 19 19 38
1026 19 ▶ return blessed $maybe_route && $maybe_route->isa( 'Mojolicious::Routes::Route' )
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($maybe_route) && $maybe_route->isa('Mojolicious::Routes::Route') ? : 0 19 19
Condition33%1/3
builtin::blessed($maybe_route) and $maybe_route->isa("Mojolicious::Routes::Route")
!ll&&!rl&&r
1900
MC/DC0%0/2
builtin::blessed($maybe_route) and $maybe_route->isa("Mojolicious::Routes::Route")
builtin::blessed($maybe_route) $maybe_route->isa("Mojolicious::Routes::Route")
Truth table33%1/3
A and B
A= builtin::blessed($maybe_route)B= $maybe_route->isa("Mojolicious::Routes::Route")
ABresult
0X0
100
111
1027 ? $maybe_route
1028 : $self->app->routes->any( $maybe_route )
1029 ;
1030 }
1031 }
1032  
1033 sub _helper_log_die {
1034 0 ▶ my ( $self, $class, $err ) = @_;
Subroutine _helper_log_die: not called CC=4 SCAR=30.0
1035 # XXX: Handle JSON::Validator errors
1036 0 ▶ if ( !$err ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($err) 0 0 0
1037 0 $err = $class;
1038 0 $class = 'Mojo::Exception';
1039 }
1040 0 ▶ if ( !$class->can( 'new' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->can("new")) 0 0 0
1041 0 ▶ die $@ unless eval "package $class; use Mojo::Base 'Mojo::Exception'; 1";
Branch: 1 branch
BranchTrueFalseTotal
unless eval "package $class; use Mojo::Base 'Mojo::Exception'; 1" 0 0 0
1042 }
1043 0 my $e = $class->new( $err )->trace( 2 );
1044 0 $self->log->fatal( $e );
1045 0 die $e;
1046 }
1047  
1048 20 1;
1049  
1050 __END__
« Yancy/Plugin/File.pm ↑ Summary Yancy/Backend.pm »