Skip to content

Commit

Permalink
Change variable name and add condition for generator without max_length
Browse files Browse the repository at this point in the history
  • Loading branch information
yohanvalencia committed Aug 23, 2023
1 parent e36cba0 commit 5e139b0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
56 changes: 36 additions & 20 deletions src/input/return_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,38 +105,54 @@ struct MaxLengthCheck<'a, INPUT> {
max_length: Option<usize>,
field_type: &'a str,
input: &'a INPUT,
static_length: usize,
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, static_length: usize) -> 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,
static_length,
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 {
let biggest_length = if self.static_length > self.current_length {
self.static_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,
));
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
1 change: 1 addition & 0 deletions tests/validators/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ def test_list_error(input_value, index):
[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

0 comments on commit 5e139b0

Please sign in to comment.