This repository demonstrates how to properly configure the jsonschema
crate (version 0.30.0) to correctly validate IPv6 addresses.
The jsonschema
crate requires explicitly enabling format validation via the should_validate_formats(true)
method. This is not a bug but rather by design - format validation is optional according to the JSON Schema specification.
To properly validate formats like IPv6 addresses, the validator must be configured as follows:
let validator = jsonschema::draft202012::options()
.should_validate_formats(true) // This line is required!
.build(&schema)?;
Without this configuration, format validation checks will not be performed, and invalid IPv6 addresses would pass validation.
{
"$schema": "http://json-schema.org/draft-2020-12/schema",
"type": "object",
"properties": {
"address": {
"type": "string",
"anyOf": [
{
"format": "ipv4"
},
{
"format": "ipv6"
}
]
}
},
"required": [
"address"
]
}
When properly configured with .should_validate_formats(true)
, the crate correctly:
- Validates proper IPv6 addresses like
2001:0db8:85a3:0000:0000:8a2e:0370:7334
- Rejects invalid addresses like
2001:0db8:85a3:0000:0000:8a2e:0370:7334:5678
(too many segments)
- Clone this repository
- Run
cargo run
- Observe that invalid IPv6 addresses correctly fail validation
The solution was confirmed by the crate maintainer in GitHub Issue #743.
A valid IPv6 address must:
- Have exactly 8 segments (when fully expanded)
- Each segment must be a valid hexadecimal value between 0 and FFFF
- Can be compressed using the
::
notation to represent one or more groups of zeros
- jsonschema: 0.30.0
- Rust: 1.86.0
- OS: Darwin Kernel Version 24.5.0 (x86_64)