Estructura.Nested.Type.URI (estructura v1.9.0)
View SourceEstructura
type implementation for handling URI
values.
This type provides functionality for:
- Generating URI values for testing
- Coercing string and map inputs into URI structs
- Validating URI values
Examples
Estructura.Nested.Type.URI.validate(URI.parse("https://example.com/path?query=value")) #⇒ {:ok, %URI{scheme: "https", host: "example.com", path: "/path", query: "query=value"}}
Estructura.Nested.Type.URI.validate("not a uri") #⇒ {:error, "Expected URI, got: \"not a uri\""}
The type implements the Estructura.Nested.Type
behaviour, providing:
generate/1
- Creates random URI values for property testingcoerce/1
- Attempts to convert input into a URIvalidate/1
- Ensures a value is a valid URI
Summary
Functions
Attempts to coerce a value into a URI.
Generates random URI values for property-based testing.
Validates that a term is a valid URI.
Functions
Attempts to coerce a value into a URI.
Delegates to URI.new/1
which handles various input formats including:
- URI strings ("https://example.com")
- Maps with URI components
Examples
Estructura.Nested.Type.URI.coerce("https://example.com/path?query=value") #⇒ {:ok, %URI{scheme: "https", host: "example.com", path: "/path", query: "query=value"}}
Estructura.Nested.Type.URI.coerce("invalid uri") #⇒ {:error, "Invalid URI format"}
Generates random URI values for property-based testing.
Options
Accepts all options supported by Estructura.StreamData.uri/1
, including:
:schemes
- List of allowed schemes (default: ["http", "https"]):hosts
- List of allowed hosts (default: generates random hosts):paths
- List of allowed paths (default: generates random paths):with_query
- Whether to include query parameters (default: true)
Examples
Estructura.Nested.Type.URI.generate() |> Enum.take(1) |> List.first() #⇒ %URI{scheme: "https", host: "example.com", path: "/some/path"}
Estructura.Nested.Type.URI.generate(schemes: ["ftp"]) |> Enum.take(1) |> List.first() #⇒ %URI{scheme: "ftp", host: "example.com", path: "/"}
Validates that a term is a valid URI.
Returns {:ok, uri}
for valid URI values,
or {:error, reason}
for invalid ones.
Examples
Estructura.Nested.Type.URI.validate(URI.parse("https://example.com")) #⇒ {:ok, %URI{scheme: "https", host: "example.com"}}
Estructura.Nested.Type.URI.validate("not a uri") #⇒ {:error, "Expected URI, got: \"not a uri\""}