From 6ea5d204aeda5f08d0467b0b748ad9d28eb9aec0 Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Date: Wed, 9 Apr 2025 21:37:58 -0300 Subject: [PATCH] Final Working Pydantics Validations --- jambo/parser/array_type_parser.py | 11 ++++-- tests/test_schema_converter.py | 62 +++++++++++++++++++++++++++---- 2 files changed, 62 insertions(+), 11 deletions(-) diff --git a/jambo/parser/array_type_parser.py b/jambo/parser/array_type_parser.py index f0329ac..e7f6a6e 100644 --- a/jambo/parser/array_type_parser.py +++ b/jambo/parser/array_type_parser.py @@ -21,9 +21,12 @@ class ArrayTypeParser(GenericTypeParser): ).from_properties(name, properties["items"]) _mappings = { - "maxItems": "max_items", - "minItems": "min_items", - "uniqueItems": "unique_items", + "maxItems": "max_length", + "minItems": "min_length", } - 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 + ) diff --git a/tests/test_schema_converter.py b/tests/test_schema_converter.py index d6403f8..c24d160 100644 --- a/tests/test_schema_converter.py +++ b/tests/test_schema_converter.py @@ -32,7 +32,13 @@ class TestSchemaConverter(TestCase): "description": "A person", "type": "object", "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"], } @@ -41,13 +47,29 @@ class TestSchemaConverter(TestCase): 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): schema = { "title": "Person", "description": "A person", "type": "object", "properties": { - "age": {"type": "integer"}, + "age": { + "type": "integer", + "minimum": 0, + "maximum": 120, + }, }, "required": ["age"], } @@ -56,7 +78,11 @@ class TestSchemaConverter(TestCase): 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): schema = { @@ -64,7 +90,11 @@ class TestSchemaConverter(TestCase): "description": "A person", "type": "object", "properties": { - "age": {"type": "number"}, + "age": { + "type": "number", + "minimum": 0, + "maximum": 120, + }, }, "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) + with self.assertRaises(ValueError): + model(age=-1.0) + + with self.assertRaises(ValueError): + model(age=121.0) def test_validation_boolean(self): schema = { @@ -98,14 +132,28 @@ class TestSchemaConverter(TestCase): "description": "A person", "type": "object", "properties": { - "friends": {"type": "array", "items": {"type": "string"}}, + "friends": { + "type": "array", + "items": {"type": "string"}, + "minItems": 1, + "maxItems": 2, + "uniqueItems": True, + }, }, "required": ["friends"], } 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): schema = {