Skip to content

Commit

Permalink
Forward default_value calls to the underlying schema for `Definitio…
Browse files Browse the repository at this point in the history
…nRefValidator` (#1573)
  • Loading branch information
Viicos authored and davidhewitt committed Dec 18, 2024
1 parent 3a0cea9 commit 253cfe0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/validators/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ impl Validator for DefinitionRefValidator {
})
}

fn default_value<'py>(
&self,
_py: Python<'py>,
_outer_loc: Option<impl Into<crate::errors::LocItem>>,
_state: &mut ValidationState<'_, 'py>,
) -> ValResult<Option<PyObject>> {
self.definition.read(|validator| {
let validator = validator.unwrap();
validator.default_value(_py, _outer_loc, _state)
})
}

fn validate_assignment<'py>(
&self,
py: Python<'py>,
Expand Down
14 changes: 14 additions & 0 deletions tests/validators/test_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,17 @@ def test_definition_chain():
),
)
assert v.validate_python('1') == 1


def test_forwards_get_default_value():
v = SchemaValidator(
core_schema.definitions_schema(
core_schema.definition_reference_schema('foo'),
[core_schema.with_default_schema(core_schema.int_schema(), default=1, ref='foo')],
)
)

default = v.get_default_value()

assert default is not None
assert default.value == 1

0 comments on commit 253cfe0

Please sign in to comment.