Skip to content

Commit

Permalink
Fix parsing BigInt from str (#1204)
Browse files Browse the repository at this point in the history
  • Loading branch information
sydney-runkle committed Mar 5, 2024
1 parent f669db9 commit c6301fe
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
5 changes: 1 addition & 4 deletions src/input/input_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,7 @@ impl<'a> Input<'a> for String {
}

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

fn validate_float(&'a self, _strict: bool) -> ValResult<ValidationMatch<EitherFloat<'a>>> {
Expand Down
7 changes: 2 additions & 5 deletions src/input/input_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::validators::decimal::create_decimal;
use super::datetime::{
bytes_as_date, bytes_as_datetime, bytes_as_time, bytes_as_timedelta, EitherDate, EitherDateTime, EitherTime,
};
use super::shared::{str_as_bool, str_as_float};
use super::shared::{str_as_bool, str_as_float, str_as_int};
use super::{
BorrowInput, EitherBytes, EitherFloat, EitherInt, EitherString, EitherTimedelta, GenericArguments, GenericIterable,
GenericIterator, GenericMapping, Input, ValidationMatch,
Expand Down Expand Up @@ -112,10 +112,7 @@ impl<'a> Input<'a> for StringMapping<'a> {

fn validate_int(&'a self, _strict: bool) -> ValResult<ValidationMatch<EitherInt<'a>>> {
match self {
Self::String(s) => match py_string_str(s)?.parse() {
Ok(i) => Ok(ValidationMatch::strict(EitherInt::I64(i))),
Err(_) => Err(ValError::new(ErrorTypeDefaults::IntParsing, self)),
},
Self::String(s) => str_as_int(self, py_string_str(s)?).map(ValidationMatch::strict),
Self::Mapping(_) => Err(ValError::new(ErrorTypeDefaults::IntType, self)),
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/validators/test_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,3 +504,11 @@ def test_allow_inf_nan_false_json() -> None:
v.validate_json('Infinity')
with pytest.raises(ValidationError, match=r'Input should be a finite number \[type=finite_number'):
v.validate_json('-Infinity')


def test_json_big_int_key():
v = SchemaValidator({'type': 'dict', 'keys_schema': {'type': 'int'}, 'values_schema': {'type': 'str'}})
big_integer = 1433352099889938534014333520998899385340
assert v.validate_python({big_integer: 'x'}) == {big_integer: 'x'}
assert v.validate_json('{"' + str(big_integer) + '": "x"}') == {big_integer: 'x'}
assert v.validate_strings({str(big_integer): 'x'}) == {big_integer: 'x'}

0 comments on commit c6301fe

Please sign in to comment.