lib/App/Dapper/Schema.pm

filter: Statementstmt 93.8% 137 / 146 Branchbran 96.7% 118 / 122 Conditioncond 85.7% 36 / 42 MC/DCmcdc 88.0% 22 / 25 Subroutinesub 92.8% 13 / 14 Podpod 100% 5 / 5 totaltotal 93.5% 331 / 354 CC 82 SCAR 32.7
CC
82
Cov
94%
check_value
38.0
for_file
30.0
load
28.0
CRAP 26.4
« lib/App/Dapper/MCP.pm ↑ Summary lib/App/Dapper/Components.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 191 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 191 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 191 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 191 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 191 ▶use Scalar::Util qw/ looks_like_number blessed /;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 191 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
50 191 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
51 191 ▶use YAML::PP qw/ LoadFile /;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 191 ▶use App::Dapper::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 191 my %TYPES = map { $_ => 1 } qw/ string number integer boolean date list map any /;
56 191 my %RULES = map { $_ => 1 } qw/ type values pattern min_length max_length min max /;
57  
58 =head2 load
59  
60 Takes the path of a schema file, reads it, checks that it is a valid schema,
61 and returns it. Dies with a message naming the file if it is not.
62  
63 =cut
64  
65 44 ▶sub load {
Subroutine load: called CC=28 SCAR=33.3
Pod documented
66 44 my ($file) = @_;
67  
68 44 my $schema = eval { LoadFile($file) };
69 44 ▶ die "error: could not parse the schema \"$file\": $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 42 44
70 42 ▶ $schema = {} unless defined $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $schema 2 40 42
71 42 ▶ 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" 2 40 42
72  
73 40 my @problems;
74  
75 40 for my $key (sort keys %$schema) {
76 45 ▶ push @problems, "unknown setting \"$key\" (use required, fields or additional)"
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /^(?:required|fields|additional)$/ 3 42 45
77 unless $key =~ /^(?:required|fields|additional)$/;
78 }
79  
80 40 my $required = $schema->{required};
81 40 ▶ $required = [] unless defined $required;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $required 29 11 40
82 40 ▶ $required = [$required] unless ref $required;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $required 1 39 40
83 40 ▶ push @problems, "\"required\" must be a list of field names" unless ref $required eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $required eq "ARRAY" 2 38 40
84 40 $schema->{required} = $required;
85  
86 40 my $fields = $schema->{fields};
87 40 ▶ $fields = {} unless defined $fields;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fields 14 26 40
88 40 ▶ push @problems, "\"fields\" must be a set of field names" unless ref $fields eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fields eq "HASH" 2 38 40
89 40 ▶ $schema->{fields} = ref $fields eq 'HASH' ? $fields : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $fields eq 'HASH' ? : 38 2 40
90  
91 40 for my $name (sort keys %{$schema->{fields}}) {
92 29 my $rules = $schema->{fields}->{$name};
93 29 ▶ $rules = { type => $rules } if defined $rules and not ref $rules;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rules and not ref $rules 7 22 29
Condition100%3/3
defined $rules and not ref $rules
!ll&&!rl&&r
1217
MC/DC100%2/2
defined $rules and not ref $rules
defined $rules not ref $rules
Truth table100%3/3
A and not(B)
A= defined $rulesB= not ref $rules
ABresult
0X0
100
111
94 29 ▶ $rules = {} unless defined $rules;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rules 1 28 29
95 29 ▶ if (ref $rules ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rules ne "HASH") 2 27 29
96 2 push @problems, "the rules for \"$name\" must be a set of key: value pairs";
97 2 next;
98 }
99 27 $schema->{fields}->{$name} = $rules;
100  
101 27 ▶ $rules->{type} = 'any' unless defined $rules->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rules->{"type"} 5 22 27
102 push @problems, "\"$name\" has the unknown type \"$rules->{type}\" (use " . join(', ', sort keys %TYPES) . ")"
103 27 ▶ unless $TYPES{$rules->{type}};
Branch: 1 branch
BranchTrueFalseTotal
unless $TYPES{$rules->{"type"}} 3 24 27
104  
105 27 for my $rule (sort keys %$rules) {
106 43 ▶ push @problems, "\"$name\" has the unknown rule \"$rule\"" unless $RULES{$rule};
Branch: 1 branch
BranchTrueFalseTotal
unless $RULES{$rule} 3 40 43
107 }
108  
109 27 ▶ if (defined $rules->{values}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rules->{"values"}) 3 24 27
110 3 ▶ $rules->{values} = [$rules->{values}] unless ref $rules->{values};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rules->{"values"} 1 2 3
111 3 ▶ 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" 1 2 3
112 }
113  
114 27 ▶ if (defined $rules->{pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rules->{"pattern"}) 3 24 27
115 3 my $pattern = $rules->{pattern};
116 push @problems, "the pattern for \"$name\" is not a valid regular expression"
117 3 ▶ unless eval { qr/$pattern/; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { qr/$pattern/; 1 } } 2 1 3
118 }
119 }
120  
121 40 ▶ $schema->{additional} = 1 unless defined $schema->{additional};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $schema->{"additional"} 35 5 40
122  
123 40 ▶ die "error: the schema \"$file\" has problems:\n" . join('', map { " $_\n" } @problems) if @problems;
Branch: 1 branch
BranchTrueFalseTotal
if @problems 11 29 40
124  
125 29 $schema->{file} = $file;
126 29 return $schema;
127 }
128  
129 =head2 for_file
130  
131 Takes the source directory and a file in it, and returns the schema that
132 applies to the file (from the nearest C<_schema.yml> at or above its
133 directory), or nothing.
134  
135 =cut
136  
137 0 ▶sub for_file {
Subroutine for_file: not called CC=5 SCAR=34.0
Pod documented
138 0 my ($source, $file) = @_;
139  
140 0 $source = File::Spec->canonpath($source);
141 0 my $dir = File::Basename::dirname($file);
142 0 while (1) {
143 0 my $schema_file = File::Spec->catfile($dir, '_schema.yml');
144 0 ▶ return load($schema_file) if -f $schema_file;
Branch: 1 branch
BranchTrueFalseTotal
if -f $schema_file 0 0 0
145 0 ▶ last if File::Spec->canonpath($dir) eq $source or $dir eq '.' or $dir eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if "File::Spec"->canonpath($dir) eq $source or $dir eq "." or $dir eq "/" 0 0 0
Condition0%0/6
"File::Spec"->canonpath($dir) eq $source or $dir eq "." or $dir eq "/"
l!l&&r!l&&!r
000
"File::Spec"->canonpath($dir) eq $source or $dir eq "."
l!l&&r!l&&!r
000
MC/DC0%0/3
"File::Spec"->canonpath($dir) eq $source or $dir eq "." or $dir eq "/"
"File::Spec"->canonpath($dir) eq $source $dir eq "." $dir eq "/"
Truth table0%0/4
A or B or C
A= "File::Spec"->canonpath($dir) eq $sourceB= $dir eq "."C= $dir eq "/"
ABCresult
1XX1
01X1
0011
0000
146 0 $dir = File::Basename::dirname($dir);
147 }
148 0 return;
149 }
150  
151 =head2 validate
152  
153 Takes a schema (from L</load>) and a hash of front matter, and returns a list
154 of messages describing every way the front matter does not match the schema.
155 An empty list means it matches.
156  
157 =cut
158  
159 12 ▶sub validate {
Subroutine validate: called CC=9 SCAR=22.0
Pod documented
160 12 my ($schema, $frontmatter) = @_;
161  
162 12 my @problems;
163  
164 12 for my $name (@{$schema->{required}}) {
165 push @problems, "\"$name\" is required"
166 13 ▶ 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} 5 8 13
Condition100%6/6
defined $frontmatter->{$name} and ref $frontmatter->{$name} || length $frontmatter->{$name}
!ll&&!rl&&r
418
ref $frontmatter->{$name} or length $frontmatter->{$name}
l!l&&r!l&&!r
261
MC/DC100%3/3
defined $frontmatter->{$name} and ref $frontmatter->{$name} || length $frontmatter->{$name}
defined $frontmatter->{$name} ref $frontmatter->{$name} length $frontmatter->{$name}
Truth table100%4/4
A and B or C
A= defined $frontmatter->{$name}B= ref $frontmatter->{$name}C= length $frontmatter->{$name}
ABCresult
0XX0
1000
11X1
1011
167 }
168  
169 12 for my $name (sort keys %$frontmatter) {
170 22 my $rules = $schema->{fields}->{$name};
171 22 ▶ if (not $rules) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rules) 14 8 22
172 14 ▶ push @problems, "\"$name\" is not one of the fields in the schema" unless $schema->{additional};
Branch: 1 branch
BranchTrueFalseTotal
unless $schema->{"additional"} 4 10 14
173 14 next;
174 }
175 8 ▶ next unless defined $frontmatter->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $frontmatter->{$name} 1 7 8
176 7 push @problems, check_value($name, $frontmatter->{$name}, $rules);
177 }
178  
179 12 return @problems;
180 }
181  
182 =head2 check_value
183  
184 Takes a field name, its value and its rules, and returns a list of messages
185 for each rule the value breaks.
186  
187 =cut
188  
189 90 ▶sub check_value {
Subroutine check_value: called CC=38 SCAR=36.4
Pod documented
190 90 my ($name, $value, $rules) = @_;
191  
192 90 my $type = $rules->{type};
193  
194 90 ▶ if ($type eq 'list') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "list") 8 82 90
195 8 ▶ my @items = ref $value eq 'ARRAY' ? @$value : ref $value ? () : ($value);
Branch: 2 branches
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 5 3 8
ref $value ? : 1 2 3
196 8 ▶ 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" 1 7 8
Condition100%3/3
ref $value and ref $value ne "ARRAY"
!ll&&!rl&&r
251
MC/DC100%2/2
ref $value and ref $value ne "ARRAY"
ref $value ref $value ne "ARRAY"
Truth table100%3/3
A and B
A= ref $valueB= ref $value ne "ARRAY"
ABresult
0X0
100
111
197 7 my @problems;
198 7 ▶ if ($rules->{values}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rules->{"values"}) 4 3 7
199 4 my %allowed = map { $_ => 1 } @{$rules->{values}};
200 4 for my $item (@items) {
201 4 push @problems, "\"$name\" has \"" . (defined $item ? $item : '') . "\", which is not one of: " . join(', ', @{$rules->{values}})
202 7 ▶ unless defined $item and $allowed{$item};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $item and $allowed{$item} 4 3 7
defined $item ? : 3 1 4
Condition100%3/3
defined $item and $allowed{$item}
!ll&&!rl&&r
133
MC/DC100%2/2
defined $item and $allowed{$item}
defined $item $allowed{$item}
Truth table100%3/3
A and B
A= defined $itemB= $allowed{$item}
ABresult
0X0
100
111
203 }
204 }
205 7 return @problems;
206 }
207  
208 82 ▶ if ($type eq 'map') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "map") 4 78 82
209 4 ▶ return ref $value eq 'HASH' ? () : "\"$name\" must be a set of key: value pairs";
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'HASH' ? : 2 2 4
210 }
211  
212 78 ▶ if ($type eq 'boolean') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "boolean") 18 60 78
213 18 ▶ return () if is_boolean($value);
Branch: 1 branch
BranchTrueFalseTotal
if is_boolean($value) 9 9 18
214 9 return "\"$name\" must be true or false";
215 }
216  
217 60 ▶ return () if $type eq 'any';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "any" 4 56 60
218  
219 56 ▶ return "\"$name\" must be a single value, not a list or a set" if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 4 52 56
220  
221 52 my @problems;
222  
223 52 ▶ if ($type eq 'number') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'number') { } 10 42 52
elsif ($type eq 'integer') { } 14 28 42
elsif ($type eq 'date') { } 9 19 28
224 10 ▶ return "\"$name\" must be a number" unless looks_like_number($value);
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($value) 3 7 10
225 }
226 elsif ($type eq 'integer') {
227 14 ▶ return "\"$name\" must be a whole number" unless $value =~ /^-?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^-?\d+$/ 5 9 14
228 }
229 elsif ($type eq 'date') {
230 return "\"$name\" must be a date, e.g. 2014-05-02T10:30:00"
231 9 ▶ unless eval { App::Dapper::Utils::parse_date($value); 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { App::Dapper::Utils::parse_date($value); 1 } } 3 6 9
232 }
233  
234 41 ▶ if ($type eq 'number' or $type eq 'integer') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "number" or $type eq "integer") 16 25 41
Condition100%3/3
$type eq "number" or $type eq "integer"
l!l&&r!l&&!r
7925
MC/DC100%2/2
$type eq "number" or $type eq "integer"
$type eq "number" $type eq "integer"
Truth table100%3/3
A or B
A= $type eq "number"B= $type eq "integer"
ABresult
1X1
011
000
235 push @problems, "\"$name\" is $value; the smallest allowed is $rules->{min}"
236 16 ▶ if defined $rules->{min} and $value < $rules->{min};
Branch: 1 branch
BranchTrueFalseTotal
if defined $rules->{"min"} and $value < $rules->{"min"} 3 13 16
Condition100%3/3
defined $rules->{"min"} and $value < $rules->{"min"}
!ll&&!rl&&r
1213
MC/DC100%2/2
defined $rules->{"min"} and $value < $rules->{"min"}
defined $rules->{"min"} $value < $rules->{"min"}
Truth table100%3/3
A and B
A= defined $rules->{"min"}B= $value < $rules->{"min"}
ABresult
0X0
100
111
237 push @problems, "\"$name\" is $value; the largest allowed is $rules->{max}"
238 16 ▶ if defined $rules->{max} and $value > $rules->{max};
Branch: 1 branch
BranchTrueFalseTotal
if defined $rules->{"max"} and $value > $rules->{"max"} 3 13 16
Condition100%3/3
defined $rules->{"max"} and $value > $rules->{"max"}
!ll&&!rl&&r
1213
MC/DC100%2/2
defined $rules->{"max"} and $value > $rules->{"max"}
defined $rules->{"max"} $value > $rules->{"max"}
Truth table100%3/3
A and B
A= defined $rules->{"max"}B= $value > $rules->{"max"}
ABresult
0X0
100
111
239 }
240  
241 41 ▶ if ($type eq 'string') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "string") 19 22 41
242 19 my $length = length $value;
243 push @problems, "\"$name\" is $length characters long; the fewest allowed is $rules->{min_length}"
244 19 ▶ if defined $rules->{min_length} and $length < $rules->{min_length};
Branch: 1 branch
BranchTrueFalseTotal
if defined $rules->{"min_length"} and $length < $rules->{"min_length"} 2 17 19
Condition100%3/3
defined $rules->{"min_length"} and $length < $rules->{"min_length"}
!ll&&!rl&&r
1612
MC/DC100%2/2
defined $rules->{"min_length"} and $length < $rules->{"min_length"}
defined $rules->{"min_length"} $length < $rules->{"min_length"}
Truth table100%3/3
A and B
A= defined $rules->{"min_length"}B= $length < $rules->{"min_length"}
ABresult
0X0
100
111
245 push @problems, "\"$name\" is $length characters long; the most allowed is $rules->{max_length}"
246 19 ▶ if defined $rules->{max_length} and $length > $rules->{max_length};
Branch: 1 branch
BranchTrueFalseTotal
if defined $rules->{"max_length"} and $length > $rules->{"max_length"} 3 16 19
Condition100%3/3
defined $rules->{"max_length"} and $length > $rules->{"max_length"}
!ll&&!rl&&r
1423
MC/DC100%2/2
defined $rules->{"max_length"} and $length > $rules->{"max_length"}
defined $rules->{"max_length"} $length > $rules->{"max_length"}
Truth table100%3/3
A and B
A= defined $rules->{"max_length"}B= $length > $rules->{"max_length"}
ABresult
0X0
100
111
247 19 ▶ if (defined $rules->{pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rules->{"pattern"}) 4 15 19
248 4 my $pattern = $rules->{pattern};
249 4 ▶ push @problems, "\"$name\" does not match the pattern $pattern" unless $value =~ /$pattern/;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /$pattern/ 2 2 4
250 }
251 }
252  
253 41 ▶ if ($rules->{values}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rules->{"values"}) 6 35 41
254 6 my %allowed = map { $_ => 1 } @{$rules->{values}};
255 4 push @problems, "\"$name\" is \"$value\", which is not one of: " . join(', ', @{$rules->{values}})
256 6 ▶ unless $allowed{$value};
Branch: 1 branch
BranchTrueFalseTotal
unless $allowed{$value} 4 2 6
257 }
258  
259 41 return @problems;
260 }
261  
262 =head2 is_boolean
263  
264 Returns true if a value is a YAML or JSON boolean, or one of 1, 0, the empty
265 string, "true" or "false".
266  
267 =cut
268  
269 30 ▶sub is_boolean {
Subroutine is_boolean: called CC=6 SCAR=17.9
Pod documented
270 30 my ($value) = @_;
271  
272 30 ▶ 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") 5 25 30
Condition100%6/6
builtin::blessed($value) and $value->isa("JSON::PP::Boolean") || $value->isa("boolean")
!ll&&!rl&&r
2415
$value->isa("JSON::PP::Boolean") or $value->isa("boolean")
l!l&&r!l&&!r
411
MC/DC100%3/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 table100%4/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
273 25 ▶ return 0 if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 4 21 25
274 21 ▶ return $value =~ /^(?:1|0|true|false|)$/i ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /^(?:1|0|true|false|)$/i ? : 11 10 21
275 }
276  
277 191 1;
« lib/App/Dapper/MCP.pm ↑ Summary lib/App/Dapper/Components.pm »