Subject: | Nested array in enum is compared by reference |
We encounter an issue while trying to check for fixed array values with
JSON::Schema.
Consider following (simplified) schema:
{
"type": "array",
"required": true,
"enum": [
["some_value_a", "some_value_b"]
]
}
Here is quick and dirty self-contained code to examine such schema:
#/usr/bin/perl
use strict; use warnings;
use JSON::Schema;
my $v = JSON::Schema->new({
type => "array", required => 1,
enum => [ [qw/some_value_a some_value_b/] ]
});
print "$_\n" foreach $v->validate([qw/some_value_a some_value_b/])->errors;
It returns:
$: does not have a value in the enumeration {ARRAY(0xab3d48)}
I believe that this should be validated with no warnings, as looking
into RFC:
http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.19:
"Comparison of enum values uses *the same* algorithm as defined in
"uniqueItems" (Section 5.15)."
http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.15
"or are arrays, contains the same number of items, and each item in the
array is equal to the corresponding item in the other array;"
Looking into implementation:
if ($schema->{'enum'})
{
$addError->("does not have a value in the enumeration {" . (join
",", @{ $schema->{'enum'} }) . '}')
unless grep { $value eq $_ } @{ $schema->{'enum'} };
}
It's clear that both arrays ($value and $_) are compared by reference
(e.g. ARRAY(0x1aeed48) and ARRAY(0x1b0ba70)).
perl -v:
This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi
Copyright 1987-2009, Larry Wall
Perl may be copied only under the terms of either the Artistic License
or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
uname -a:
Linux gs5138 2.6.32-279.14.1.el6.x86_64 #1 SMP Tue Nov 6 23:43:09 UTC
2012 x86_64 x86_64 x86_64 GNU/Linux