Skip to content

Commit 4741091

Browse files
committed
implement ask() on WithDefaultValidator
1 parent 662cf50 commit 4741091

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/validators/with_default.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ impl Validator for WithDefaultValidator {
103103
&self.name
104104
}
105105

106+
fn ask(&self, question: &str) -> bool {
107+
self.validator.ask(question)
108+
}
109+
106110
fn complete(&mut self, build_context: &BuildContext) -> PyResult<()> {
107111
self.validator.complete(build_context)
108112
}

tests/validators/test_with_default.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from pydantic_core import SchemaError, SchemaValidator
44

5-
from ..conftest import PyAndJson
5+
from ..conftest import PyAndJson, plain_repr
66

77

88
def test_typed_dict_default():
@@ -222,3 +222,36 @@ def test_on_error_wrong():
222222
def test_build_default_and_default_factory():
223223
with pytest.raises(SchemaError, match="'default' and 'default_factory' cannot be used together"):
224224
SchemaValidator({'type': 'default', 'schema': 'int', 'default_factory': lambda: 1, 'default': 2})
225+
226+
227+
def test_model_class():
228+
class MyModel:
229+
__slots__ = '__dict__', '__fields_set__'
230+
field_a: str
231+
field_b: int
232+
233+
v = SchemaValidator(
234+
{
235+
'type': 'new-class',
236+
'class_type': MyModel,
237+
'schema': {
238+
'type': 'default',
239+
'schema': {
240+
'type': 'typed-dict',
241+
'return_fields_set': True,
242+
'fields': {'field_a': {'schema': {'type': 'str'}}, 'field_b': {'schema': {'type': 'int'}}},
243+
},
244+
'default': ({'field_a': '[default-a]', 'field_b': '[default-b]'}, ()),
245+
'on_error': 'default',
246+
},
247+
}
248+
)
249+
assert 'expect_fields_set:true' in plain_repr(v)
250+
m = v.validate_python({'field_a': 'test', 'field_b': 12})
251+
assert isinstance(m, MyModel)
252+
assert m.field_a == 'test'
253+
assert m.field_b == 12
254+
m = v.validate_python({'field_a': 'test', 'field_b': 'wrong'})
255+
assert isinstance(m, MyModel)
256+
assert m.field_a == '[default-a]'
257+
assert m.field_b == '[default-b]'

0 commit comments

Comments
 (0)