Skip to content

Commit

Permalink
eureka!
Browse files Browse the repository at this point in the history
  • Loading branch information
zzstoatzz committed Dec 16, 2024
1 parent 967a9c6 commit 1fd79c3
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions packages/atproto_client/models/string_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'<validator {self.validate_fn.__name__}>'


def only_validate_if_strict(validate_fn: Callable[..., str]) -> Callable[..., str]:
"""Skip pydantic validation if not opting into strict validation via context.
Expand All @@ -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
Expand Down

0 comments on commit 1fd79c3

Please sign in to comment.