Skip to content

value_checks

value_checks

Checks the executor applies to generated replacement values.

This module lives outside replacement.generators so the executor can check candidates without importing generator implementations.

Functions:

Name Description
generated_value_is_valid

Return whether a generated value meets the request's local constraints.

is_ipv4

Return whether value is an IPv4 address.

is_ipv6

Return whether value, with any enclosing URL brackets removed, is an IPv6 address.

is_luhn_valid

Return whether the ASCII digits in value, ignoring separators such as spaces and dashes, pass the Luhn check.

keeps_original_name_part

Return whether a full-name replacement keeps the original's first, middle, or last name.

generated_value_is_valid(request, value)

Return whether a generated value meets the request's local constraints.

A replacement must differ from its original ignoring letter case, because a reused replacement may be written in the case of each occurrence it replaces: Ada accepted for ADA would be written back as ADA. Surrounding whitespace is ignored too: Kenneth for Kenneth keeps the real name, and free text, which never has the padding, would reuse it for every Kenneth.

Source code in src/nemo_safe_synthesizer/pii_replacer/replacement/value_checks.py
def generated_value_is_valid(request: ReplacementGenerationRequest, value: str) -> bool:
    """Return whether a generated value meets the request's local constraints.

    A replacement must differ from its original ignoring letter case, because a reused replacement may be
    written in the case of each occurrence it replaces: ``Ada`` accepted for ``ADA`` would be written back
    as ``ADA``. Surrounding whitespace is ignored too: ``Kenneth`` for `` Kenneth`` keeps the real name,
    and free text, which never has the padding, would reuse it for every ``Kenneth``.
    """
    if not value or value.strip().casefold() == request.original_value.strip().casefold():
        return False
    if request.entity_type is EntityType.CREDIT_DEBIT_CARD:
        return is_luhn_valid(value)
    if request.entity_type is EntityType.IPV4:
        return is_ipv4(value)
    if request.entity_type is EntityType.IPV6:
        return is_ipv6(value)
    return True

is_ipv4(value)

Return whether value is an IPv4 address.

Source code in src/nemo_safe_synthesizer/pii_replacer/replacement/value_checks.py
def is_ipv4(value: str) -> bool:
    """Return whether ``value`` is an IPv4 address."""
    try:
        ipaddress.IPv4Address(value)
    except ValueError:
        return False
    return True

is_ipv6(value)

Return whether value, with any enclosing URL brackets removed, is an IPv6 address.

Source code in src/nemo_safe_synthesizer/pii_replacer/replacement/value_checks.py
def is_ipv6(value: str) -> bool:
    """Return whether ``value``, with any enclosing URL brackets removed, is an IPv6 address."""
    try:
        ipaddress.IPv6Address(value.strip("[]"))
    except ValueError:
        return False
    return True

is_luhn_valid(value)

Return whether the ASCII digits in value, ignoring separators such as spaces and dashes, pass the Luhn check.

Example

is_luhn_valid("4111 1111 1111 1111") is True.

Source code in src/nemo_safe_synthesizer/pii_replacer/replacement/value_checks.py
def is_luhn_valid(value: str) -> bool:
    """Return whether the ASCII digits in ``value``, ignoring separators such as spaces and dashes, pass the Luhn check.

    Example:
        ``is_luhn_valid("4111 1111 1111 1111")`` is ``True``.
    """
    digits = "".join(character for character in value if character in string.digits)
    return len(digits) >= 2 and luhn.is_valid(digits)

keeps_original_name_part(original, candidate, pattern, *, locale='en_US')

Return whether a full-name replacement keeps the original's first, middle, or last name.

A whole-name comparison accepts John Brown for John Smith, which keeps the real first name. Both names are read by nameparser in the order of pattern or locale, so titles such as Mr., suffixes such as Jr., and a Last, First layout do not hide a kept part, and each part is compared with the same part. Words that are fixed text in pattern, such as Dr. in Dr. {First} {Last}, are skipped. Trailing commas and periods are ignored, and single letters are not compared, so a shared initial does not count.

Example

keeps_original_name_part("Mr. John Smith", "John Brown", None) # True: same first name keeps_original_name_part("Dr. Ana Ruiz", "Dr. Maria Lopez", "Dr. {First} {Last}") # False

Source code in src/nemo_safe_synthesizer/pii_replacer/replacement/value_checks.py
def keeps_original_name_part(original: str, candidate: str, pattern: str | None, *, locale: str = "en_US") -> bool:
    """Return whether a full-name replacement keeps the original's first, middle, or last name.

    A whole-name comparison accepts ``John Brown`` for ``John Smith``, which keeps the real first name. Both names are
    read by nameparser in the order of ``pattern`` or ``locale``, so titles such as ``Mr.``, suffixes such as
    ``Jr.``, and a ``Last, First`` layout do not hide a kept part, and each part is compared with the same part.
    Words that are fixed text in ``pattern``, such as ``Dr.`` in ``Dr. {First} {Last}``, are skipped. Trailing
    commas and periods are ignored, and single letters are not compared, so a shared initial does not count.

    Example:
        keeps_original_name_part("Mr. John Smith", "John Brown", None)  # True: same first name
        keeps_original_name_part("Dr. Ana Ruiz", "Dr. Maria Lopez", "Dr. {First} {Last}")  # False
    """
    # Imported here so checking other entity types does not load nameparser or Faker.
    from .person_names import read_full_name

    fixed = _pattern_literal_words(pattern)
    original_parts = read_full_name(original, locale=locale, pattern=pattern)
    candidate_parts = read_full_name(candidate, locale=locale, pattern=pattern)
    for part in ("first", "middle", "last"):
        kept = {word for word in _name_words(original_parts[part]) if len(word) > 1 and word not in fixed}
        if kept & set(_name_words(candidate_parts[part])):
            return True
    return False