rfc3986-validator
A pure Python regex-based validator for RFC3986 URIs and URI references
Repository Health
Technical Analysis
rfc3986-validator is a small Python package that checks whether a string is a syntactically valid URI (or URI-reference) per RFC3986, the IETF grammar underlying most modern URI/URL formats. It implements the RFC’s ABNF grammar directly as compiled regular expressions — covering scheme, authority (including IPv4 and IPv6 host literals), path, query, and fragment components — and exposes a single validate_rfc3986() function.
Like its sibling rfc3339-validator, it’s widely used transitively as the default uri/uri-reference format checker inside the jsonschema ecosystem, giving it outsized reach relative to its tiny codebase.
What You Get
- A single
validate_rfc3986(uri, rule=...)function returning a boolean - Full RFC3986 ABNF grammar coverage: scheme, authority, userinfo, IPv4/IPv6 host literals, path, query, fragment
- Support for both absolute
URIand relativeURI_referencevalidation via theruleparameter - Zero runtime dependencies — pure Python regex, no external parsing library
Common Use Cases
- Implementing the
uri/uri-referenceformat checker for JSON Schema validation (used internally byjsonschema) - Validating URL/URI fields in API request payloads before storing or dereferencing them
- Rejecting malformed URIs (e.g. unencoded spaces) in user-submitted links
- Distinguishing absolute URIs from relative references in link-processing pipelines
Under The Hood
Architecture — the module builds up RFC3986’s grammar compositionally: an IPv6_RE regex feeds into AUTHORITY_RE (host/userinfo/port), which feeds into URI_RE and a second URI_REFERENCE_RE for relative references, each pre-compiled once at import time; validate_rfc3986() simply picks the right compiled pattern based on the rule argument and calls .match().
Tech Stack — pure Python standard library only (re), no third-party runtime dependencies, packaged with plain setuptools.
Code Quality — the test suite exercises both URI and URI_reference rules against valid and invalid examples (encoded/unencoded characters, IPv6 authorities, missing schemes); the regex-heavy implementation trades some readability for direct fidelity to the RFC’s ABNF, with comments mapping each regex fragment back to its grammar rule.
API Design — a single function with a string argument and an optional rule keyword covers both use cases (strict absolute URI vs. permissive URI-reference) without needing separate functions or classes, keeping the surface area minimal for library consumers like jsonschema.