#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ # @Version : Python 3.11.4 # @Software : Sublime Text 4 # @Author : StudentCWZ # @Email : StudentCWZ@outlook.com # @Date : 2023/10/29 00:07 # @File : user.py # @Description : """ from pydantic import BaseModel, EmailStr, field_validator from application.extensions.init_marshmallow import ma from application.extensions.init_sqlalchemy import db from application.models import User from marshmallow import validates, ValidationError class CreateUserItem(BaseModel): username: str password: str email: EmailStr @classmethod @field_validator("username") def name_must_contain_only_characters(cls, value): if not value.isalpha(): raise ValueError("Username must contain only characters.") return value class UserSchema(ma.SQLAlchemyAutoSchema): id = ma.Int(dump_only=True) username = ma.Str(required=True) password = ma.String(load_only=True, required=True) email = ma.Email(required=True) class Meta: model = User sqla_session = db.session load_instance = True exclude = ("_password",) @validates('username') def validate_username(self, username): if len(username) < 3: raise ValidationError('Username must be at least 3 characters.')