Skip to content

Commit

Permalink
Fix handling of true/false in schema, when loaded with YAML::Syck closes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Henning Thorsen authored and elcamlost committed Oct 2, 2018
1 parent 65b7942 commit 6fc2671
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,5 +1,8 @@
Revision history for perl distribution JSON-Validator

0.83 Not Released
- Fix handling of true/false in schema, when loaded with YAML::Syck #27

0.82 2016-08-09T12:45:05+0200
- Fix finding all $ref occurances jhthorsen/swagger2#95

Expand Down
1 change: 1 addition & 0 deletions lib/JSON/Validator.pm
Expand Up @@ -158,6 +158,7 @@ sub _load_schema_from_data {
sub _load_schema_from_text {
return Mojo::JSON::decode_json($_[1]) if $_[1] =~ /^\s*\{/s;
$_[0]->{coerce}{booleans} = 1; # internal coercion
local $YAML::Syck::ImplicitTyping = 1;
_load_yaml($_[1]) || undef;
}

Expand Down
6 changes: 3 additions & 3 deletions t/booleans.t
Expand Up @@ -2,7 +2,7 @@ use Mojo::Base -strict;
use Test::More;
use JSON::Validator;

my $validator = JSON::Validator->new->schema({properties => {required => {type => "boolean"}}});
my $validator = JSON::Validator->new->schema({properties => {required => {type => 'boolean'}}});

my @errors = $validator->validate({required => '0'});
is $errors[0]->{message}, 'Expected boolean - got string.', 'string 0 is not detected as boolean';
Expand All @@ -13,12 +13,12 @@ for my $value (!!1, !!0) {
ok !@errors, "boolean ($value). (@errors)";
}

for my $value (1, "1", "0", "") {
for my $value (1, '1', '0', '') {
my @errors = $validator->validate({required => $value});
ok @errors, "not boolean ($value). @errors";
}

for my $value ("true", "false") {
for my $value ('true', 'false') {
my @errors = $validator->validate({required => $value});
ok !@errors, "boolean ($value). (@errors)";
}
Expand Down
27 changes: 27 additions & 0 deletions t/issue-27-yaml-syck-false.t
@@ -0,0 +1,27 @@
use Mojo::Base -strict;
use Test::More;
use JSON::Validator;

plan skip_all => $@ unless eval 'require YAML::Syck;1';

use JSON::Validator;
Mojo::Util::monkey_patch('JSON::Validator' => _load_yaml => \&YAML::Syck::Load);

my $validator = JSON::Validator->new->schema('data://main/yaml-syck.yml');
my @errors = $validator->validate({firstName => 'Jan Henning', lastName => 'Thorsen', age => 42});

ok $INC{'YAML/Syck.pm'}, 'YAML::Syck is loaded';
ok !$INC{'YAML/XS.pm'}, 'YAML::XS is not loaded';
is "@errors", "/: Properties not allowed: age.", "additionalProperties: false";

done_testing;

__DATA__
@@ yaml-syck.yml
---
type: object
required: [firstName, lastName]
additionalProperties: false
properties:
firstName: { type: string }
lastName: { type: string }

0 comments on commit 6fc2671

Please sign in to comment.