Skip to content

"ipv4" format validator rejects 0.x.y.z IPs #512

@GREsau

Description

@GREsau
let schema = jsonschema::compile(&json!({
    "type": "string",
    "format": "ipv4"
})).unwrap();

println!("{}", schema.is_valid(&json!("0.0.0.0")));

This outputs false, when I would expect true because 0.0.0.0 is a valid dotted-quad representation of an IPv4 address. All other IP addresses starting with 0 (e.g. 0.1.2.3) are similarly rejected.

This appears to be due to the fix for json-schema-org/JSON-Schema-Test-Suite#469 (commit 7d32eff), but I think this particular fix is simultaneously:

  • too strict, e.g. it rejects 0.0.0.0
  • too loose because it allows leading zeroes in the second/third/fourth octets, e.g. it allows 1.0123.1.1
  • unnecessary, because Ipv4Addr's FromStr implementation already disallows leading zeros in any octet. Edit: this was only added in rust 1.58 (Reject octal zeros in IPv4 addresses rust-lang/rust#86984), so the explicit check in this crate wasn't unnecessary when it was first implemented!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions