Skip to content

Commit 0c85ada

Browse files
committed
Add fail_fast to set,frozen_set and tuple types
1 parent d4a126e commit 0c85ada

File tree

5 files changed

+36
-1
lines changed

5 files changed

+36
-1
lines changed

src/input/return_enums.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ pub(crate) fn validate_iter_to_set<'py>(
194194
max_length: Option<usize>,
195195
validator: &CombinedValidator,
196196
state: &mut ValidationState<'_, 'py>,
197+
fail_fast: bool,
197198
) -> ValResult<()> {
198199
let mut errors: Vec<ValLineError> = Vec::new();
199200
for (index, item_result) in iter.enumerate() {
@@ -224,6 +225,9 @@ pub(crate) fn validate_iter_to_set<'py>(
224225
Err(ValError::Omit) => (),
225226
Err(err) => return Err(err),
226227
}
228+
if fail_fast && !errors.is_empty() {
229+
break;
230+
}
227231
}
228232

229233
if errors.is_empty() {

src/validators/frozenset.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct FrozenSetValidator {
1717
min_length: Option<usize>,
1818
max_length: Option<usize>,
1919
name: String,
20+
fail_fast: bool,
2021
}
2122

2223
impl BuildValidator for FrozenSetValidator {
@@ -42,6 +43,7 @@ impl Validator for FrozenSetValidator {
4243
max_length: self.max_length,
4344
item_validator: &self.item_validator,
4445
state,
46+
fail_fast: self.fail_fast,
4547
})??;
4648
min_length_check!(input, "Frozenset", self.min_length, f_set);
4749
Ok(f_set.into_py(py))
@@ -59,6 +61,7 @@ struct ValidateToFrozenSet<'a, 's, 'py, I: Input<'py> + ?Sized> {
5961
max_length: Option<usize>,
6062
item_validator: &'a CombinedValidator,
6163
state: &'a mut ValidationState<'s, 'py>,
64+
fail_fast: bool,
6265
}
6366

6467
impl<'py, T, I> ConsumeIterator<PyResult<T>> for ValidateToFrozenSet<'_, '_, 'py, I>
@@ -77,6 +80,7 @@ where
7780
self.max_length,
7881
self.item_validator,
7982
self.state,
83+
self.fail_fast,
8084
)
8185
}
8286
}

src/validators/set.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub struct SetValidator {
1515
min_length: Option<usize>,
1616
max_length: Option<usize>,
1717
name: String,
18+
fail_fast: bool,
1819
}
1920

2021
macro_rules! set_build {
@@ -42,6 +43,7 @@ macro_rules! set_build {
4243
min_length: schema.get_as(pyo3::intern!(py, "min_length"))?,
4344
max_length,
4445
name,
46+
fail_fast: schema.get_as(pyo3::intern!(py, "fail_fast"))?.unwrap_or(false),
4547
}
4648
.into())
4749
}
@@ -72,6 +74,7 @@ impl Validator for SetValidator {
7274
max_length: self.max_length,
7375
item_validator: &self.item_validator,
7476
state,
77+
fail_fast: self.fail_fast,
7578
})??;
7679
min_length_check!(input, "Set", self.min_length, set);
7780
Ok(set.into_py(py))
@@ -89,6 +92,7 @@ struct ValidateToSet<'a, 's, 'py, I: Input<'py> + ?Sized> {
8992
max_length: Option<usize>,
9093
item_validator: &'a CombinedValidator,
9194
state: &'a mut ValidationState<'s, 'py>,
95+
fail_fast: bool,
9296
}
9397

9498
impl<'py, T, I> ConsumeIterator<PyResult<T>> for ValidateToSet<'_, '_, 'py, I>
@@ -107,6 +111,7 @@ where
107111
self.max_length,
108112
self.item_validator,
109113
self.state,
114+
self.fail_fast,
110115
)
111116
}
112117
}

src/validators/tuple.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub struct TupleValidator {
1919
min_length: Option<usize>,
2020
max_length: Option<usize>,
2121
name: String,
22+
fail_fast: bool,
2223
}
2324

2425
impl BuildValidator for TupleValidator {
@@ -50,6 +51,7 @@ impl BuildValidator for TupleValidator {
5051
min_length: schema.get_as(intern!(py, "min_length"))?,
5152
max_length: schema.get_as(intern!(py, "max_length"))?,
5253
name,
54+
fail_fast: schema.get_as(intern!(py, "fail_fast"))?.unwrap_or(false),
5355
}
5456
.into())
5557
}
@@ -69,6 +71,7 @@ impl TupleValidator {
6971
item_validators: &[CombinedValidator],
7072
collection_iter: &mut NextCountingIterator<impl Iterator<Item = I>>,
7173
actual_length: Option<usize>,
74+
fail_fast: bool,
7275
) -> ValResult<()> {
7376
// Validate the head:
7477
for validator in item_validators {
@@ -90,6 +93,9 @@ impl TupleValidator {
9093
}
9194
}
9295
}
96+
if fail_fast && !errors.is_empty() {
97+
return Ok(());
98+
}
9399
}
94100

95101
Ok(())
@@ -128,8 +134,13 @@ impl TupleValidator {
128134
head_validators,
129135
collection_iter,
130136
actual_length,
137+
self.fail_fast,
131138
)?;
132139

140+
if self.fail_fast && !errors.is_empty() {
141+
return Ok(output);
142+
}
143+
133144
let n_tail_validators = tail_validators.len();
134145
if n_tail_validators == 0 {
135146
for (index, input_item) in collection_iter {
@@ -141,6 +152,10 @@ impl TupleValidator {
141152
Err(ValError::Omit) => (),
142153
Err(err) => return Err(err),
143154
}
155+
156+
if self.fail_fast && !errors.is_empty() {
157+
return Ok(output);
158+
}
144159
}
145160
} else {
146161
// Populate a buffer with the first n_tail_validators items
@@ -172,6 +187,10 @@ impl TupleValidator {
172187
Err(ValError::Omit) => (),
173188
Err(err) => return Err(err),
174189
}
190+
191+
if self.fail_fast && !errors.is_empty() {
192+
return Ok(output);
193+
}
175194
}
176195

177196
// Validate the buffered items using the tail validators
@@ -184,6 +203,7 @@ impl TupleValidator {
184203
tail_validators,
185204
&mut NextCountingIterator::new(tail_buffer.into_iter(), index),
186205
actual_length,
206+
self.fail_fast,
187207
)?;
188208
}
189209
} else {
@@ -197,6 +217,7 @@ impl TupleValidator {
197217
&self.validators,
198218
collection_iter,
199219
actual_length,
220+
self.fail_fast,
200221
)?;
201222

202223
// Generate an error if there are any extra items:

tests/validators/test_frozenset.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ def test_repr():
248248
'title="frozenset[any]",'
249249
'validator=FrozenSet(FrozenSetValidator{'
250250
'strict:true,item_validator:Any(AnyValidator),min_length:Some(42),max_length:None,'
251-
'name:"frozenset[any]"'
251+
'name:"frozenset[any]",'
252+
'fail_fast:false'
252253
'}),'
253254
'definitions=[],'
254255
'cache_strings=True)'

0 commit comments

Comments
 (0)