From 0333e8447bb3f5c9252a09e7b9aba9095ee087bd Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Mon, 7 Apr 2025 17:40:58 +0200 Subject: [PATCH 1/2] Allow JSON `BigInt` to validate against `float` schema --- Cargo.lock | 1 + Cargo.toml | 1 + src/input/input_json.rs | 4 ++++ tests/validators/test_float.py | 2 ++ 4 files changed, 8 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index e645e120e..bbfc4f5b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -433,6 +433,7 @@ dependencies = [ "idna", "jiter", "num-bigint", + "num-traits", "pyo3", "pyo3-build-config", "regex", diff --git a/Cargo.toml b/Cargo.toml index 4bc99e6ff..91fefc4ef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,6 +44,7 @@ url = "2.5.4" idna = "1.0.3" base64 = "0.22.1" num-bigint = "0.4.6" +num-traits = "0.2.19" uuid = "1.16.0" jiter = { version = "0.9.0", features = ["python"] } hex = "0.4.3" diff --git a/src/input/input_json.rs b/src/input/input_json.rs index 4de3fd4cf..6828f5927 100644 --- a/src/input/input_json.rs +++ b/src/input/input_json.rs @@ -1,6 +1,7 @@ use std::borrow::Cow; use jiter::{JsonArray, JsonObject, JsonValue}; +use num_traits::cast::ToPrimitive; use pyo3::prelude::*; use pyo3::types::{PyDict, PyList, PyString}; use speedate::MicrosecondsPrecisionOverflowBehavior; @@ -173,6 +174,9 @@ impl<'py, 'data> Input<'py> for JsonValue<'data> { match self { JsonValue::Float(f) => Ok(ValidationMatch::exact(EitherFloat::F64(*f))), JsonValue::Int(i) => Ok(ValidationMatch::strict(EitherFloat::F64(*i as f64))), + JsonValue::BigInt(b) => Ok(ValidationMatch::strict(EitherFloat::F64( + b.to_f64().expect("BigInt should always return some value"), + ))), JsonValue::Bool(b) if !strict => Ok(ValidationMatch::lax(EitherFloat::F64(if *b { 1.0 } else { 0.0 }))), JsonValue::Str(str) if !strict => str_as_float(self, str).map(ValidationMatch::lax), _ => Err(ValError::new(ErrorTypeDefaults::FloatType, self)), diff --git a/tests/validators/test_float.py b/tests/validators/test_float.py index 8a0235ed5..b7fdc3b1c 100644 --- a/tests/validators/test_float.py +++ b/tests/validators/test_float.py @@ -11,6 +11,7 @@ from ..conftest import Err, PyAndJson, plain_repr +i64_max = 9_223_372_036_854_775_807 f64_max = 1.7976931348623157e308 @@ -20,6 +21,7 @@ (0, 0), (1, 1), (42, 42), + (i64_max + 1, i64_max + 1), ('42', 42), (' 42.1 ', 42.1), ('42.123', 42.123), From a28a0849edba8d349aa6ec0eff92bed5730b2ef5 Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Wed, 9 Apr 2025 12:06:49 +0200 Subject: [PATCH 2/2] Feedback --- tests/validators/test_float.py | 6 ++++-- tests/validators/test_int.py | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/validators/test_float.py b/tests/validators/test_float.py index b7fdc3b1c..dd78e8499 100644 --- a/tests/validators/test_float.py +++ b/tests/validators/test_float.py @@ -1,5 +1,6 @@ import math import re +import sys from decimal import Decimal from typing import Any @@ -11,8 +12,8 @@ from ..conftest import Err, PyAndJson, plain_repr -i64_max = 9_223_372_036_854_775_807 -f64_max = 1.7976931348623157e308 +i64_max = (2**63) - 1 +f64_max = sys.float_info.max @pytest.mark.parametrize( @@ -22,6 +23,7 @@ (1, 1), (42, 42), (i64_max + 1, i64_max + 1), + (f64_max, f64_max), ('42', 42), (' 42.1 ', 42.1), ('42.123', 42.123), diff --git a/tests/validators/test_int.py b/tests/validators/test_int.py index 4db426032..1de2e0f0a 100644 --- a/tests/validators/test_int.py +++ b/tests/validators/test_int.py @@ -11,7 +11,7 @@ from ..conftest import Err, PyAndJson, plain_repr -i64_max = 9_223_372_036_854_775_807 +i64_max = (2**63) - 1 @pytest.mark.parametrize(