Functional Weave
Code in Rust

validation.gs1-check-digit@1.0.0

impl/python/is_gs1_check_digit_valid.py

592 bytes · the Python implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

from .validation_gs1_check_digit_gs1_check_digit import gs1_check_digit  ← gs1CheckDigit, another function of this group · built into the same file, even by a slim install


def is_gs1_check_digit_valid(key: str) -> bool:
    """Whether the last digit of `key` is the GS1 check digit of the digits
    before it.

    Only the arithmetic: the length, the prefix and an all-zero key are the
    caller's to judge. Validators answer rather than raise, so anything that is
    not two or more ASCII digits is False.
    """
    if not isinstance(key, str) or len(key) < 2 or any(ch < "0" or ch > "9" for ch in key):
        return False
    return gs1_check_digit(key[:-1]) == ord(key[-1]) - 48