Skip to content

Commit 0333e84

Browse files
committed
Allow JSON BigInt to validate against float schema
1 parent 0a5bbfc commit 0333e84

File tree

4 files changed

+8
-0
lines changed

4 files changed

+8
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

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

14+
i64_max = 9_223_372_036_854_775_807
1415
f64_max = 1.7976931348623157e308
1516

1617

@@ -20,6 +21,7 @@
2021
(0, 0),
2122
(1, 1),
2223
(42, 42),
24+
(i64_max + 1, i64_max + 1),
2325
('42', 42),
2426
(' 42.1 ', 42.1),
2527
('42.123', 42.123),

0 commit comments

Comments
 (0)