rfc3987-syntax2ο
Helper functions to parse and validate the syntax of terms defined in RFC 3987 β the IETF standard for Internationalized Resource Identifiers (IRIs).
[!NOTE]
This is a maintained fork of https://github.com/willynilly/rfc3987-syntax.
π― Purposeο
The goal of rfc3987-syntax2 is to provide a lightweight, permissively licensed Python module for validating that strings conform to the ABNF grammar defined in RFC 3987. These helpers are:
β Strictly aligned with the syntax rules of RFC 3987
β Built using a permissive MIT license
β Designed for both open source and proprietary use
β Powered by Lark, a fast, EBNF-based parser
π§ Note: This project focuses on syntax validation only. RFC 3987 specifies additional semantic rules (e.g., Unicode normalization, BiDi constraints, percent-encoding requirements) that are intentionally out of scope.
π License, Attribution, and Citationο
rfc3987-syntax2 is licensed under the MIT License, which allows reuse in both open source and commercial software.
This project:
β Does not depend on the
rfc3987Python package (GPL-licensed)β Uses
lark, licensed under MITβ Implements grammar from RFC 3987, using RFC 3986 where RFC 3987 delegates syntax
β οΈ This project is not affiliated with or endorsed by the authors of RFC 3987 or the
rfc3987Python package.
β οΈ Limitationsο
The grammar and parser enforce only the ABNF syntax defined in RFC 3987 (and referenced RFC 3986 productions where applicable).
The following are not validated and must be handled separately for full compliance:
Unicode Normalization Form C (NFC)
Bidirectional text (BiDi) constraints (RFC 3987 Β§4.1)
Port number ranges (must be 0β65535)
Context-aware percent-encoding requirements
Additional host/network semantics beyond ABNF syntax (for example, canonical representation expectations)
ChatGPT 40 was used during the original development process. Errors may exist due to this assistance. Additional review, testing, and bug fixes by human experts is welcome.
π¦ Installationο
pip install rfc3987-syntax2
π Documentationο
Complete documentation is available on Read the Docs. This includes:
Responsibilities & Capabilities
Install Instructions
API Reference
Usage Examples
π Usageο
List all supported βtermsβ (i.e., non-terminals and terminals within ABNF production rules) used to validate the syntax of an IRI according to RFC 3987ο
from rfc3987_syntax2 import RFC3987_SYNTAX_TERMS
print("Supported terms:")
for term in RFC3987_SYNTAX_TERMS:
print(term)
Syntactically validate a string using the general-purpose validatorο
from rfc3987_syntax2 import is_valid_syntax
if is_valid_syntax(term='iri', value='http://github.com'):
print("β Valid IRI syntax")
if not is_valid_syntax(term='iri', value='bob'):
print("β Invalid IRI syntax")
if is_valid_syntax(term='iri_reference', value='bob'):
print("β Valid IRI-reference syntax")
Alternatively, use term-specific helpers to validate RFC 3987 syntax.ο
from rfc3987_syntax2 import is_valid_syntax_iri
from rfc3987_syntax2 import is_valid_syntax_iri_reference
if is_valid_syntax_iri('http://github.com'):
print("β Valid IRI syntax")
if not is_valid_syntax_iri('bob'):
print("β Invalid IRI syntax")
if is_valid_syntax_iri_reference('bob'):
print("β Valid IRI-reference syntax")
Get the Lark parse tree for a syntax validation (useful for additional semantic validation)ο
from rfc3987_syntax2 import parse
ptree: ParseTree = parse(term="iri", value="http://github.com")
print(ptree)
π Sourcesο
This grammar was derived from:
RFC 3987 β Internationalized Resource Identifiers (IRIs)
β Defines IRI syntax and extensions to URI (e.g. Unicode characters,ucschar)
β https://datatracker.ietf.org/doc/html/rfc3987RFC 3986 β Uniform Resource Identifier (URI): Generic Syntax
β Provides reusable components likescheme,authority,ipv4address, etc.
β https://datatracker.ietf.org/doc/html/rfc3986
π When
RFC 3986is listed as the source, it is used in accordance with RFC 3987, which explicitly references it for foundational elements.
Rule-to-Source Mappingο
Rule/Component |
Source |
Notes |
|---|---|---|
|
RFC 3987 |
Top-level IRI rule |
|
RFC 3987 |
Top-level IRI Reference rule |
|
RFC 3987 |
Top-level Absolute IRI rule |
|
RFC 3986 |
Referenced by RFC 3987 Β§2.2 |
|
RFC 3987 |
IRI-specific hierarchy |
|
RFC 3987 |
IRI-specific relative ref |
|
RFC 3987 |
IRI-specific relative part |
|
RFC 3987 |
IRI authority production |
|
RFC 3987 |
IRI userinfo production |
|
RFC 3987 |
IRI host production |
|
RFC 3987 |
IRI reg-name production |
|
RFC 3987 |
Union of IRI path variants |
|
RFC 3987 |
IRI path format variant |
|
RFC 3987 |
Absolute path |
|
RFC 3987 |
Path disallowing scheme prefix |
|
RFC 3987 |
Used in non-scheme contexts |
|
RFC 3987 |
Empty IRI path |
|
RFC 3987 |
Query extension to URI |
|
RFC 3987 |
Fragment extension to URI |
|
RFC 3987 |
IRI path characters and segments |
|
RFC 3987 |
IRI-specific path constraint |
|
RFC 3987 |
Includes |
|
RFC 3987 |
Unicode support |
|
RFC 3986 |
Reserved characters |
|
RFC 3986 |
IPv6 or IPvFuture in |
|
RFC 3986 |
RFC 3986 IPv6address production; syntax-level validation only |
|
RFC 3986 |
Forward-compatible |
|
RFC 3986 |
Dotted-decimal IPv4 |
|
RFC 3986 |
Final 32 bits of IPv6 |
|
RFC 3986 |
Hex and decimal chunks |
|
RFC 3986 |
Optional numeric |
|
RFC 3986 |
Percent encoding (e.g. |
|
RFC 3986 |
Character classes |
|
Project helper |
Local helper token used in |
|
RFC 3986 |
Used by |
Contents: