"""`format: uri` follows RFC 3986 and agrees with the TypeScript converter.""" import pytest from composio.utils.schema_converter import json_schema_to_pydantic_type from composio.utils.schema_formats import matches_json_schema_format from pydantic import TypeAdapter, ValidationError VALID_URIS = [ "http://example.com", "https://example.com/a?b=c#d", "HTTP://EXAMPLE.COM", "http://[::1]:80/p?q#f", "urn:isbn:0451450523", "mailto:user@example.com", "a:b", ] INVALID_URIS = [ "", "relative/path", "//example.com", "1http://example.com", " http://example.com", "http://exa mple.com", "http://example.com/%ZZ", "http://[bad", "http://example.com#a#b", "http://example.com/\\path", "http://example.com/{a}", "http://example.com|x", "http://ex\u00e9mple.com", ] @pytest.mark.unit @pytest.mark.schema @pytest.mark.parametrize("value", VALID_URIS) def test_uri_format_accepts_rfc3986_uris(value: str) -> None: assert matches_json_schema_format("uri", value) adapter = TypeAdapter( json_schema_to_pydantic_type({"type": "string", "format": "uri"}) ) assert adapter.validate_python(value) == value @pytest.mark.unit @pytest.mark.schema @pytest.mark.parametrize("value", INVALID_URIS) def test_uri_format_rejects_non_uris(value: str) -> None: assert not matches_json_schema_format("uri", value) adapter = TypeAdapter( json_schema_to_pydantic_type({"type": "string", "format": "uri"}) ) with pytest.raises(ValidationError): adapter.validate_python(value)