From 1fd79c3518cb67b9e9aee002cace1859edcb2dce Mon Sep 17 00:00:00 2001 From: zzstoatzz Date: Sun, 15 Dec 2024 20:11:35 -0600 Subject: [PATCH] eureka! --- .../atproto_client/models/string_formats.py | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/packages/atproto_client/models/string_formats.py b/packages/atproto_client/models/string_formats.py index 06c833da..b9a3699d 100644 --- a/packages/atproto_client/models/string_formats.py +++ b/packages/atproto_client/models/string_formats.py @@ -60,6 +60,21 @@ ) +class _MaybeStrictValidator: + def __init__(self, validate_fn: Callable[..., str]) -> None: + self.validate_fn = validate_fn + self.__name__ = validate_fn.__name__ + self.__doc__ = validate_fn.__doc__ + + def __call__(self, v: str, info: ValidationInfo) -> str: + if info and isinstance(info.context, Mapping) and info.context.get(_OPT_IN_KEY, False): + return cast(core_schema.NoInfoValidatorFunction, self.validate_fn)(v) + return v + + def __repr__(self) -> str: + return f'' + + def only_validate_if_strict(validate_fn: Callable[..., str]) -> Callable[..., str]: """Skip pydantic validation if not opting into strict validation via context. @@ -69,18 +84,7 @@ def only_validate_if_strict(validate_fn: Callable[..., str]) -> Callable[..., st Returns: A wrapped validation function that only validates in strict mode """ - - def wrapper(v: str, info: ValidationInfo) -> str: - if info and isinstance(info.context, Mapping) and info.context.get(_OPT_IN_KEY, False): - return cast(core_schema.NoInfoValidatorFunction, validate_fn)(v) - return v - - # Preserve the original function's name and docstring without - # requiring the decorated function to be passed `info` - wrapper.__name__ = validate_fn.__name__ - wrapper.__doc__ = validate_fn.__doc__ - - return wrapper + return _MaybeStrictValidator(validate_fn) @only_validate_if_strict