| 1 | =head1 NAME | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | Config::Neat::Schema - Validate Config::Neat files against schema | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | File 01.nconf: | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | foo { | ||||||||||||||||||||||||||||
| 10 | bar baz etc | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | etc { | ||||||||||||||||||||||||||||
| 13 | pwd 1 2 | ||||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | abc def | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | File schema.nconf: | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | foo | ||||||||||||||||||||||||||||
| 21 | { | ||||||||||||||||||||||||||||
| 22 | bar ARRAY | ||||||||||||||||||||||||||||
| 23 | etc | ||||||||||||||||||||||||||||
| 24 | { | ||||||||||||||||||||||||||||
| 25 | * ARRAY | ||||||||||||||||||||||||||||
| 26 | pwd STRING | ||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | data DATA | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | if file 01.nconf is validated against schema.nconf, it will: | ||||||||||||||||||||||||||||
| 32 | 1) convert arrays to strings for the known nodes with 'STRING' type | ||||||||||||||||||||||||||||
| 33 | 2) die or warn (depending on the settings) when an unknown node is found | ||||||||||||||||||||||||||||
| 34 | (in the example above, 'abc'). | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | '*' as the name of the node means 'node with any name'. If such catch-all rule | ||||||||||||||||||||||||||||
| 37 | is not specified, all possible node values need to be specified explicitly. | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | Possible type specifiers are: HASH (this is default if not specified), | ||||||||||||||||||||||||||||
| 40 | ARRAY, STRING, ARRAY_OR_HASH, STRING_OR_HASH, or DATA. 'DATA' nodes may contain | ||||||||||||||||||||||||||||
| 41 | any arbitrary data structure and are not validated. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | Copyright (C) 2012-2015 Igor Afanasyev <igor.afanasyev@gmail.com> | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | L<https://github.com/iafan/Config-Neat> | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =cut | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | package Config::Neat::Schema; | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 1 | our $VERSION = '1.4'; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1 | ▶ | use Config::Neat::Array; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | 1 | ▶ | use Config::Neat::Inheritable; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | 1 | ▶ | use Config::Neat::Util qw(new_ixhash is_hash is_any_hash is_any_array is_simple_array is_neat_array hash_has_sequential_keys); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | 1 | ▶ | use File::Spec::Functions qw(rel2abs); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | 1 | ▶ | use File::Basename qw(dirname); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | 1 | ▶ | use Tie::IxHash; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # | ||||||||||||||||||||||||||||
| 67 | # Initialize object | ||||||||||||||||||||||||||||
| 68 | # | ||||||||||||||||||||||||||||
| 69 | sub new { | ||||||||||||||||||||||||||||
| 70 | 1 | ▶ | my ($class, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 1 | my $self = { | |||||||||||||||||||||||||||
| 73 | schema => $data | ||||||||||||||||||||||||||||
| 74 | }; | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 1 | bless $self, $class; | |||||||||||||||||||||||||||
| 77 | 1 | return $self; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # Given file name, will read and store the schema file | ||||||||||||||||||||||||||||
| 81 | sub load { | ||||||||||||||||||||||||||||
| 82 | 1 | ▶ | my ($self, $filename, $binmode) = @_; | ||||||||||||||||||||||||||
|
Subroutine
load: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 83 | 1 | my $c = Config::Neat::Inheritable->new(); | |||||||||||||||||||||||||||
| 84 | 1 | return $self->{schema} = $c->parse_file($filename, $binmode); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # Store loaded data as current schema | ||||||||||||||||||||||||||||
| 88 | sub set { | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | my ($self, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
set: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 90 | 0 | $self->{schema} = $data; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # Validates provided data structure (parsed config file) against the previously loaded schema | ||||||||||||||||||||||||||||
| 94 | # with expanded '@inherit' blocks | ||||||||||||||||||||||||||||
| 95 | sub validate { | ||||||||||||||||||||||||||||
| 96 | 20 | ▶ | my ($self, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 97 | 20 | ▶ | die "Schema should be loaded prior to validation" unless defined $self->{schema}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 20 | return $self->validate_node($self->{schema}, $data, undef, undef, []); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | sub validate_node { | ||||||||||||||||||||||||||||
| 102 | 120 | ▶ | my ($self, $schema_node, $data_node, $parent_data, $parent_data_key, $path) = @_; | ||||||||||||||||||||||||||
|
Subroutine
validate_node: called CC=33 SCAR=35.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 120 | my $pathstr = '/'.join('/', @$path); | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 120 | ▶ | if (!$schema_node) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 1 | die "Node '$pathstr' is not defined in the schema"; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 119 | my $schema_type = $self->get_node_type($schema_node); | |||||||||||||||||||||||||||
| 111 | 119 | my $data_type = $self->get_node_type($data_node); | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | #print "::[$pathstr] schema_type=[$schema_type], data_type=[$data_type]\n"; | ||||||||||||||||||||||||||||
| 114 | #use Data::Dumper; print Dumper($data_node); | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 119 | ▶ | if ($schema_type eq 'STRING') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 117 | # the node itself is already a scalar and contains the type definition | ||||||||||||||||||||||||||||
| 118 | 0 | $schema_type = $schema_node; | |||||||||||||||||||||||||||
| 119 | } elsif ($schema_type eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 120 | # the string representation of the node contains the type definition | ||||||||||||||||||||||||||||
| 121 | 46 | $schema_type = $schema_node->as_string; | |||||||||||||||||||||||||||
| 122 | ▶ | } elsif ($schema_type eq 'HASH' and defined $schema_node->{''}) { | |||||||||||||||||||||||||||
|
Condition66%2/3
$schema_type eq "HASH" and defined $schema_node->{""}
MC/DC50%1/2
$schema_type eq "HASH" and defined $schema_node->{""}
$schema_type eq "HASH"
defined $schema_node->{""}
Truth table66%2/3
A and B
A= $schema_type eq "HASH"B= defined $schema_node->{""}
| |||||||||||||||||||||||||||||
| 123 | # if it's a hash, the the string representation of the node's default parameter | ||||||||||||||||||||||||||||
| 124 | # may contain the type definition override | ||||||||||||||||||||||||||||
| 125 | 38 | my $val = $schema_node->{''}; | |||||||||||||||||||||||||||
| 126 | 38 | ▶ | $schema_type = $schema_node->{''}->as_string if is_neat_array($val); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 38 | ▶ | $schema_type = $schema_node->{''} if ref(\$val) eq 'SCALAR'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # disambiguate fuzzy node schema types | ||||||||||||||||||||||||||||
| 131 | 119 | ▶ | if ($schema_type eq 'ARRAY_OR_HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 17 | ▶ | $schema_type = ($data_type eq 'HASH') ? 'HASH' : 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 119 | ▶ | if ($schema_type eq 'STRING_OR_HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 2 | ▶ | $schema_type = ($data_type eq 'HASH') ? 'HASH' : 'STRING'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # automatic casting from ARRAY to STRING | ||||||||||||||||||||||||||||
| 140 | 119 | ▶ | if ($schema_type eq 'STRING' and $data_type eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$schema_type eq "STRING" and $data_type eq "ARRAY"
MC/DC50%1/2
$schema_type eq "STRING" and $data_type eq "ARRAY"
$schema_type eq "STRING"
$data_type eq "ARRAY"
Truth table66%2/3
A and B
A= $schema_type eq "STRING"B= $data_type eq "ARRAY"
| |||||||||||||||||||||||||||||
| 141 | 14 | $parent_data->{$parent_data_key} = $data_node = $data_node->as_string; | |||||||||||||||||||||||||||
| 142 | 14 | $data_type = $schema_type; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # automatic casting from ARRAY to BOOLEAN | ||||||||||||||||||||||||||||
| 146 | 119 | ▶ | if ($schema_type eq 'BOOLEAN' and $data_type eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$schema_type eq "BOOLEAN" and $data_type eq "ARRAY"
MC/DC50%1/2
$schema_type eq "BOOLEAN" and $data_type eq "ARRAY"
$schema_type eq "BOOLEAN"
$data_type eq "ARRAY"
Truth table66%2/3
A and B
A= $schema_type eq "BOOLEAN"B= $data_type eq "ARRAY"
| |||||||||||||||||||||||||||||
| 147 | 11 | ▶ | die "'".$data_node->as_string."' is not a valid boolean number\n" unless $data_node->is_boolean; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 10 | $parent_data->{$parent_data_key} = $data_node = $data_node->as_boolean; | |||||||||||||||||||||||||||
| 149 | 10 | $data_type = $schema_type; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | # skip (don't validate) DATA nodes | ||||||||||||||||||||||||||||
| 153 | 118 | ▶ | return 1 if ($schema_type eq 'DATA'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 108 | ▶ | if ($schema_type eq 'LIST') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | # if this is not a simple array of scalars, wrap as an array | ||||||||||||||||||||||||||||
| 157 | 8 | ▶ | if (is_simple_array($data_node) or !is_any_array($data_node)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
is_simple_array($data_node) or not is_any_array($data_node)
MC/DC100%2/2
is_simple_array($data_node) or not is_any_array($data_node)
is_simple_array($data_node)
not is_any_array($data_node)
Truth table100%3/3
A or not(B)
A= is_simple_array($data_node)B= not is_any_array($data_node)
| |||||||||||||||||||||||||||||
| 158 | 2 | $data_node = [$data_node]; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | # then, convert an array to an ixhash with sequential keys | ||||||||||||||||||||||||||||
| 161 | 8 | my $h = new_ixhash; | |||||||||||||||||||||||||||
| 162 | 8 | my $i = 0; | |||||||||||||||||||||||||||
| 163 | 8 | map { $h->{$i++} = $_ } @$data_node; | |||||||||||||||||||||||||||
| 164 | 8 | $parent_data->{$parent_data_key} = $data_node = $h; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 8 | $data_type = 'HASH'; | |||||||||||||||||||||||||||
| 167 | 8 | $schema_type = 'ARRAY'; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | # see if automatic casting from HASH to ARRAY is possible | ||||||||||||||||||||||||||||
| 171 | 108 | my $cast_to_array; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 108 | ▶ | if ($schema_type eq 'ARRAY' and $data_type eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$schema_type eq "ARRAY" and $data_type eq "HASH"
MC/DC100%2/2
$schema_type eq "ARRAY" and $data_type eq "HASH"
$schema_type eq "ARRAY"
$data_type eq "HASH"
Truth table100%3/3
A and B
A= $schema_type eq "ARRAY"B= $data_type eq "HASH"
| |||||||||||||||||||||||||||||
| 174 | 15 | ▶ | die "Can't cast '$pathstr' to ARRAY, since it is a HASH containing non-sequential keys" unless hash_has_sequential_keys($data_node); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 14 | $cast_to_array = 1; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 107 | ▶ | if ($schema_type ne $data_type && !$cast_to_array) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$schema_type ne $data_type and not $cast_to_array
MC/DC0%0/2
$schema_type ne $data_type and not $cast_to_array
$schema_type ne $data_type
not $cast_to_array
Truth table66%2/3
A and not(B)
A= $schema_type ne $data_typeB= not $cast_to_array
| |||||||||||||||||||||||||||||
| 179 | 0 | die "'$pathstr' is $data_type, while it is expected to be $schema_type"; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 107 | ▶ | if ($data_type eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | # flatten the array | ||||||||||||||||||||||||||||
| 184 | 24 | $parent_data->{$parent_data_key} = $data_node->as_flat_array; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 107 | ▶ | if ($data_type eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 59 | foreach my $key (keys %$data_node) { | |||||||||||||||||||||||||||
| 189 | 101 | my @a = @$path; | |||||||||||||||||||||||||||
| 190 | 101 | push @a, $key; | |||||||||||||||||||||||||||
| 191 | 101 | ▶ | if ($key eq '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | # TODO: check if the default parameter for the hash is allowed, and if it is a string or array | ||||||||||||||||||||||||||||
| 193 | } else { | ||||||||||||||||||||||||||||
| 194 | 101 | ▶ | die "Can't validate '/", join('/', @a), "', because schema contains no definition for it" if !is_hash($schema_node); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 100 | ▶ | my $schema_subnode = $schema_node->{$key} || $schema_node->{'*'}; | ||||||||||||||||||||||||||
|
Condition100%3/3
$schema_node->{$key} || $schema_node->{'*'}
MC/DC100%2/2
$schema_node->{$key} || $schema_node->{'*'}
$schema_node->{$key}
$schema_node->{'*'}
Truth table100%3/3
A || B
A= $schema_node->{$key}B= $schema_node->{'*'}
| |||||||||||||||||||||||||||||
| 196 | 100 | $self->validate_node($schema_subnode, $data_node->{$key}, $data_node, $key, \@a); | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 99 | ▶ | if ($cast_to_array) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | 13 | my @a = values %$data_node; | |||||||||||||||||||||||||||
| 203 | 13 | $parent_data->{$parent_data_key} = Config::Neat::Array->new(\@a); | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 99 | return 1; | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | sub get_node_type { | ||||||||||||||||||||||||||||
| 210 | 238 | ▶ | my ($self, $node) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_node_type: called CC=4 SCAR=17.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 211 | 238 | ▶ | return 'HASH' if ref($node) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | 106 | ▶ | return 'ARRAY' if is_any_array($node); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | 0 | ▶ | return 'STRING' if ref(\$node) eq 'SCALAR'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | 0 | return 'UNKNOWN'; | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 1 | 1; | |||||||||||||||||||||||||||