| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 42 | ▶ | $schema = {} unless defined $schema; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 42 | ▶ | die "error: the schema \"$file\" must contain key: value pairs\n" unless ref $schema eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | unless $key =~ /^(?:required|fields|additional)$/; | ||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 40 | my $required = $schema->{required}; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 40 | ▶ | $required = [] unless defined $required; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 40 | ▶ | $required = [$required] unless ref $required; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 40 | ▶ | push @problems, "\"required\" must be a list of field names" unless ref $required eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 40 | $schema->{required} = $required; | |||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 40 | my $fields = $schema->{fields}; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 40 | ▶ | $fields = {} unless defined $fields; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 40 | ▶ | push @problems, "\"fields\" must be a set of field names" unless ref $fields eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 40 | ▶ | $schema->{fields} = ref $fields eq 'HASH' ? $fields : {}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $rules and not ref $rules
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
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 29 | ▶ | $rules = {} unless defined $rules; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 29 | ▶ | if (ref $rules ne 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | push @problems, "\"$name\" has the unknown type \"$rules->{type}\" (use " . join(', ', sort keys %TYPES) . ")" | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 27 | ▶ | unless $TYPES{$rules->{type}}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 27 | ▶ | if (defined $rules->{values}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 3 | ▶ | $rules->{values} = [$rules->{values}] unless ref $rules->{values}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 3 | ▶ | push @problems, "\"values\" for \"$name\" must be a list" unless ref $rules->{values} eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 27 | ▶ | if (defined $rules->{pattern}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 40 | ▶ | $schema->{additional} = 1 unless defined $schema->{additional}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | 40 | ▶ | die "error: the schema \"$file\" has problems:\n" . join('', map { " $_\n" } @problems) if @problems; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | last if File::Spec->canonpath($dir) eq $source or $dir eq '.' or $dir eq '/'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
"File::Spec"->canonpath($dir) eq $source or $dir eq "." or $dir eq "/"
"File::Spec"->canonpath($dir) eq $source or $dir eq "."
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 "/"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
defined $frontmatter->{$name} and ref $frontmatter->{$name} || length $frontmatter->{$name}
ref $frontmatter->{$name} or length $frontmatter->{$name}
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}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 14 | ▶ | push @problems, "\"$name\" is not one of the fields in the schema" unless $schema->{additional}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | 14 | next; | |||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 8 | ▶ | next unless defined $frontmatter->{$name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | 8 | ▶ | my @items = ref $value eq 'ARRAY' ? @$value : ref $value ? () : ($value); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | 8 | ▶ | return "\"$name\" must be a list" if ref $value and ref $value ne 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $value and ref $value ne "ARRAY"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 7 | my @problems; | |||||||||||||||||||||||||||||||||||||||||
| 198 | 7 | ▶ | if ($rules->{values}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $item and $allowed{$item}
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}
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||
| 205 | 7 | return @problems; | |||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | 82 | ▶ | if ($type eq 'map') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 4 | ▶ | return ref $value eq 'HASH' ? () : "\"$name\" must be a set of key: value pairs"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | 78 | ▶ | if ($type eq 'boolean') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 18 | ▶ | return () if is_boolean($value); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | 9 | return "\"$name\" must be true or false"; | |||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | 60 | ▶ | return () if $type eq 'any'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 56 | ▶ | return "\"$name\" must be a single value, not a list or a set" if ref $value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | 52 | my @problems; | |||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | 52 | ▶ | if ($type eq 'number') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 10 | ▶ | return "\"$name\" must be a number" unless looks_like_number($value); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||
| 226 | elsif ($type eq 'integer') { | ||||||||||||||||||||||||||||||||||||||||||
| 227 | 14 | ▶ | return "\"$name\" must be a whole number" unless $value =~ /^-?\d+$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | 41 | ▶ | if ($type eq 'number' or $type eq 'integer') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$type eq "number" or $type eq "integer"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $rules->{"min"} and $value < $rules->{"min"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $rules->{"max"} and $value > $rules->{"max"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | 41 | ▶ | if ($type eq 'string') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $rules->{"min_length"} and $length < $rules->{"min_length"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $rules->{"max_length"} and $length > $rules->{"max_length"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | 19 | ▶ | if (defined $rules->{pattern}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | 4 | my $pattern = $rules->{pattern}; | |||||||||||||||||||||||||||||||||||||||||
| 249 | 4 | ▶ | push @problems, "\"$name\" does not match the pattern $pattern" unless $value =~ /$pattern/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | 41 | ▶ | if ($rules->{values}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
builtin::blessed($value) and $value->isa("JSON::PP::Boolean") || $value->isa("boolean")
$value->isa("JSON::PP::Boolean") or $value->isa("boolean")
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | 25 | ▶ | return 0 if ref $value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 274 | 21 | ▶ | return $value =~ /^(?:1|0|true|false|)$/i ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | 191 | 1; | |||||||||||||||||||||||||||||||||||||||||