Skip to content

Commit 9a25aa6

Browse files
authored
Allow JSON BigInt to validate against float schema (#1685)
1 parent 0a5bbfc commit 9a25aa6

File tree

5 files changed

+12
-2
lines changed

5 files changed

+12
-2
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ url = "2.5.4"
4444
idna = "1.0.3"
4545
base64 = "0.22.1"
4646
num-bigint = "0.4.6"
47+
num-traits = "0.2.19"
4748
uuid = "1.16.0"
4849
jiter = { version = "0.9.0", features = ["python"] }
4950
hex = "0.4.3"

src/input/input_json.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::borrow::Cow;
22

33
use jiter::{JsonArray, JsonObject, JsonValue};
4+
use num_traits::cast::ToPrimitive;
45
use pyo3::prelude::*;
56
use pyo3::types::{PyDict, PyList, PyString};
67
use speedate::MicrosecondsPrecisionOverflowBehavior;
@@ -173,6 +174,9 @@ impl<'py, 'data> Input<'py> for JsonValue<'data> {
173174
match self {
174175
JsonValue::Float(f) => Ok(ValidationMatch::exact(EitherFloat::F64(*f))),
175176
JsonValue::Int(i) => Ok(ValidationMatch::strict(EitherFloat::F64(*i as f64))),
177+
JsonValue::BigInt(b) => Ok(ValidationMatch::strict(EitherFloat::F64(
178+
b.to_f64().expect("BigInt should always return some value"),
179+
))),
176180
JsonValue::Bool(b) if !strict => Ok(ValidationMatch::lax(EitherFloat::F64(if *b { 1.0 } else { 0.0 }))),
177181
JsonValue::Str(str) if !strict => str_as_float(self, str).map(ValidationMatch::lax),
178182
_ => Err(ValError::new(ErrorTypeDefaults::FloatType, self)),

tests/validators/test_float.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import math
22
import re
3+
import sys
34
from decimal import Decimal
45
from typing import Any
56

@@ -11,7 +12,8 @@
1112

1213
from ..conftest import Err, PyAndJson, plain_repr
1314

14-
f64_max = 1.7976931348623157e308
15+
i64_max = (2**63) - 1
16+
f64_max = sys.float_info.max
1517

1618

1719
@pytest.mark.parametrize(
@@ -20,6 +22,8 @@
2022
(0, 0),
2123
(1, 1),
2224
(42, 42),
25+
(i64_max + 1, i64_max + 1),
26+
(f64_max, f64_max),
2327
('42', 42),
2428
(' 42.1 ', 42.1),
2529
('42.123', 42.123),

tests/validators/test_int.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from ..conftest import Err, PyAndJson, plain_repr
1313

14-
i64_max = 9_223_372_036_854_775_807
14+
i64_max = (2**63) - 1
1515

1616

1717
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)