Initial Implementation of Default
This commit is contained in:
@@ -90,5 +90,5 @@ class SchemaConverter:
|
|||||||
if description := properties.get("description"):
|
if description := properties.get("description"):
|
||||||
_field_args["description"] = description
|
_field_args["description"] = description
|
||||||
|
|
||||||
_default_value = ... if name in required_keys else None
|
_default_value = ... if name in required_keys else properties.get("default")
|
||||||
return _field_type, Field(_default_value, **_field_args)
|
return _field_type, Field(_default_value, **_field_args)
|
||||||
|
|||||||
@@ -179,3 +179,25 @@ class TestSchemaConverter(TestCase):
|
|||||||
|
|
||||||
self.assertEqual(obj.address.street, "123 Main St")
|
self.assertEqual(obj.address.street, "123 Main St")
|
||||||
self.assertEqual(obj.address.city, "Springfield")
|
self.assertEqual(obj.address.city, "Springfield")
|
||||||
|
|
||||||
|
def test_default(self):
|
||||||
|
schema = {
|
||||||
|
"title": "Person",
|
||||||
|
"description": "A person",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"name": {"type": "string"},
|
||||||
|
"age": {
|
||||||
|
"type": "integer",
|
||||||
|
"default": 30,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"required": ["name"],
|
||||||
|
}
|
||||||
|
|
||||||
|
model = SchemaConverter.build(schema)
|
||||||
|
|
||||||
|
obj = model(name="John")
|
||||||
|
|
||||||
|
self.assertEqual(obj.name, "John")
|
||||||
|
self.assertEqual(obj.age, 30)
|
||||||
|
|||||||
Reference in New Issue
Block a user