Product case study · November 18, 2026

Ask Claude Code to reject the almost-valid ID

Repair a Python ID validator with Claude Code. Test trailing text, final newlines and Unicode digits before accepting a regex fix.

Reading time
6 min
Checked
Nov 18, 2026
A charcoal paper inspection frame checks a blue ticket while an overlong ochre ticket catches against its edge.
Validation should account for the whole input, including the part that almost fits.
Bottom line

Give the agent examples it must reject before asking it to repair a validator. A matching prefix is not a valid identifier.

JOB-0042 passes your validator. So does JOB-0042-delete-everything. The regular expression found the part it wanted and never asked whether anything came after it.

This is a useful small job for Claude Code because the tempting repair can still be wrong. Adding an end anchor leaves a final-newline trap. Switching to whole-string matching leaves a separate question: which digits does this identifier allow?

The exercise uses invented job IDs, Bash on macOS, Linux or WSL, Python 3, and an installed, signed-in Claude Code. The Python fixture and reference repairs were tested locally; the Claude prompt is an authored exercise, not a recorded agent run. Official sources were checked November 18, 2026.

Make the overly friendly validator

Create a disposable folder and its first file:

id_lab=$(mktemp -d /tmp/claude-id-validation.XXXXXX)
cd "$id_lab"
cat > job_id.py <<'PY'
import re


def is_job_id(value):
    return re.match(r"JOB-\d{4}", value) is not None
PY
python3 - <<'PY'
from job_id import is_job_id
for value in ["JOB-0042", "JOB-0042-extra", "JOB-0042\n"]:
    print(repr(value), is_job_id(value))
PY

All three return True in the local reproduction. Using repr() makes the hidden newline visible in the output.

Python’s regular-expression reference describes match() as checking at the start of a string. That is useful when extracting a prefix. Here, the function is supposed to judge an entire identifier.

Write down what counts as an ID

Save this as SPEC.md:

is_job_id(value) receives a Python str and returns a bool.
Accept exactly JOB- followed by four ASCII digits, 0 through 9.
Leading zeroes are allowed. Case is significant.
Reject extra characters, spaces, final newlines and non-ASCII digits.
Do not strip, normalize, case-fold or convert the input.
Non-string inputs are outside this exercise's contract.
Only job_id.py may change. Keep this specification and tests fixed.
Use the Python standard library; do not install packages.

The ASCII restriction is a choice for this fictional machine identifier. It is not a general rule for names, addresses or human text. Decide the real format with the system that issues IDs before copying the pattern into a product.

Save this as test_job_id.py:

import unittest
from job_id import is_job_id


class JobIdTests(unittest.TestCase):
    def test_contract(self):
        cases = [
            ("JOB-0000", True),
            ("JOB-0042", True),
            ("JOB-9999", True),
            ("", False),
            ("job-0042", False),
            (" JOB-0042", False),
            ("JOB-0042 ", False),
            ("JOB-042", False),
            ("JOB-00420", False),
            ("JOB-0042-extra", False),
            ("JOB-0042\n", False),
            ("JOB-0042\r\n", False),
            ("JOB-\u0660\u0660\u0664\u0662", False),
            ("JOB-\uff10\uff10\uff14\uff12", False),
        ]
        for value, expected in cases:
            with self.subTest(value=repr(value)):
                self.assertIs(is_job_id(value), expected)

The last two entries contain Arabic-Indic and fullwidth decimal digits, written as escapes so the fixture is easy to copy. Run:

python3 -m unittest -v test_job_id

The original failed seven of the fourteen cases locally. The runner reports one test method with failing subtests, not fourteen separate test methods. Python documents that reporting model in its unittest guide. assertIs also checks that the function returns an actual boolean rather than a match object.

Give Claude the contract and the counterexamples

Start claude in the practice directory and paste:

Read SPEC.md, job_id.py and test_job_id.py.
Run python3 -m unittest -v test_job_id and report the failing inputs.
Explain why checking a prefix is insufficient for this contract.
Change only job_id.py. Do not change the tests or specification.
Do not install packages, commit or delegate.
Check whether your fix rejects final newlines and non-ASCII digits.
Do not strip or normalize input to make it pass.
Run the same tests after your edit and report the actual result.
Explain what these tests do not prove about a real job system.

Anthropic’s common workflows recommends giving Claude the reproduction command and concrete debugging context. This prompt adds the acceptance contract so the agent does not quietly redefine validity. Review tool permissions for the practice folder; the requested scope is not a filesystem sandbox.

I would keep the rejection cases visible during review. A tidy-looking regex is easy to approve too quickly; a table showing exactly what it still accepts is harder to misread.

Catch the two plausible partial fixes

These are candidate implementations to review, not extra requirements for Claude to use:

Candidate return expressionRemaining failure in this fixture
re.match(r"JOB-[0-9]{4}$", value) is not NoneAccepts a final \n
re.fullmatch(r"JOB-\d{4}", value) is not NoneAccepts both non-ASCII digit examples
re.fullmatch(r"JOB-[0-9]{4}", value) is not NonePasses all fourteen cases

Python documents two independent details behind that table: $ can match just before a final newline, and \d in a string pattern includes Unicode decimal digits. fullmatch() requires the whole string to fit the pattern. See the syntax and matching reference.

The complete reference repair is:

import re


def is_job_id(value):
    return re.fullmatch(r"JOB-[0-9]{4}", value) is not None

Both partial repairs and the complete repair were run against the unchanged table locally. Their failing subtest counts were one, two and zero respectively. This is evidence about this fixture, not a measurement of Claude’s success rate.

Verify the edited file yourself

Exit the agent session, inspect job_id.py, then rerun:

python3 -m unittest -v test_job_id

Expect OK with no failing subtests. If the test command cannot import the module, check that you are in the practice folder and both filenames match. If it passes suspiciously early, compare the tests with the published table and check whether the agent changed expected values. If only newline or Unicode cases fail, use the candidate table to locate the incomplete repair.

Reject a patch that calls .strip() before matching: it turns an invalid input into a different, valid input. A user-facing form may deliberately normalize input, but that needs its own documented behavior and tests.

The finished function answers one narrow question: does this string have the required shape? It cannot establish that the job exists, belongs to the current user or may be modified. Keep those checks at the application’s lookup and authorization boundaries. An ID that fits the stencil is still only an ID.

Sources

Checked November 18, 2026:

Put this to work

How to specify exact identifier rules before an agent edits a regular expression.

Try

Give Claude Code a small validator and a fixed set of valid and invalid inputs.

Prove it worked

Run the same test table against the original, two partial repairs and the final repair.

Keep in view

  • Expose a validator that accepts an ID followed by unwanted text.
  • Separate whole-string matching from the choice of allowed digits.
  • Check incomplete fixes against unchanged acceptance tests.
Learn the workflow: Start a scoped coding session with Claude Code