Skip to content
You're viewing docs for Dev. See the latest version →

Pydantic Extra Types Isbn

The pydantic_extra_types.isbn module provides functionality to receive and validate ISBN.

ISBN (International Standard Book Number) is a numeric commercial book identifier which is intended to be unique. This module provides an ISBN type for Pydantic models.

ISBN

Bases: str

Represents a ISBN and provides methods for conversion, validation, and serialization.

from pydantic import BaseModel

from pydantic_extra_types.isbn import ISBN


class Book(BaseModel):
    isbn: ISBN


book = Book(isbn='8537809667')
print(book)
# > isbn='9788537809662'

Methods

validate_isbn_format

@staticmethod

def validate_isbn_format(value: str) -> None

Validate a ISBN format from the provided str value.

Returns

None

Parameters

value : str

The str value representing the ISBN in 10 or 13 digits.

Raises
  • PydanticCustomError — If the ISBN is not valid.

convert_isbn10_to_isbn13

@staticmethod

def convert_isbn10_to_isbn13(value: str) -> str

Convert an ISBN-10 to ISBN-13.

Returns

str — The converted ISBN or the original value if no conversion is necessary.

Parameters

value : str

The ISBN-10 value to be converted.

isbn10_digit_calc

def isbn10_digit_calc(isbn: str) -> str

Calculate the ISBN-10 check digit from the provided str value. More information on the validation algorithm on Wikipedia

Returns

str — The calculated last digit of the ISBN-10 value.

Parameters

isbn : str

The str value representing the ISBN in 10 digits.

isbn13_digit_calc

def isbn13_digit_calc(isbn: str) -> str

Calc a ISBN-13 last digit from the provided str value. More information on the validation algorithm on Wikipedia

Returns

str — The calculated last digit of the ISBN-13 value.

Parameters

isbn : str

The str value representing the ISBN in 13 digits.