"""Schema utilities.""" from typing import Any, Optional from marshmallow import fields, validate from users import constants class Auth0Organization(fields.String): """ Auth0 organization field for Marshmallow schemas. This field handles the validation of Auth0 organization names and provides automatic normalization for specific brand names (e.g., converting "theorchard" to "orchard"). """ def __init__(self, *, accept_orchard_brand: bool = True, **kwargs: Any) -> None: """ Initialize the Auth0Organization field. Args: accept_orchard_brand: If True, "theorchard" brand string will be automatically replaced with the "orchard" Auth0 organization name. """ super().__init__(**kwargs) self.accept_orchard_brand = accept_orchard_brand self.validators.insert(0, validate.OneOf(choices=constants.ORG_TYPES)) def _validated(self, value: str) -> str: value = value.lower() if self.accept_orchard_brand and value == constants.ORCHARD_BRAND: value = constants.AUTH0_ORCHARD_ORG_NAME return value def _deserialize( self, value: Any, attr: Optional[str], data: Optional[dict[str, Any]], **kwargs: Any ) -> str: value = super()._deserialize(value, attr, data, **kwargs) return self._validated(value)