Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/validators/model_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,16 @@ impl Validator for ModelFieldsValidator {
}
Err(err) => return Err(err),
};

let state = &mut state.rebind_extra(|extra| extra.field_name = Some(field.name_py.bind(py).clone()));

if let Some((lookup_path, value)) = op_key_value {
if let Some(ref mut used_keys) = used_keys {
// key is "used" whether or not validation passes, since we want to skip this key in
// extra logic either way
used_keys.insert(lookup_path.first_key());
}

let state =
&mut state.rebind_extra(|extra| extra.field_name = Some(field.name_py.bind(py).clone()));

match field.validator.validate(py, value.borrow_input(), state) {
Ok(value) => {
model_dict.set_item(&field.name_py, value)?;
Expand Down
35 changes: 35 additions & 0 deletions tests/validators/test_with_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -967,3 +967,38 @@ def test_default_factory_not_called_union_ok(container_schema_builder) -> None:
v = SchemaValidator(schema)
s = SchemaSerializer(schema)
assert s.to_python(v.validate_python({'a': 1}), mode='json') == {'a': 1, 'b': 2, 'c': 3}


def test_default_validate_default_after_validator_field_name() -> None:
class Model:
pass

field_name: str | None = None

def val_func(value, info: core_schema.ValidationInfo):
nonlocal field_name
field_name = info.field_name
return value

schema = core_schema.model_schema(
cls=Model,
schema=core_schema.model_fields_schema(
fields={
'a': core_schema.model_field(
schema=core_schema.with_default_schema(
schema=core_schema.with_info_after_validator_function(
val_func,
schema=core_schema.str_schema(),
),
default='default',
)
)
}
),
config={'validate_default': True},
)

val = SchemaValidator(schema)
val.validate_python({})

assert field_name == 'a'
Loading