lib/App/Dapper/Schema.pm

filter: Statementstmt 19.0% 25 / 131 Branchbran 0.0% 0 / 116 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 63.6% 7 / 11 Podpod 100% 4 / 4 totaltotal 11.2% 36 / 320 CC 77 SCAR 67.7
CC
77
Cov
9%
check_value
1406.0
load
812.0
validate
90.0
CRAP 871.6
↑ Summary lib/App/Dapper/Publish.pm »
1 package App::Dapper::Schema;
2  
3 =head1 NAME
4  
5 App::Dapper::Schema - Check front matter against a C<_schema.yml> file.
6  
7 =head1 DESCRIPTION
8  
9 A C<_schema.yml> file in a source directory describes the front matter that
10 pages in that directory (and the directories below it, unless they have a
11 schema of their own) must have. For example:
12  
13 required: [title, date]
14 fields:
15 title:
16 type: string
17 max_length: 70
18 date:
19 type: date
20 categories:
21 type: list
22 values: [blog, notes]
23 description:
24 type: string
25 max_length: 160
26 draft:
27 type: boolean
28 additional: true
29  
30 C<required> lists the fields every page must set. C<fields> describes each
31 field:
32  
33 type string, number, integer, boolean, date, list, map or any
34 values the allowed values (for a list: the allowed items)
35 pattern a regular expression a string must match
36 min_length, max_length limits on the length of a string
37 min, max limits on a number
38  
39 C<additional: false> makes any field not listed in C<fields> an error.
40  
41 =cut
42  
43 32 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 32 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 32 ▶use Scalar::Util qw/ looks_like_number blessed /;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 32 ▶use YAML::PP qw/ LoadFile /;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 32 ▶use App::Dapper::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 32 my %TYPES = map { $_ => 1 } qw/ string number integer boolean date list map any /;
54 32 my %RULES = map { $_ => 1 } qw/ type values pattern min_length max_length min max /;
55  
56 =head2 load
57  
58 Takes the path of a schema file, reads it, checks that it is a valid schema,
59 and returns it. Dies with a message naming the file if it is not.
60  
61 =cut
62  
63 0 ▶sub load {
Subroutine load: not called CC=28 SCAR=67.0
Pod documented
64 0 my ($file) = @_;
65  
66 0 my $schema = eval { LoadFile($file) };
67 0 ▶ die "error: could not parse the schema \"$file\": $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
68 0 ▶ $schema = {} unless defined $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $schema 0 0 0
69 0 ▶ die "error: the schema \"$file\" must contain key: value pairs\n" unless ref $schema eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema eq "HASH" 0 0 0
70  
71 0 my @problems;
72  
73 0 for my $key (sort keys %$schema) {
74 0 ▶ push @problems, "unknown setting \"$key\" (use required, fields or additional)"
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /^(?:required|fields|additional)$/ 0 0 0
75 unless $key =~ /^(?:required|fields|additional)$/;
76 }
77  
78 0 my $required = $schema->{required};
79 0 ▶ $required = [] unless defined $required;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $required 0 0 0
80 0 ▶ $required = [$required] unless ref $required;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $required 0 0 0
81 0 ▶ push @problems, "\"required\" must be a list of field names" unless ref $required eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $required eq "ARRAY" 0 0 0
82 0 $schema->{required} = $required;
83  
84 0 my $fields = $schema->{fields};
85 0 ▶ $fields = {} unless defined $fields;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fields 0 0 0
86 0 ▶ push @problems, "\"fields\" must be a set of field names" unless ref $fields eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fields eq "HASH" 0 0 0
87 0 ▶ $schema->{fields} = ref $fields eq 'HASH' ? $fields : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $fields eq 'HASH' ? : 0 0 0
88  
89 0 for my $name (sort keys %{$schema->{fields}}) {
90 0 my $rules = $schema->{fields}->{$name};
91 0 ▶ $rules = { type => $rules } if defined $rules and not ref $rules;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rules and not ref $rules 0 0 0
Condition0%0/3
defined $rules and not ref $rules
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rules and not ref $rules
defined $rules not ref $rules
Truth table0%0/3
A and not(B)
A= defined $rulesB= not ref $rules
ABresult
0X0
100
111
92 0 ▶ $rules = {} unless defined $rules;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rules 0 0 0
93 0 ▶ if (ref $rules ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rules ne "HASH") 0 0 0
94 0 push @problems, "the rules for \"$name\" must be a set of key: value pairs";
95 0 next;
96 }
97 0 $schema->{fields}->{$name} = $rules;
98  
99 0 ▶ $rules->{type} = 'any' unless defined $rules->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rules->{"type"} 0 0 0
100 push @problems, "\"$name\" has the unknown type \"$rules->{type}\" (use " . join(', ', sort keys %TYPES) . ")"
101 0 ▶ unless $TYPES{$rules->{type}};
Branch: 1 branch
BranchTrueFalseTotal
unless $TYPES{$rules->{"type"}} 0 0 0
102  
103 0 for my $rule (sort keys %$rules) {
104 0 ▶ push @problems, "\"$name\" has the unknown rule \"$rule\"" unless $RULES{$rule};
Branch: 1 branch
BranchTrueFalseTotal
unless $RULES{$rule} 0 0 0
105 }
106  
107 0 ▶ if (defined $rules->{values}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rules->{"values"}) 0 0 0
108 0 ▶ $rules->{values} = [$rules->{values}] unless ref $rules->{values};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rules->{"values"} 0 0 0
109 0 ▶ push @problems, "\"values\" for \"$name\" must be a list" unless ref $rules->{values} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rules->{"values"} eq "ARRAY" 0 0 0
110 }
111  
112 0 ▶ if (defined $rules->{pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rules->{"pattern"}) 0 0 0
113 0 my $pattern = $rules->{pattern};
114 push @problems, "the pattern for \"$name\" is not a valid regular expression"
115 0 ▶ unless eval { qr/$pattern/; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { qr/$pattern/; 1 } } 0 0 0
116 }
117 }
118  
119 0 ▶ $schema->{additional} = 1 unless defined $schema->{additional};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $schema->{"additional"} 0 0 0
120  
121 0 ▶ die "error: the schema \"$file\" has problems:\n" . join('', map { " $_\n" } @problems) if @problems;
Branch: 1 branch
BranchTrueFalseTotal
if @problems 0 0 0
122  
123 0 $schema->{file} = $file;
124 0 return $schema;
125 }
126  
127 =head2 validate
128  
129 Takes a schema (from L</load>) and a hash of front matter, and returns a list
130 of messages describing every way the front matter does not match the schema.
131 An empty list means it matches.
132  
133 =cut
134  
135 0 ▶sub validate {
Subroutine validate: not called CC=9 SCAR=45.0
Pod documented
136 0 my ($schema, $frontmatter) = @_;
137  
138 0 my @problems;
139  
140 0 for my $name (@{$schema->{required}}) {
141 push @problems, "\"$name\" is required"
142 0 ▶ unless defined $frontmatter->{$name} and (ref $frontmatter->{$name} or length $frontmatter->{$name});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $frontmatter->{$name} and ref $frontmatter->{$name} || length $frontmatter->{$name} 0 0 0
Condition0%0/6
defined $frontmatter->{$name} and ref $frontmatter->{$name} || length $frontmatter->{$name}
!ll&&!rl&&r
000
ref $frontmatter->{$name} or length $frontmatter->{$name}
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $frontmatter->{$name} and ref $frontmatter->{$name} || length $frontmatter->{$name}
defined $frontmatter->{$name} ref $frontmatter->{$name} length $frontmatter->{$name}
Truth table0%0/4
A and B or C
A= defined $frontmatter->{$name}B= ref $frontmatter->{$name}C= length $frontmatter->{$name}
ABCresult
0XX0
1000
11X1
1011
143 }
144  
145 0 for my $name (sort keys %$frontmatter) {
146 0 my $rules = $schema->{fields}->{$name};
147 0 ▶ if (not $rules) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rules) 0 0 0
148 0 ▶ push @problems, "\"$name\" is not one of the fields in the schema" unless $schema->{additional};
Branch: 1 branch
BranchTrueFalseTotal
unless $schema->{"additional"} 0 0 0
149 0 next;
150 }
151 0 ▶ next unless defined $frontmatter->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $frontmatter->{$name} 0 0 0
152 0 push @problems, check_value($name, $frontmatter->{$name}, $rules);
153 }
154  
155 0 return @problems;
156 }
157  
158 =head2 check_value
159  
160 Takes a field name, its value and its rules, and returns a list of messages
161 for each rule the value breaks.
162  
163 =cut
164  
165 0 ▶sub check_value {
Subroutine check_value: not called CC=37 SCAR=72.5
Pod documented
166 0 my ($name, $value, $rules) = @_;
167  
168 0 my $type = $rules->{type};
169  
170 0 ▶ if ($type eq 'list') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "list") 0 0 0
171 0 ▶ my @items = ref $value eq 'ARRAY' ? @$value : ref $value ? () : ($value);
Branch: 2 branches
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 0 0 0
ref $value ? : 0 0 0
172 0 ▶ return "\"$name\" must be a list" if ref $value and ref $value ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value and ref $value ne "ARRAY" 0 0 0
Condition0%0/3
ref $value and ref $value ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $value and ref $value ne "ARRAY"
ref $value ref $value ne "ARRAY"
Truth table0%0/3
A and B
A= ref $valueB= ref $value ne "ARRAY"
ABresult
0X0
100
111
173 0 my @problems;
174 0 ▶ if ($rules->{values}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rules->{"values"}) 0 0 0
175 0 my %allowed = map { $_ => 1 } @{$rules->{values}};
176 0 for my $item (@items) {
177 0 push @problems, "\"$name\" has \"$item\", which is not one of: " . join(', ', @{$rules->{values}})
178 0 ▶ unless defined $item and $allowed{$item};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item and $allowed{$item} 0 0 0
Condition0%0/3
defined $item and $allowed{$item}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $item and $allowed{$item}
defined $item $allowed{$item}
Truth table0%0/3
A and B
A= defined $itemB= $allowed{$item}
ABresult
0X0
100
111
179 }
180 }
181 0 return @problems;
182 }
183  
184 0 ▶ if ($type eq 'map') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "map") 0 0 0
185 0 ▶ return ref $value eq 'HASH' ? () : "\"$name\" must be a set of key: value pairs";
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'HASH' ? : 0 0 0
186 }
187  
188 0 ▶ if ($type eq 'boolean') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "boolean") 0 0 0
189 0 ▶ return () if is_boolean($value);
Branch: 1 branch
BranchTrueFalseTotal
if is_boolean($value) 0 0 0
190 0 return "\"$name\" must be true or false";
191 }
192  
193 0 ▶ return () if $type eq 'any';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "any" 0 0 0
194  
195 0 ▶ return "\"$name\" must be a single value, not a list or a set" if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 0 0
196  
197 0 my @problems;
198  
199 0 ▶ if ($type eq 'number') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'number') { } 0 0 0
elsif ($type eq 'integer') { } 0 0 0
elsif ($type eq 'date') { } 0 0 0
200 0 ▶ return "\"$name\" must be a number" unless looks_like_number($value);
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($value) 0 0 0
201 }
202 elsif ($type eq 'integer') {
203 0 ▶ return "\"$name\" must be a whole number" unless $value =~ /^-?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^-?\d+$/ 0 0 0
204 }
205 elsif ($type eq 'date') {
206 return "\"$name\" must be a date, e.g. 2014-05-02T10:30:00"
207 0 ▶ unless eval { App::Dapper::Utils::parse_date($value); 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { App::Dapper::Utils::parse_date($value); 1 } } 0 0 0
208 }
209  
210 0 ▶ if ($type eq 'number' or $type eq 'integer') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "number" or $type eq "integer") 0 0 0
Condition0%0/3
$type eq "number" or $type eq "integer"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "number" or $type eq "integer"
$type eq "number" $type eq "integer"
Truth table0%0/3
A or B
A= $type eq "number"B= $type eq "integer"
ABresult
1X1
011
000
211 push @problems, "\"$name\" is $value; the smallest allowed is $rules->{min}"
212 0 ▶ if defined $rules->{min} and $value < $rules->{min};
Branch: 1 branch
BranchTrueFalseTotal
if defined $rules->{"min"} and $value < $rules->{"min"} 0 0 0
Condition0%0/3
defined $rules->{"min"} and $value < $rules->{"min"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rules->{"min"} and $value < $rules->{"min"}
defined $rules->{"min"} $value < $rules->{"min"}
Truth table0%0/3
A and B
A= defined $rules->{"min"}B= $value < $rules->{"min"}
ABresult
0X0
100
111
213 push @problems, "\"$name\" is $value; the largest allowed is $rules->{max}"
214 0 ▶ if defined $rules->{max} and $value > $rules->{max};
Branch: 1 branch
BranchTrueFalseTotal
if defined $rules->{"max"} and $value > $rules->{"max"} 0 0 0
Condition0%0/3
defined $rules->{"max"} and $value > $rules->{"max"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rules->{"max"} and $value > $rules->{"max"}
defined $rules->{"max"} $value > $rules->{"max"}
Truth table0%0/3
A and B
A= defined $rules->{"max"}B= $value > $rules->{"max"}
ABresult
0X0
100
111
215 }
216  
217 0 ▶ if ($type eq 'string') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "string") 0 0 0
218 0 my $length = length $value;
219 push @problems, "\"$name\" is $length characters long; the fewest allowed is $rules->{min_length}"
220 0 ▶ if defined $rules->{min_length} and $length < $rules->{min_length};
Branch: 1 branch
BranchTrueFalseTotal
if defined $rules->{"min_length"} and $length < $rules->{"min_length"} 0 0 0
Condition0%0/3
defined $rules->{"min_length"} and $length < $rules->{"min_length"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rules->{"min_length"} and $length < $rules->{"min_length"}
defined $rules->{"min_length"} $length < $rules->{"min_length"}
Truth table0%0/3
A and B
A= defined $rules->{"min_length"}B= $length < $rules->{"min_length"}
ABresult
0X0
100
111
221 push @problems, "\"$name\" is $length characters long; the most allowed is $rules->{max_length}"
222 0 ▶ if defined $rules->{max_length} and $length > $rules->{max_length};
Branch: 1 branch
BranchTrueFalseTotal
if defined $rules->{"max_length"} and $length > $rules->{"max_length"} 0 0 0
Condition0%0/3
defined $rules->{"max_length"} and $length > $rules->{"max_length"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rules->{"max_length"} and $length > $rules->{"max_length"}
defined $rules->{"max_length"} $length > $rules->{"max_length"}
Truth table0%0/3
A and B
A= defined $rules->{"max_length"}B= $length > $rules->{"max_length"}
ABresult
0X0
100
111
223 0 ▶ if (defined $rules->{pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rules->{"pattern"}) 0 0 0
224 0 my $pattern = $rules->{pattern};
225 0 ▶ push @problems, "\"$name\" does not match the pattern $pattern" unless $value =~ /$pattern/;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /$pattern/ 0 0 0
226 }
227 }
228  
229 0 ▶ if ($rules->{values}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rules->{"values"}) 0 0 0
230 0 my %allowed = map { $_ => 1 } @{$rules->{values}};
231 0 push @problems, "\"$name\" is \"$value\", which is not one of: " . join(', ', @{$rules->{values}})
232 0 ▶ unless $allowed{$value};
Branch: 1 branch
BranchTrueFalseTotal
unless $allowed{$value} 0 0 0
233 }
234  
235 0 return @problems;
236 }
237  
238 =head2 is_boolean
239  
240 Returns true if a value is a YAML or JSON boolean, or one of 1, 0, the empty
241 string, "true" or "false".
242  
243 =cut
244  
245 0 ▶sub is_boolean {
Subroutine is_boolean: not called CC=6 SCAR=37.4
Pod documented
246 0 my ($value) = @_;
247  
248 0 ▶ return 1 if blessed($value) and ($value->isa('JSON::PP::Boolean') or $value->isa('boolean'));
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($value) and $value->isa("JSON::PP::Boolean") || $value->isa("boolean") 0 0 0
Condition0%0/6
builtin::blessed($value) and $value->isa("JSON::PP::Boolean") || $value->isa("boolean")
!ll&&!rl&&r
000
$value->isa("JSON::PP::Boolean") or $value->isa("boolean")
l!l&&r!l&&!r
000
MC/DC0%0/3
builtin::blessed($value) and $value->isa("JSON::PP::Boolean") || $value->isa("boolean")
builtin::blessed($value) $value->isa("JSON::PP::Boolean") $value->isa("boolean")
Truth table0%0/4
A and B or C
A= builtin::blessed($value)B= $value->isa("JSON::PP::Boolean")C= $value->isa("boolean")
ABCresult
0XX0
1000
11X1
1011
249 0 ▶ return 0 if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 0 0
250 0 ▶ return $value =~ /^(?:1|0|true|false|)$/i ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /^(?:1|0|true|false|)$/i ? : 0 0 0
251 }
252  
253 32 1;
↑ Summary lib/App/Dapper/Publish.pm »