Estructura.Nested.Type.URI (estructura v1.9.0)

View Source

Estructura 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 testing
  • coerce/1 - Attempts to convert input into a URI
  • validate/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

coerce(term)

Attempts to coerce a value into a URI.

Delegates to URI.new/1 which handles various input formats including:

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"}

generate(opts \\ [])

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: "/"}

validate(term)

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\""}