Skip to content

Accept one group before '::' in the ipv6 format - #223

Merged
horejsek merged 1 commit into
horejsek:masterfrom
pcbeingused333:fix/ipv6-one-group-before-double-colon
Oct 20, 2026
Merged

horejsek merged 1 commit into
horejsek:masterfrom
pcbeingused333:fix/ipv6-one-group-before-double-colon

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

The ipv6 format rejects valid addresses that have exactly one group before :: and six groups after it (counting an IPv4 tail as two):

>>> import fastjsonschema
>>> v = fastjsonschema.compile({"type": "string", "format": "ipv6"})
>>> v("1::2:3:4:5:6")
fastjsonschema.exceptions.JsonSchemaValueException: data must be ipv6
>>> v("1::2:3:4:1.2.3.4")
fastjsonschema.exceptions.JsonSchemaValueException: data must be ipv6
>>> import ipaddress; ipaddress.IPv6Address("1::2:3:4:5:6")
IPv6Address('1::2:3:4:5:6')

In draft04.py, the regex alternative for "up to two groups before ::" is (?:h:h)?::(?:h:){3}..., which allows exactly two groups or none. The other alternatives use (?:(?:h:){,n}h)?. This PR uses the same form here ({,1}).

The current JSON-Schema-Test-Suite has a case for this: optional/format/ipv6.json, "one group before '::' and five groups after is valid". The suite pinned in this repo (Test-JSON-Schema-Acceptance-1.026) is older and doesn't include it.

Tests

  • tests/test_format.py::test_ipv6: added 1::2:3:4:5:6 and 1::2:3:4:1.2.3.4 (valid), plus 1::2:3:4:5:6:7:8 (invalid, too many groups). The two valid cases fail on master and pass with this change.
  • Every address shape was compared with ipaddress.IPv6Address: 0–8 groups before and after ::, with and without an IPv4 tail, plus the fully written-out forms.
    • Master: 2 mismatches (1::10:11:12:13:14 and 1::10:11:12:1.2.3.4).
    • This branch: none.
  • pytest tests with the pinned suite: 4512 passed, 449 xfailed, 936 xpassed. The 12 errors are in tests/benchmarks, which need pytest-benchmark (not installed in that run).
  • pylint fastjsonschema: no new warnings.

This change was written with an AI coding assistant (Claude Code).

🤖 Generated with Claude Code

The ipv6 regex alternative for "up to two groups before '::', three
groups plus a final pair (or IPv4) after" was written as
`(?:h:h)?::`, which allows exactly two groups or none, never one. So
`1::2:3:4:5:6` and `1::2:3:4:1.2.3.4` were rejected although they are
valid (RFC 4291 2.2). Every other alternative uses `(?:(?:h:){,n}h)?`;
use the same form here.

The new JSON-Schema-Test-Suite has a case for this in
optional/format/ipv6.json ("one group before '::' and five groups after
is valid").

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@horejsek
horejsek merged commit 68c2ad4 into horejsek:master Oct 20, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants