Skip to content

Commit c6301fe

Browse files
Fix parsing BigInt from str (#1204)
1 parent f669db9 commit c6301fe

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

src/input/input_json.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,7 @@ impl<'a> Input<'a> for String {
370370
}
371371

372372
fn validate_int(&'a self, _strict: bool) -> ValResult<ValidationMatch<EitherInt<'a>>> {
373-
match self.parse() {
374-
Ok(i) => Ok(ValidationMatch::lax(EitherInt::I64(i))),
375-
Err(_) => Err(ValError::new(ErrorTypeDefaults::IntParsing, self)),
376-
}
373+
str_as_int(self, self).map(ValidationMatch::lax)
377374
}
378375

379376
fn validate_float(&'a self, _strict: bool) -> ValResult<ValidationMatch<EitherFloat<'a>>> {

src/input/input_string.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::validators::decimal::create_decimal;
1111
use super::datetime::{
1212
bytes_as_date, bytes_as_datetime, bytes_as_time, bytes_as_timedelta, EitherDate, EitherDateTime, EitherTime,
1313
};
14-
use super::shared::{str_as_bool, str_as_float};
14+
use super::shared::{str_as_bool, str_as_float, str_as_int};
1515
use super::{
1616
BorrowInput, EitherBytes, EitherFloat, EitherInt, EitherString, EitherTimedelta, GenericArguments, GenericIterable,
1717
GenericIterator, GenericMapping, Input, ValidationMatch,
@@ -112,10 +112,7 @@ impl<'a> Input<'a> for StringMapping<'a> {
112112

113113
fn validate_int(&'a self, _strict: bool) -> ValResult<ValidationMatch<EitherInt<'a>>> {
114114
match self {
115-
Self::String(s) => match py_string_str(s)?.parse() {
116-
Ok(i) => Ok(ValidationMatch::strict(EitherInt::I64(i))),
117-
Err(_) => Err(ValError::new(ErrorTypeDefaults::IntParsing, self)),
118-
},
115+
Self::String(s) => str_as_int(self, py_string_str(s)?).map(ValidationMatch::strict),
119116
Self::Mapping(_) => Err(ValError::new(ErrorTypeDefaults::IntType, self)),
120117
}
121118
}

tests/validators/test_int.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,3 +504,11 @@ def test_allow_inf_nan_false_json() -> None:
504504
v.validate_json('Infinity')
505505
with pytest.raises(ValidationError, match=r'Input should be a finite number \[type=finite_number'):
506506
v.validate_json('-Infinity')
507+
508+
509+
def test_json_big_int_key():
510+
v = SchemaValidator({'type': 'dict', 'keys_schema': {'type': 'int'}, 'values_schema': {'type': 'str'}})
511+
big_integer = 1433352099889938534014333520998899385340
512+
assert v.validate_python({big_integer: 'x'}) == {big_integer: 'x'}
513+
assert v.validate_json('{"' + str(big_integer) + '": "x"}') == {big_integer: 'x'}
514+
assert v.validate_strings({str(big_integer): 'x'}) == {big_integer: 'x'}

0 commit comments

Comments
 (0)