python-jsonschema / jsonschema

An implementation of the JSON Schema specification for Python
https://python-jsonschema.readthedocs.io
MIT License
4.58k stars 578 forks source link

`format: uri` doesn't work properly #1192

Closed eirnym closed 9 months ago

eirnym commented 9 months ago

I'm trying to validate a schema where URI must be an actual URI. to achieve this, beside the jsonschema pip package, I've installed requires rfc3987 and rfc3986-validator as described in documentation. TL;DR both don't work properly as I see.

Then I've tried to run a quick test against my main schema and found that URI isn't validated properly. To test it further, I've create a very small test to test for objects with invalid URI format.

In my test below I present 3 invalid URI objects: first is an empty string, second is a number, third contains a space character (which is invalid in URI by RFC).

Could you please explain why does it happening? Are such cases in the test suite used for this project? If my setup is not good enough, what should I change in my setup/fix the code to make this simple test show 3 errors?

The "$schema" key does nothing in this case.

import pytest
import jsonschema

@pytest.mark.parametrize(
    "schema_info",
    (
        "",
        123,
        " 123123 12312 123 123 123 ",
    ),
)
def test_schema(schema_info):
    validator = jsonschema.Draft202012Validator(
        schema={"type": "string", "format": "uri"}
    )
    validator.validate(schema_info)
Julian commented 9 months ago

See the FAQ and the format documentation, you haven't passed in a format checker.

eirnym commented 9 months ago

Could you please connect these 3 parts of the documentation?

Julian commented 9 months ago

Sorry which 3? You mean what specifically are you missing? format_checker=Draft202012Validator.FORMAT_CHECKER.

eirnym commented 9 months ago

Ffor me Google showed this page first: https://python-jsonschema.readthedocs.io/en/latest/validate/, not pages you proposed me to read. The first I've seen was the table with the information what to install. Usually, when I read documentation, I read about some additional dependencies, then how to use them, this page shows this documentation n the opposite way. Probably this is the reason, why I haven't connected the page I've mentioned and page you gave are actually the same.

And the FAQ tells about the reasoning (which is a huge plus for your documentation by the way).