From f2aede9e0263b1460edce0e3b2fad873d76a0be1 Mon Sep 17 00:00:00 2001 From: Dmitriy-Klv Date: Mon, 20 Apr 2026 21:52:55 +0200 Subject: [PATCH 1/3] the task #2 from HW #2 was completed --- HWs/DmitriyK/HM2/main.py | 82 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 HWs/DmitriyK/HM2/main.py diff --git a/HWs/DmitriyK/HM2/main.py b/HWs/DmitriyK/HM2/main.py new file mode 100644 index 0000000..ce63252 --- /dev/null +++ b/HWs/DmitriyK/HM2/main.py @@ -0,0 +1,82 @@ +import pydantic +from pydantic import (BaseModel, EmailStr, Field, + field_validator) + + +class Address(BaseModel): + city: str = Field(..., min_length=2) + street: str = Field(..., min_length=3) + house_number: int = Field(..., gt=0) + + +class User(BaseModel): + name: str = Field(..., min_length=2, pattern=r"^[a-zA-Z]+$") + age: int = Field(..., ge=0, le=120) + email: EmailStr + is_employed: bool = Field(...) + address: Address + + @field_validator("age") + @classmethod + def validate_age_and_employment(cls, value: int, info): + is_working = info.data.get("is_employed") + + if is_working is True and value < 18: + raise ValueError( + "A user under 18 years of age cannot have the status 'employed'" + ) + + return value + + +def process_user_registration(json_str: str): + try: + user = User.model_validate_json(json_str) + return user.model_dump_json() + except pydantic.ValidationError as e: + return f"Validation error: {e.json()}" + + +if __name__ == "__main__": + good_json = """ + { + "name": "Bob", + "age": 25, + "email": "bob@example.com", + "is_employed": true, + "address": { + "city": "Berlin", + "street": "Strasse", + "house_number": 10 + } + } + """ + + bad_age_json = """ + { + "name": "Alex", + "age": 15, + "email": "alex@example.com", + "is_employed": true, + "address": {"city": "Munich", "street": "Strasse", "house_number": 5} + } + """ + + bad_name_json = """ + { + "name": "Tom", + "age": 20, + "email": "tom@example.com", + "is_employed": false, + "address": {"city": "Berlin", "street": "Strasse", "house_number": 1} + } + """ + + print("--- Successful registration ---") + print(process_user_registration(good_json)) + + print("\n--- Error (age + job) ---") + print(process_user_registration(bad_age_json)) + + print("\n--- Error (name with numbers) ---") + print(process_user_registration(bad_name_json)) From d423d08babbc4893dc9c3b12a2ddc2d03104fdb4 Mon Sep 17 00:00:00 2001 From: Dmitriy-Klv Date: Wed, 22 Apr 2026 10:48:43 +0200 Subject: [PATCH 2/3] update HM2 --- HWs/DmitriyK/HM2/main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/HWs/DmitriyK/HM2/main.py b/HWs/DmitriyK/HM2/main.py index ce63252..a3efacc 100644 --- a/HWs/DmitriyK/HM2/main.py +++ b/HWs/DmitriyK/HM2/main.py @@ -17,7 +17,6 @@ class User(BaseModel): address: Address @field_validator("age") - @classmethod def validate_age_and_employment(cls, value: int, info): is_working = info.data.get("is_employed") From 55f49038ef2d89094dff8feea2904a93405274ef Mon Sep 17 00:00:00 2001 From: Dmitriy-Klv Date: Wed, 22 Apr 2026 14:43:16 +0200 Subject: [PATCH 3/3] update HM2 final --- HWs/DmitriyK/HM2/main.py | 88 ++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 53 deletions(-) diff --git a/HWs/DmitriyK/HM2/main.py b/HWs/DmitriyK/HM2/main.py index a3efacc..7b68fbc 100644 --- a/HWs/DmitriyK/HM2/main.py +++ b/HWs/DmitriyK/HM2/main.py @@ -1,81 +1,63 @@ import pydantic -from pydantic import (BaseModel, EmailStr, Field, - field_validator) - +from pydantic import BaseModel, EmailStr, Field, model_validator class Address(BaseModel): city: str = Field(..., min_length=2) street: str = Field(..., min_length=3) house_number: int = Field(..., gt=0) - class User(BaseModel): - name: str = Field(..., min_length=2, pattern=r"^[a-zA-Z]+$") + name: str = Field(..., min_length=2, pattern=r"^[a-zA-Z\s]+$") age: int = Field(..., ge=0, le=120) email: EmailStr - is_employed: bool = Field(...) + is_employed: bool address: Address - @field_validator("age") - def validate_age_and_employment(cls, value: int, info): - is_working = info.data.get("is_employed") - - if is_working is True and value < 18: - raise ValueError( - "A user under 18 years of age cannot have the status 'employed'" - ) + @model_validator(mode='after') + def validate_employment_age(self) -> 'User': + if self.is_employed: + if not (18 <= self.age <= 65): + raise ValueError( + f"Employed users must be between 18 and 65 years old. " + f"Current age provided: {self.age}" + ) + return self - return value - -def process_user_registration(json_str: str): +def process_user_registration(json_str: str) -> str: + """ + Deserializes JSON, validates data using Pydantic, + and serializes it back to JSON. + """ try: user = User.model_validate_json(json_str) - return user.model_dump_json() + return user.model_dump_json(indent=4) except pydantic.ValidationError as e: return f"Validation error: {e.json()}" - if __name__ == "__main__": - good_json = """ - { - "name": "Bob", - "age": 25, - "email": "bob@example.com", + success_json = """{ + "name": "John Doe", + "age": 30, + "email": "john.doe@example.com", "is_employed": true, "address": { "city": "Berlin", - "street": "Strasse", - "house_number": 10 + "street": "Friedrichstrasse", + "house_number": 101 } - } - """ + }""" - bad_age_json = """ - { - "name": "Alex", - "age": 15, - "email": "alex@example.com", + failed_age_json = """{ + "name": "Old Working Man", + "age": 70, + "email": "oldman@example.com", "is_employed": true, - "address": {"city": "Munich", "street": "Strasse", "house_number": 5} - } - """ - - bad_name_json = """ - { - "name": "Tom", - "age": 20, - "email": "tom@example.com", - "is_employed": false, - "address": {"city": "Berlin", "street": "Strasse", "house_number": 1} - } - """ - - print("--- Successful registration ---") - print(process_user_registration(good_json)) + "address": {"city": "Hamburg", "street": "Reeperbahn", "house_number": 5} + }""" - print("\n--- Error (age + job) ---") - print(process_user_registration(bad_age_json)) + print("--- SUCCESS CASE ---") + print(process_user_registration(success_json)) - print("\n--- Error (name with numbers) ---") - print(process_user_registration(bad_name_json)) + print("\n--- FAILED CASE (Age 70 + Employed) ---") + print(process_user_registration(failed_age_json)) \ No newline at end of file