Skip to content

Commit 272577f

Browse files
abstract out this logic into a utility sub
1 parent bdd6821 commit 272577f

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

lib/JSON/Schema/Tiny.pm

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -465,11 +465,10 @@ sub _eval_keyword_multipleOf ($data, $schema, $state) {
465465
and do { $data = 0+$data; 1 });
466466

467467
# if either value is a float, use the bignum library for the calculation
468-
if (ref($data) =~ /^Math::Big(?:Int|Float)$/
469-
or ref($schema->{multipleOf}) =~ /^Math::Big(?:Int|Float)$/
468+
if (is_bignum($data) or is_bignum($schema->{multipleOf})
470469
or get_type($data) eq 'number' or get_type($schema->{multipleOf}) eq 'number') {
471-
$data = ref($data) =~ /^Math::Big(?:Int|Float)$/ ? $data->copy : Math::BigFloat->new($data);
472-
my $divisor = ref($schema->{multipleOf}) =~ /^Math::Big(?:Int|Float)$/ ? $schema->{multipleOf} : Math::BigFloat->new($schema->{multipleOf});
470+
$data = is_bignum($data) ? $data->copy : Math::BigFloat->new($data);
471+
my $divisor = is_bignum($schema->{multipleOf}) ? $schema->{multipleOf} : Math::BigFloat->new($schema->{multipleOf});
473472
my ($quotient, $remainder) = $data->bdiv($divisor);
474473
return E($state, 'overflow while calculating quotient') if $quotient->is_inf;
475474
return 1 if $remainder == 0;
@@ -1070,12 +1069,12 @@ sub is_type ($type, $value) {
10701069
}
10711070

10721071
if ($type eq 'number') {
1073-
return ref($value) =~ /^Math::Big(?:Int|Float)$/
1072+
return is_bignum($value)
10741073
|| !($flags & B::SVf_POK) && ($flags & (B::SVf_IOK | B::SVf_NOK));
10751074
}
10761075

10771076
if ($type eq 'integer') {
1078-
return ref($value) =~ /^Math::Big(?:Int|Float)$/ && $value->is_int
1077+
return is_bignum($value) && $value->is_int
10791078
|| !($flags & B::SVf_POK) && ($flags & (B::SVf_IOK | B::SVf_NOK)) && int($value) == $value;
10801079
}
10811080
}
@@ -1093,7 +1092,7 @@ sub get_type ($value) {
10931092
return 'array' if is_plain_arrayref($value);
10941093
return 'boolean' if is_bool($value);
10951094

1096-
return ref($value) =~ /^Math::Big(?:Int|Float)$/ ? ($value->is_int ? 'integer' : 'number')
1095+
return is_bignum($value) ? ($value->is_int ? 'integer' : 'number')
10971096
: (blessed($value) ? '' : 'reference to ').ref($value)
10981097
if is_ref($value);
10991098

@@ -1114,6 +1113,10 @@ sub is_bool ($value) {
11141113
or $value->isa('JSON::XS::Boolean'));
11151114
}
11161115

1116+
sub is_bignum ($value) {
1117+
ref($value) =~ /^Math::Big(?:Int|Float)$/;
1118+
}
1119+
11171120
# compares two arbitrary data payloads for equality, as per
11181121
# https://json-schema.org/draft/2020-12/json-schema-core.html#rfc.section.4.2.2
11191122
# if provided with a state hashref, any differences are recorded within
@@ -1286,7 +1289,7 @@ sub assert_array_schemas ($schema, $state) {
12861289

12871290
sub sprintf_num ($value) {
12881291
# use original value as stored in the NV, without losing precision
1289-
ref($value) =~ /^Math::Big(?:Int|Float)$/ ? $value->bstr : sprintf('%s', $value);
1292+
is_bignum($value) ? $value->bstr : sprintf('%s', $value);
12901293
}
12911294

12921295
1;
@@ -1322,7 +1325,7 @@ validator, supporting the most popular keywords.
13221325
13231326
=head1 FUNCTIONS
13241327
1325-
=for Pod::Coverage is_type get_type is_bool is_equal is_elements_unique jsonp canonical_uri E abort
1328+
=for Pod::Coverage is_type get_type is_bool is_bignum is_equal is_elements_unique jsonp canonical_uri E abort
13261329
assert_keyword_type assert_pattern assert_uri assert_non_negative_integer assert_array_schemas
13271330
new assert_uri_reference sprintf_num
13281331

0 commit comments

Comments
 (0)