Skip to content

Commit

Permalink
Fix max length error on conlist with type int (#902)
Browse files Browse the repository at this point in the history
  • Loading branch information
yohanvalencia authored Aug 23, 2023
1 parent b076a8f commit 882b57f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
56 changes: 41 additions & 15 deletions src/input/return_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,31 +105,54 @@ struct MaxLengthCheck<'a, INPUT> {
max_length: Option<usize>,
field_type: &'a str,
input: &'a INPUT,
known_input_length: usize,
}

impl<'a, INPUT: Input<'a>> MaxLengthCheck<'a, INPUT> {
fn new(max_length: Option<usize>, field_type: &'a str, input: &'a INPUT) -> Self {
fn new(max_length: Option<usize>, field_type: &'a str, input: &'a INPUT, known_input_length: usize) -> Self {
Self {
current_length: 0,
max_length,
field_type,
input,
known_input_length,
}
}

fn incr(&mut self) -> ValResult<'a, ()> {
if let Some(max_length) = self.max_length {
self.current_length += 1;
if self.current_length > max_length {
return Err(ValError::new(
ErrorType::TooLong {
field_type: self.field_type.to_string(),
max_length,
actual_length: self.current_length,
context: None,
},
self.input,
));
match self.max_length {
Some(max_length) => {
self.current_length += 1;
if self.current_length > max_length {
let biggest_length = if self.known_input_length > self.current_length {
self.known_input_length
} else {
self.current_length
};
return Err(ValError::new(
ErrorType::TooLong {
field_type: self.field_type.to_string(),
max_length,
actual_length: biggest_length,
context: None,
},
self.input,
));
}
}
None => {
self.current_length += 1;
if self.current_length > self.known_input_length {
return Err(ValError::new(
ErrorType::TooLong {
field_type: self.field_type.to_string(),
max_length: self.known_input_length,
actual_length: self.current_length,
context: None,
},
self.input,
));
}
}
}
Ok(())
Expand Down Expand Up @@ -315,7 +338,7 @@ impl<'a> GenericIterable<'a> {
let capacity = self
.generic_len()
.unwrap_or_else(|| max_length.unwrap_or(DEFAULT_CAPACITY));
let max_length_check = MaxLengthCheck::new(max_length, field_type, input);
let max_length_check = MaxLengthCheck::new(max_length, field_type, input, capacity);

macro_rules! validate {
($iter:expr) => {
Expand Down Expand Up @@ -371,7 +394,10 @@ impl<'a> GenericIterable<'a> {
field_type: &'static str,
max_length: Option<usize>,
) -> ValResult<'a, Vec<PyObject>> {
let max_length_check = MaxLengthCheck::new(max_length, field_type, input);
let capacity = self
.generic_len()
.unwrap_or_else(|| max_length.unwrap_or(DEFAULT_CAPACITY));
let max_length_check = MaxLengthCheck::new(max_length, field_type, input, capacity);

match self {
GenericIterable::List(collection) => {
Expand Down
10 changes: 8 additions & 2 deletions tests/validators/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ def test_list_error(input_value, index):
infinite_generator(),
Err('List should have at most 44 items after validation, not 45 [type=too_long,'),
),
(
{'max_length': 4, 'items_schema': {'type': 'int'}},
[0, 1, 2, 3, 4, 5, 6, 7, 8],
Err('List should have at most 4 items after validation, not 9 [type=too_long,'),
),
({}, infinite_generator(), Err('List should have at most 10 items after validation, not 11 [type=too_long,')),
],
)
def test_list_length_constraints(kwargs: Dict[str, Any], input_value, expected):
Expand Down Expand Up @@ -391,9 +397,9 @@ def f(v: int) -> int:
{
'type': 'too_long',
'loc': (),
'msg': 'List should have at most 10 items after validation, not 11',
'msg': 'List should have at most 10 items after validation, not 15',
'input': data,
'ctx': {'field_type': 'List', 'max_length': 10, 'actual_length': 11},
'ctx': {'field_type': 'List', 'max_length': 10, 'actual_length': 15},
}
)

Expand Down

0 comments on commit 882b57f

Please sign in to comment.