From 6fc2671dc25a69e217d7054477b4a9e410a010c9 Mon Sep 17 00:00:00 2001 From: Jan Henning Thorsen Date: Tue, 16 Aug 2016 18:30:19 +0200 Subject: [PATCH] Fix handling of true/false in schema, when loaded with YAML::Syck closes #27 --- Changes | 3 +++ lib/JSON/Validator.pm | 1 + t/booleans.t | 6 +++--- t/issue-27-yaml-syck-false.t | 27 +++++++++++++++++++++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 t/issue-27-yaml-syck-false.t diff --git a/Changes b/Changes index 3cd35e55..45a81987 100644 --- a/Changes +++ b/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 diff --git a/lib/JSON/Validator.pm b/lib/JSON/Validator.pm index af7c4986..be274da0 100644 --- a/lib/JSON/Validator.pm +++ b/lib/JSON/Validator.pm @@ -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; } diff --git a/t/booleans.t b/t/booleans.t index 2e083714..6ec74262 100644 --- a/t/booleans.t +++ b/t/booleans.t @@ -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'; @@ -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)"; } diff --git a/t/issue-27-yaml-syck-false.t b/t/issue-27-yaml-syck-false.t new file mode 100644 index 00000000..e111f5f3 --- /dev/null +++ b/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 }