Commits

Toby Inkster committed 2d76acd

fix eunm bug RT#81867

  • Participants
  • Parent commits 278e8d4

Comments (0)

Files changed (4)

lib/JSON/Schema.pm

  else
  {
    print "Errors\n";
-	print " - $_\n" foreach $result->errors;
+   print " - $_\n" foreach $result->errors;
  }
 
 =head1 DESCRIPTION
   my $validator = JSON::Schema->new({
     properties => { 
       'time' => { type => ['DateTime','string'] },
-	 },
+    },
   });
   my $object = {
     'time' => DateTime->now;

lib/JSON/Schema/Examples.pod

    nickname  => 'TOBYINK',
    email     => { value => 'tobyink@cpan.org' },
    adr       => { countryName => 'England', region => 'East Sussex' },
-   });
+ });
  
  # Now we create our JSON Schema validator:
  my $card_schema = get('http://json-schema.org/card'); 
      locale     => { type=>['DateTime::Locale','string'] },
      time_zone  => { type=>['DateTime::TimeZone', 'string'] },
      formatter  => { type=>'any' },
-     }
-   };
+   }
+ };
  my $validator = JSON::Schema->new($datetime_new_schema);
  
  my %params = get_user_input();

lib/JSON/Schema/Helper.pm

 				if ($schema->{'uniqueItems'})
 				{
 					my %hash;
-					$hash{ to_json([$_],{canonical=>1,convert_blessed=>1}) }++ foreach @$value;
+					$hash{ to_json([$_],{canonical=>1,convert_blessed=>1}) }++ for @$value;
 					$addError->("Array must not contain duplicates.")
 						unless scalar(keys %hash) == scalar(@$value);
 				}
 			}
 			if ($schema->{'enum'})
 			{
+				my %enum;
+				$enum{ to_json([$_],{canonical=>1,convert_blessed=>1}) }++ for @{ $schema->{'enum'} };
+				my $this_value = to_json([$value],{canonical=>1,convert_blessed=>1});
 				$addError->("does not have a value in the enumeration {" . (join ",", @{ $schema->{'enum'} }) . '}')
-					unless grep { $value eq $_ } @{ $schema->{'enum'} };
+					unless exists $enum{$this_value};
 			}
 			if ($schema->{'divisibleBy'} and $self->jsMatchType('number', $value))
 			{
+=head1 PURPOSE
+
+Test that enumerations work; in particular enumerations of non-references.
+
+=head1 SEE ALSO
+
+L<https://rt.cpan.org/Ticket/Display.html?id=81867>.
+
+=head1 AUTHOR
+
+Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
+
+=head1 COPYRIGHT AND LICENCE
+
+Copyright 2012 Toby Inkster.
+
+This file is tri-licensed. It is available under the X11 (a.k.a. MIT)
+licence; you can also redistribute it and/or modify it under the same
+terms as Perl itself.
+
+=cut
+
+use strict;
+use warnings;
+use Test::More;
+
+use JSON::Schema;
+
+my $male = JSON::Schema->new(
+	{
+		type => 'object',
+		properties => {
+			chromosomes => {
+				enum => [
+					[qw( X Y )],
+					[qw( Y X )],
+				],
+			}
+		},
+	},
+);
+
+my $female = JSON::Schema->new(
+	{
+		type => 'object',
+		properties => {
+			chromosomes => {
+				enum => [
+					[qw( X X )],
+				],
+			}
+		},
+	},
+);
+
+ok(
+	!$male->validate({ name => "Kate", chromosomes => [qw( X X )] }),
+	"it's short for Bob",
+);
+
+ok(
+	$female->validate({ name => "Kate", chromosomes => [qw( X X )] }),
+);
+
+ok(
+	$male->validate({ name => "Dave", chromosomes => [qw( X Y )] }),
+);
+
+ok(
+	$male->validate({ name => "Arnie", chromosomes => [qw( Y X )] }),
+);
+
+ok(
+	!$male->validate({ name => "Eddie", chromosomes => [qw( X Y Y )] }),
+);
+
+ok(
+	!$male->validate({ name => "Steve", chromosomes => 'XY' }),
+);
+
+done_testing;