# coding: utf-8

"""
    用户全部 API 文档

    No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)

    The version of the OpenAPI document: v1
    Generated by OpenAPI Generator (https://openapi-generator.tech)

    Do not edit the class manually.
"""  # noqa: E501


from __future__ import annotations
import pprint
import re  # noqa: F401
import json

from pydantic import BaseModel, ConfigDict, Field, StrictStr, field_validator
from typing import Any, ClassVar, Dict, List, Optional
from typing_extensions import Annotated
from typing import Optional, Set
from typing_extensions import Self

class GrantRequest(BaseModel):
    """
    GrantRequest
    """ # noqa: E501
    redirect_uri: Optional[StrictStr] = Field(default=None, description="返回地址。默认无限制，也可在【安全-开放认证网址白名单】配置")
    grant_type: Annotated[str, Field(min_length=1, strict=True)] = Field(description="授权类型。可选：email、phone、unionid、account")
    scopes: Annotated[str, Field(min_length=1, strict=True)] = Field(description="自定义授权范围，用英文空格分隔")
    user_name: Optional[StrictStr] = Field(default=None, description="用户名。授权类型为：email/phone/account必填", alias="userName")
    password: Optional[StrictStr] = Field(default=None, description="登录密码。授权类型为：email/phone/account必填")
    union_id: Optional[StrictStr] = Field(default=None, description="unionId。授权类型为：unionid必填", alias="unionId")
    platform: Optional[StrictStr] = Field(default=None, description="平台。授权类型为：unionid必填")
    expire_in_days: Optional[Annotated[int, Field(le=99, strict=True, ge=1)]] = Field(default=None, description="授权有效期。1~99天", alias="expireInDays")
    __properties: ClassVar[List[str]] = ["redirect_uri", "grant_type", "scopes", "userName", "password", "unionId", "platform", "expireInDays"]

    @field_validator('grant_type')
    def grant_type_validate_regular_expression(cls, value):
        """Validates the regular expression"""
        if not re.match(r"^(email|phone|unionid|account)$", value):
            raise ValueError(r"must validate the regular expression /^(email|phone|unionid|account)$/")
        return value

    model_config = ConfigDict(
        populate_by_name=True,
        validate_assignment=True,
        protected_namespaces=(),
    )


    def to_str(self) -> str:
        """Returns the string representation of the model using alias"""
        return pprint.pformat(self.model_dump(by_alias=True))

    def to_json(self) -> str:
        """Returns the JSON representation of the model using alias"""
        # TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
        return json.dumps(self.to_dict())

    @classmethod
    def from_json(cls, json_str: str) -> Optional[Self]:
        """Create an instance of GrantRequest from a JSON string"""
        return cls.from_dict(json.loads(json_str))

    def to_dict(self) -> Dict[str, Any]:
        """Return the dictionary representation of the model using alias.

        This has the following differences from calling pydantic's
        `self.model_dump(by_alias=True)`:

        * `None` is only added to the output dict for nullable fields that
          were set at model initialization. Other fields with value `None`
          are ignored.
        """
        excluded_fields: Set[str] = set([
        ])

        _dict = self.model_dump(
            by_alias=True,
            exclude=excluded_fields,
            exclude_none=True,
        )
        # set to None if redirect_uri (nullable) is None
        # and model_fields_set contains the field
        if self.redirect_uri is None and "redirect_uri" in self.model_fields_set:
            _dict['redirect_uri'] = None

        # set to None if user_name (nullable) is None
        # and model_fields_set contains the field
        if self.user_name is None and "user_name" in self.model_fields_set:
            _dict['userName'] = None

        # set to None if password (nullable) is None
        # and model_fields_set contains the field
        if self.password is None and "password" in self.model_fields_set:
            _dict['password'] = None

        # set to None if union_id (nullable) is None
        # and model_fields_set contains the field
        if self.union_id is None and "union_id" in self.model_fields_set:
            _dict['unionId'] = None

        # set to None if platform (nullable) is None
        # and model_fields_set contains the field
        if self.platform is None and "platform" in self.model_fields_set:
            _dict['platform'] = None

        return _dict

    @classmethod
    def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
        """Create an instance of GrantRequest from a dict"""
        if obj is None:
            return None

        if not isinstance(obj, dict):
            return cls.model_validate(obj)

        _obj = cls.model_validate({
            "redirect_uri": obj.get("redirect_uri"),
            "grant_type": obj.get("grant_type"),
            "scopes": obj.get("scopes"),
            "userName": obj.get("userName"),
            "password": obj.get("password"),
            "unionId": obj.get("unionId"),
            "platform": obj.get("platform"),
            "expireInDays": obj.get("expireInDays")
        })
        return _obj


