jhthorsen / json-validator

:cop: Validate data against a JSON schema
https://metacpan.org/release/JSON-Validator
57 stars 59 forks source link

Possible issue with minProperties #29

Closed dnmfarrell closed 8 years ago

dnmfarrell commented 8 years ago

Hi,

I'm having trouble getting minProperties to be enforced:

#!/usr/bin/env perl
use strict;
use warnings;
use JSON::Validator;

my $validator = JSON::Validator->new;
$validator->schema({
  foo => {
    type => 'object',
    properties => {
      bar => {
        type => 'integer',
      }
    },
    minProperties => 1,
  }
});

print $validator->validate({ foo => { } });

I'm running version 0.86 on Perl 5.22.0.

jhthorsen commented 8 years ago

Pretty sure your schema is incorrect. Maybe you want something like this:

$validator->schema({
  type       => 'object',
  properties => {
    foo => {
      type          => 'object',
      minProperties => 1,
      properties    => {
        bar => {
          type => 'integer'
        }
      },
    },
  },
});
dnmfarrell commented 8 years ago

Yes you're right. This does raise an error as expected:

#!/usr/bin/env perl
use strict;
use warnings;
use JSON::Validator;

my $validator = JSON::Validator->new;
$validator->schema({
    type => 'object',
    properties => {
      bar => {
        type => 'integer',
      }
    },
    minProperties => 1,
});

print $validator->validate({ });

Thank you!

jhthorsen commented 8 years ago

You're very welcome 😄