Stranger6667/jsonschema

`"ipv4"` format validator rejects 0.x.y.z IPs

Closed this issue · 2 comments

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 (rust-lang/rust#86984), so the explicit check in this crate wasn't unnecessary when it was first implemented!

Thank you! Indeed, you are right! I’ll fix it soon

Fixed in 0.19.1