Get Membership To Play All Videos In This Website.
Your Dashboard
Sign-in
Sign-up
RegularPython
  • Membership
  • DataEngineering
  • Basics
    • Realtime Projects In Telugu
    • Video Courses
      • Python Basics In Telugu
      • AWS Projects In Telugu
      • AWS Projects In English
    • Online Tests
      • Online Test
      • Interview Questions
    • Online Store
    • Student Reviews
    • Mock Interviews
    1. Home
    2. Python Basic Courses

    regularpython@gmail.com

    Image

    Pydantic with custom validations

    • Category
      AWS Lambda, Pydantic, Python, User Management, API Gateway, RDS, Data Validation
    • Course Menu
      • Introduction to Pydantic
      • Key Features of Pydantic
      • Pydantic Module Features and Use Cases
      • AWS Lambda User Management with Pydantic
      • Pydantic with custom validations

    AWS Lambda User Management Project with Pydantic

    Pydantic Validation Models with Additional Validations

    from pydantic import BaseModel, EmailStr, constr, validator
    
    class UserSignup(BaseModel):
        username: constr(min_length=3, max_length=20, regex=r'^[a-zA-Z0-9_]+$')
        email: EmailStr
        password: constr(min_length=8, regex=r'^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d@$!%*?&]+$')
        confirm_password: str
        phone_number: constr(regex=r'^\\+?[1-9]\\d{1,14}$')  # International format
        age: int
    
        @validator('confirm_password')
        def passwords_match(cls, v, values):
            if v != values.get('password'):
                raise ValueError('Passwords must match')
            return v
    
        @validator('age')
        def age_must_be_valid(cls, v):
            if v < 18:
                raise ValueError('Age must be at least 18')
            return v

    New Validations:

    Username: 3-20 characters, letters, numbers, and underscores only.
    Password: Minimum 8 characters, including at least one letter and one number.
    Phone Number: Must be in international format (e.g., +919876543210).
    Age: Must be at least 18 years old.

    Conclusion

    This enhanced project ensures better data integrity with detailed field validations using Pydantic. Users can easily understand the validation rules and avoid common input errors.

    RegularPython

    This website helps to learn ptyhon from basics to advanced level. Everything explained with some practical examples.

    • About
    • Privacy
    • Video Courses
    • Online Test
    • Python Blog

    Marthahalli, Bengalore.
    regularpython@gmail.com

    © Copyright 2020 RegularPython.

    • Sign-in
    • Sign-up