Final Working Pydantics Validations

This commit is contained in:
2025-04-09 21:37:58 -03:00
parent 63dc0de4b2
commit 6ea5d204ae
2 changed files with 62 additions and 11 deletions

View File

@@ -21,9 +21,12 @@ class ArrayTypeParser(GenericTypeParser):
).from_properties(name, properties["items"]) ).from_properties(name, properties["items"])
_mappings = { _mappings = {
"maxItems": "max_items", "maxItems": "max_length",
"minItems": "min_items", "minItems": "min_length",
"uniqueItems": "unique_items",
} }
return list[_item_type], mappings_properties_builder(properties, _mappings) wrapper_type = set if properties.get("uniqueItems", False) else list
return wrapper_type[_item_type], mappings_properties_builder(
properties, _mappings
)

View File

@@ -32,7 +32,13 @@ class TestSchemaConverter(TestCase):
"description": "A person", "description": "A person",
"type": "object", "type": "object",
"properties": { "properties": {
"name": {"type": "string"}, "name": {"type": "string", "maxLength": 4, "minLength": 1},
"email": {
"type": "string",
"maxLength": 50,
"minLength": 5,
"pattern": r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$",
},
}, },
"required": ["name"], "required": ["name"],
} }
@@ -41,13 +47,29 @@ class TestSchemaConverter(TestCase):
self.assertEqual(model(name="John", age=30).name, "John") self.assertEqual(model(name="John", age=30).name, "John")
with self.assertRaises(ValueError):
model(name=123, age=30, email="teste@hideyoshi.com")
with self.assertRaises(ValueError):
model(name="John Invalid", age=45, email="teste@hideyoshi.com")
with self.assertRaises(ValueError):
model(name="", age=45, email="teste@hideyoshi.com")
with self.assertRaises(ValueError):
model(name="John", age=45, email="hideyoshi.com")
def test_validation_integer(self): def test_validation_integer(self):
schema = { schema = {
"title": "Person", "title": "Person",
"description": "A person", "description": "A person",
"type": "object", "type": "object",
"properties": { "properties": {
"age": {"type": "integer"}, "age": {
"type": "integer",
"minimum": 0,
"maximum": 120,
},
}, },
"required": ["age"], "required": ["age"],
} }
@@ -56,7 +78,11 @@ class TestSchemaConverter(TestCase):
self.assertEqual(model(age=30).age, 30) self.assertEqual(model(age=30).age, 30)
self.assertEqual(model(age="30").age, 30) with self.assertRaises(ValueError):
model(age=-1)
with self.assertRaises(ValueError):
model(age=121)
def test_validation_float(self): def test_validation_float(self):
schema = { schema = {
@@ -64,7 +90,11 @@ class TestSchemaConverter(TestCase):
"description": "A person", "description": "A person",
"type": "object", "type": "object",
"properties": { "properties": {
"age": {"type": "number"}, "age": {
"type": "number",
"minimum": 0,
"maximum": 120,
},
}, },
"required": ["age"], "required": ["age"],
} }
@@ -73,7 +103,11 @@ class TestSchemaConverter(TestCase):
self.assertEqual(model(age=30).age, 30.0) self.assertEqual(model(age=30).age, 30.0)
self.assertEqual(model(age="30").age, 30.0) with self.assertRaises(ValueError):
model(age=-1.0)
with self.assertRaises(ValueError):
model(age=121.0)
def test_validation_boolean(self): def test_validation_boolean(self):
schema = { schema = {
@@ -98,14 +132,28 @@ class TestSchemaConverter(TestCase):
"description": "A person", "description": "A person",
"type": "object", "type": "object",
"properties": { "properties": {
"friends": {"type": "array", "items": {"type": "string"}}, "friends": {
"type": "array",
"items": {"type": "string"},
"minItems": 1,
"maxItems": 2,
"uniqueItems": True,
},
}, },
"required": ["friends"], "required": ["friends"],
} }
model = SchemaConverter.build(schema) model = SchemaConverter.build(schema)
self.assertEqual(model(friends=["John", "Jane"]).friends, ["John", "Jane"]) self.assertEqual(
model(friends=["John", "Jane", "John"]).friends, {"John", "Jane"}
)
with self.assertRaises(ValueError):
model(friends=[])
with self.assertRaises(ValueError):
model(friends=["John", "Jane", "Invalid"])
def test_validation_object(self): def test_validation_object(self):
schema = { schema = {