Subject: | Number should accept exponential form as per JSON schema specification |
If we do pass floating numbers like 0.000000023, currently it fails
$result = $schema4->validate({ test => 0.000000023 });
ok $result->valid, 'number against decimal number with more than 5 decimal places'
or map { diag "reason: $_" } $result->errors;
Test fails with this reason:
not ok 24 - number against decimal number with more than 5 decimal places
# Failed test 'number against decimal number with more than 5 decimal places'
# at t/05union.t line 181.
# reason: $.test: string value found, but a number is required
as it converts into 23e-07
Refer https://tools.ietf.org/html/rfc7159#section-6
Subject: | numberschema.patch |
diff --git a/lib/JSON/Schema/Helper.pm b/lib/JSON/Schema/Helper.pm
index 67b659e..3cefc6d 100644
--- a/lib/JSON/Schema/Helper.pm
+++ b/lib/JSON/Schema/Helper.pm
@@ -489,7 +489,7 @@ sub jsMatchType
if (lc $type eq 'number')
{
- return ($value =~ /^\-?[0-9]*(\.[0-9]*)?$/ and length $value) ? TRUE : FALSE;
+ return ($value =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/ and length $value) ? TRUE : FALSE;
}
if (lc $type eq 'integer')
diff --git a/t/05union.t b/t/05union.t
index 4801e41..ec862c6 100644
--- a/t/05union.t
+++ b/t/05union.t
@@ -166,4 +166,33 @@ $result = $schema3->validate({ test => undef });
ok $result->valid, 'all types against null'
or map { diag "reason: $_" } $result->errors;
+my $schema4 = JSON::Schema->new(
+ { type => 'object'
+ , additionalProperties => 0
+ , properties =>
+ { test =>
+ { type => [ qw/number/ ], required => 1 }
+ }
+ } );
+
+$result = $schema4->validate({ test => 23 });
+ok $result->valid, 'number against integer'
+ or map { diag "reason: $_" } $result->errors;
+
+$result = $schema4->validate({ test => 0.00023 });
+ok $result->valid, 'number against decimal number'
+ or map { diag "reason: $_" } $result->errors;
+
+$result = $schema4->validate({ test => 0.000000023 });
+ok $result->valid, 'number against decimal number with more than 5 decimal places'
+ or map { diag "reason: $_" } $result->errors;
+
+$result = $schema4->validate({ test => -0.000000023 });
+ok $result->valid, 'number against negative number with more than 5 decimal places'
+ or map { diag "reason: $_" } $result->errors;
+
+$result = $schema4->validate({ test => 23e-05 });
+ok $result->valid, 'number against exponential number'
+ or map { diag "reason: $_" } $result->errors;
+
done_testing;