| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | $schema = {} unless defined $schema; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | die "error: the schema \"$file\" must contain key: value pairs\n" unless ref $schema eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | unless $key =~ /^(?:required|fields|additional)$/; | ||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | my $required = $schema->{required}; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | $required = [] unless defined $required; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | $required = [$required] unless ref $required; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | push @problems, "\"required\" must be a list of field names" unless ref $required eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | $schema->{required} = $required; | |||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | my $fields = $schema->{fields}; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | $fields = {} unless defined $fields; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | push @problems, "\"fields\" must be a set of field names" unless ref $fields eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | $schema->{fields} = ref $fields eq 'HASH' ? $fields : {}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $rules and not ref $rules
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
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | $rules = {} unless defined $rules; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if (ref $rules ne 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | push @problems, "\"$name\" has the unknown type \"$rules->{type}\" (use " . join(', ', sort keys %TYPES) . ")" | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | unless $TYPES{$rules->{type}}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if (defined $rules->{values}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | $rules->{values} = [$rules->{values}] unless ref $rules->{values}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | push @problems, "\"values\" for \"$name\" must be a list" unless ref $rules->{values} eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | if (defined $rules->{pattern}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | $schema->{additional} = 1 unless defined $schema->{additional}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | die "error: the schema \"$file\" has problems:\n" . join('', map { " $_\n" } @problems) if @problems; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/6
defined $frontmatter->{$name} and ref $frontmatter->{$name} || length $frontmatter->{$name}
ref $frontmatter->{$name} or length $frontmatter->{$name}
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}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | ▶ | push @problems, "\"$name\" is not one of the fields in the schema" unless $schema->{additional}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | next unless defined $frontmatter->{$name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | ▶ | my @items = ref $value eq 'ARRAY' ? @$value : ref $value ? () : ($value); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | ▶ | return "\"$name\" must be a list" if ref $value and ref $value ne 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $value and ref $value ne "ARRAY"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | my @problems; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | ▶ | if ($rules->{values}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $item and $allowed{$item}
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}
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | return @problems; | |||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | ▶ | if ($type eq 'map') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | ▶ | return ref $value eq 'HASH' ? () : "\"$name\" must be a set of key: value pairs"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | if ($type eq 'boolean') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | ▶ | return () if is_boolean($value); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | return "\"$name\" must be true or false"; | |||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | ▶ | return () if $type eq 'any'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | ▶ | return "\"$name\" must be a single value, not a list or a set" if ref $value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | my @problems; | |||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | ▶ | if ($type eq 'number') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | ▶ | return "\"$name\" must be a number" unless looks_like_number($value); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||
| 202 | elsif ($type eq 'integer') { | ||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | ▶ | return "\"$name\" must be a whole number" unless $value =~ /^-?\d+$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | ▶ | if ($type eq 'number' or $type eq 'integer') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$type eq "number" or $type eq "integer"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $rules->{"min"} and $value < $rules->{"min"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $rules->{"max"} and $value > $rules->{"max"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | ▶ | if ($type eq 'string') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $rules->{"min_length"} and $length < $rules->{"min_length"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $rules->{"max_length"} and $length > $rules->{"max_length"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | ▶ | if (defined $rules->{pattern}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | my $pattern = $rules->{pattern}; | |||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | ▶ | push @problems, "\"$name\" does not match the pattern $pattern" unless $value =~ /$pattern/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | ▶ | if ($rules->{values}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/6
builtin::blessed($value) and $value->isa("JSON::PP::Boolean") || $value->isa("boolean")
$value->isa("JSON::PP::Boolean") or $value->isa("boolean")
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | ▶ | return 0 if ref $value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | ▶ | return $value =~ /^(?:1|0|true|false|)$/i ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | 32 | 1; | |||||||||||||||||||||||||||||||||||||||||