Skip to content

Commit 97cdbcf

Browse files
authored
Fix strip whitespace ordering (#264)
* Switched order of strip_whitespace to before length checks * Changed logic of string length test * Changed pattern test to match whitespace stripping
1 parent bb13fda commit 97cdbcf

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

src/validators/string.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ impl Validator for StrConstrainedValidator {
9292
let either_str = input.validate_str(extra.strict.unwrap_or(self.strict))?;
9393
let cow = either_str.as_cow()?;
9494
let mut str = cow.as_ref();
95+
if self.strip_whitespace {
96+
str = str.trim();
97+
}
9598
if let Some(min_length) = self.min_length {
9699
if str.len() < min_length {
97100
// return py_error!("{} is shorter than {}", str, min_length);
@@ -114,10 +117,6 @@ impl Validator for StrConstrainedValidator {
114117
}
115118
}
116119

117-
if self.strip_whitespace {
118-
str = str.trim();
119-
}
120-
121120
let py_string = if self.to_lower {
122121
PyString::new(py, &str.to_lowercase())
123122
} else if self.to_upper {

tests/validators/test_string.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ def test_str_not_json(input_value, expected):
7979
({'pattern': r'\d+$'}, 'foobar 123', 'foobar 123'),
8080
({'pattern': r'^\d+$'}, '12345a', Err("String should match pattern '^\\d+$' [kind=str_pattern_mismatch")),
8181
# strip comes after length check
82-
({'max_length': 5, 'strip_whitespace': True}, '1234 ', Err('String should have at most 5 characters')),
82+
({'max_length': 5, 'strip_whitespace': True}, '1234 ', '1234'),
8383
# to_upper and strip comes after pattern check
8484
({'to_upper': True, 'pattern': 'abc'}, 'abc', 'ABC'),
85-
({'strip_whitespace': True, 'pattern': r'\d+$'}, 'foobar 123 ', Err("String should match pattern '\\d+$'")),
85+
({'strip_whitespace': True, 'pattern': r'\d+$'}, 'foobar 123 ', 'foobar 123'),
8686
({'min_length': 1}, '🐈 Hello', '🐈 Hello'),
8787
],
8888
)

0 commit comments

Comments
 (0)