Skip to content

Commit 75664c5

Browse files
committed
fix logic
1 parent fd9030b commit 75664c5

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

src/validators/function.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ fn destructure_function_schema(schema: &PyDict) -> PyResult<(bool, &PyAny)> {
2020
let func: &PyDict = schema.get_as_req(intern!(schema.py(), "function"))?;
2121
let call: &PyAny = func.get_as_req(intern!(schema.py(), "call"))?;
2222
let func_type: &str = func.get_as_req(intern!(schema.py(), "type"))?;
23-
let is_model_instance_method = match func_type {
23+
let is_field_validator = match func_type {
2424
"field" => true,
2525
"general" => false,
2626
_ => unreachable!(),
2727
};
28-
Ok((is_model_instance_method, call))
28+
Ok((is_field_validator, call))
2929
}
3030

3131
impl BuildValidator for FunctionBuilder {
@@ -57,7 +57,7 @@ macro_rules! impl_build {
5757
) -> PyResult<CombinedValidator> {
5858
let py = schema.py();
5959
let validator = build_validator(schema.get_as_req(intern!(py, "schema"))?, config, build_context)?;
60-
let (is_model_instance_method, function) = destructure_function_schema(schema)?;
60+
let (is_field_validator, function) = destructure_function_schema(schema)?;
6161
let name = format!(
6262
"{}[{}(), {}]",
6363
$name,
@@ -72,7 +72,7 @@ macro_rules! impl_build {
7272
None => py.None(),
7373
},
7474
name,
75-
is_model_instance_method,
75+
is_field_validator,
7676
}
7777
.into())
7878
}
@@ -86,7 +86,7 @@ pub struct FunctionBeforeValidator {
8686
func: PyObject,
8787
config: PyObject,
8888
name: String,
89-
is_model_instance_method: bool,
89+
is_field_validator: bool,
9090
}
9191

9292
impl_build!(FunctionBeforeValidator, "function-before");
@@ -100,7 +100,7 @@ impl Validator for FunctionBeforeValidator {
100100
slots: &'data [CombinedValidator],
101101
recursion_guard: &'s mut RecursionGuard,
102102
) -> ValResult<'data, PyObject> {
103-
let info = ValidationInfo::new(py, extra, &self.config, self.is_model_instance_method)?;
103+
let info = ValidationInfo::new(py, extra, &self.config, self.is_field_validator)?;
104104
let value = self
105105
.func
106106
.call1(py, (input.to_object(py), info))
@@ -129,7 +129,7 @@ pub struct FunctionAfterValidator {
129129
func: PyObject,
130130
config: PyObject,
131131
name: String,
132-
is_model_instance_method: bool,
132+
is_field_validator: bool,
133133
}
134134

135135
impl_build!(FunctionAfterValidator, "function-after");
@@ -144,7 +144,7 @@ impl Validator for FunctionAfterValidator {
144144
recursion_guard: &'s mut RecursionGuard,
145145
) -> ValResult<'data, PyObject> {
146146
let v = self.validator.validate(py, input, extra, slots, recursion_guard)?;
147-
let info = ValidationInfo::new(py, extra, &self.config, self.is_model_instance_method)?;
147+
let info = ValidationInfo::new(py, extra, &self.config, self.is_field_validator)?;
148148
self.func.call1(py, (v, info)).map_err(|e| convert_err(py, e, input))
149149
}
150150

@@ -166,21 +166,21 @@ pub struct FunctionPlainValidator {
166166
func: PyObject,
167167
config: PyObject,
168168
name: String,
169-
is_model_instance_method: bool,
169+
is_field_validator: bool,
170170
}
171171

172172
impl FunctionPlainValidator {
173173
pub fn build(schema: &PyDict, config: Option<&PyDict>) -> PyResult<CombinedValidator> {
174174
let py = schema.py();
175-
let (is_model_instance_method, function) = destructure_function_schema(schema)?;
175+
let (is_field_validator, function) = destructure_function_schema(schema)?;
176176
Ok(Self {
177177
func: function.into_py(py),
178178
config: match config {
179179
Some(c) => c.into(),
180180
None => py.None(),
181181
},
182182
name: format!("function-plain[{}()]", function_name(function)?),
183-
is_model_instance_method,
183+
is_field_validator,
184184
}
185185
.into())
186186
}
@@ -195,7 +195,7 @@ impl Validator for FunctionPlainValidator {
195195
_slots: &'data [CombinedValidator],
196196
_recursion_guard: &'s mut RecursionGuard,
197197
) -> ValResult<'data, PyObject> {
198-
let info = ValidationInfo::new(py, extra, &self.config, self.is_model_instance_method)?;
198+
let info = ValidationInfo::new(py, extra, &self.config, self.is_field_validator)?;
199199
self.func
200200
.call1(py, (input.to_object(py), info))
201201
.map_err(|e| convert_err(py, e, input))
@@ -212,7 +212,7 @@ pub struct FunctionWrapValidator {
212212
func: PyObject,
213213
config: PyObject,
214214
name: String,
215-
is_model_instance_method: bool,
215+
is_field_validator: bool,
216216
}
217217

218218
impl_build!(FunctionWrapValidator, "function-wrap");
@@ -229,7 +229,7 @@ impl Validator for FunctionWrapValidator {
229229
let call_next_validator = ValidatorCallable {
230230
validator: InternalValidator::new(py, "ValidatorCallable", &self.validator, slots, extra, recursion_guard),
231231
};
232-
let info = ValidationInfo::new(py, extra, &self.config, self.is_model_instance_method)?;
232+
let info = ValidationInfo::new(py, extra, &self.config, self.is_field_validator)?;
233233
self.func
234234
.call1(py, (input.to_object(py), call_next_validator, info))
235235
.map_err(|e| convert_err(py, e, input))
@@ -327,11 +327,11 @@ pub struct ValidationInfo {
327327
}
328328

329329
impl ValidationInfo {
330-
fn new(py: Python, extra: &Extra, config: &PyObject, is_model_instance_method: bool) -> PyResult<Self> {
331-
let res = if is_model_instance_method {
330+
fn new(py: Python, extra: &Extra, config: &PyObject, is_field_validator: bool) -> PyResult<Self> {
331+
let res = if is_field_validator {
332332
let field_name = match extra.field_name {
333-
Some(v) => v,
334-
None => return Err(PyRuntimeError::new_err("This validator expected to be run inside the context of a model field but not model field was found")),
333+
Some(field_name) => field_name,
334+
_ => return Err(PyRuntimeError::new_err("This validator expected to be run inside the context of a model field but not model field was found")),
335335
};
336336
Self {
337337
config: config.clone_ref(py),

src/validators/typed_dict.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,13 @@ impl TypedDictValidator {
348348
where
349349
'data: 's,
350350
{
351+
let extra = Extra {
352+
data: extra.data,
353+
field_name: Some(field),
354+
assignee_field: None,
355+
strict: extra.strict,
356+
context: extra.context,
357+
};
351358
// TODO probably we should set location on errors here
352359
let data = match extra.data {
353360
Some(data) => data,
@@ -380,12 +387,12 @@ impl TypedDictValidator {
380387
if field.frozen {
381388
Err(ValError::new_with_loc(ErrorType::Frozen, input, field.name.to_string()))
382389
} else {
383-
prepare_result(field.validator.validate(py, input, extra, slots, recursion_guard))
390+
prepare_result(field.validator.validate(py, input, &extra, slots, recursion_guard))
384391
}
385392
} else if self.check_extra && !self.forbid_extra {
386393
// this is the "allow" case of extra_behavior
387394
match self.extra_validator {
388-
Some(ref validator) => prepare_result(validator.validate(py, input, extra, slots, recursion_guard)),
395+
Some(ref validator) => prepare_result(validator.validate(py, input, &extra, slots, recursion_guard)),
389396
None => prepare_tuple(input.to_object(py)),
390397
}
391398
} else {

0 commit comments

Comments
 (0)