Skip to content

Commit

Permalink
📝 add more documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
yezz123 committed Jan 12, 2024
1 parent 4ff6d9a commit c0272fd
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
42 changes: 39 additions & 3 deletions pydantic_extra_types/isbn.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,51 @@ class Book(BaseModel):

@classmethod
def __get_pydantic_core_schema__(cls, source: type[Any], handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
"""
Return a Pydantic CoreSchema with the ISBN validation.
Args:
source: The source type to be converted.
handler: The handler to get the CoreSchema.
Returns:
A Pydantic CoreSchema with the ISBN validation.
"""
return core_schema.with_info_before_validator_function(
cls._validate,
core_schema.str_schema(),
)

@classmethod
def _validate(cls, __input_value: str, _: Any) -> str:
"""
Validate a ISBN from the provided str value.
Args:
__input_value: The str value to be validated.
_: The source type to be converted.
Returns:
The validated ISBN.
Raises:
PydanticCustomError: If the ISBN is not valid.
"""
cls.validate_isbn_format(__input_value)

return cls.convert_isbn10_to_isbn13(__input_value)

@staticmethod
def validate_isbn_format(value: str) -> None:
"""Validate a ISBN format from the provided str value."""
"""Validate a ISBN format from the provided str value.
Args:
value: The str value to be validated.
Raises:
PydanticCustomError: If the ISBN is not valid.
"""

isbn_length = len(value)

Expand All @@ -102,8 +133,13 @@ def validate_isbn_format(value: str) -> None:

@staticmethod
def convert_isbn10_to_isbn13(value: str) -> str:
"""
Convert an ISBN-10 to ISBN-13.
"""Convert an ISBN-10 to ISBN-13.
Args:
value: The ISBN-10 value to be converted.
Returns:
The converted ISBN-13 value.
"""

if len(value) == 10:
Expand Down
22 changes: 22 additions & 0 deletions pydantic_extra_types/mac_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,35 @@ class Network(BaseModel):

@classmethod
def __get_pydantic_core_schema__(cls, source: type[Any], handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
"""
Return a Pydantic CoreSchema with the MAC address validation.
Args:
source: The source type to be converted.
handler: The handler to get the CoreSchema.
Returns:
A Pydantic CoreSchema with the MAC address validation.
"""
return core_schema.with_info_before_validator_function(
cls._validate,
core_schema.str_schema(),
)

@classmethod
def _validate(cls, __input_value: str, _: Any) -> str:
"""
Validate a MAC Address from the provided str value.
Args:
__input_value: The str value to be validated.
_: The source type to be converted.
Returns:
The validated MAC Address.
"""
return cls.validate_mac_address(__input_value.encode())

@staticmethod
Expand Down

0 comments on commit c0272fd

Please sign in to comment.