View Source Dequel.Semantic.Coerce (Dequel v0.4.1)
Type coercion for query values.
Converts string values from the parser into their proper typed equivalents based on field type information from the schema resolver.
Supported Types
| Type | Input | Output |
|---|---|---|
:integer | "25" | 25 |
:float | "3.14" | 3.14 |
:boolean | "true"/"false"/"1"/"0" | true/false |
:date | "2024-01-15" | ~D[2024-01-15] |
:naive_datetime | "2024-01-15T10:30:00" | ~N[2024-01-15 10:30:00] |
:utc_datetime | "2024-01-15T10:30:00Z" | ~U[2024-01-15 10:30:00Z] |
:decimal | "19.99" | Decimal.new("19.99") |
:string/unknown | "foo" | "foo" (unchanged) |
Invalid values are returned unchanged, letting the adapter or runtime handle errors.
Summary
Functions
Coerces a string value to the given type.
Functions
Coerces a string value to the given type.
Returns the original value if coercion fails or type is unknown.
Examples
iex> Dequel.Semantic.Coerce.coerce("42", :integer)
42
iex> Dequel.Semantic.Coerce.coerce("-123", :integer)
-123
iex> Dequel.Semantic.Coerce.coerce("invalid", :integer)
"invalid"
iex> Dequel.Semantic.Coerce.coerce("3.14", :float)
3.14
iex> Dequel.Semantic.Coerce.coerce("true", :boolean)
true
iex> Dequel.Semantic.Coerce.coerce("false", :boolean)
false
iex> Dequel.Semantic.Coerce.coerce("1", :boolean)
true
iex> Dequel.Semantic.Coerce.coerce("0", :boolean)
false
iex> Dequel.Semantic.Coerce.coerce("2024-01-15", :date)
~D[2024-01-15]
iex> Dequel.Semantic.Coerce.coerce("2024-01-15T10:30:00", :naive_datetime)
~N[2024-01-15 10:30:00]
iex> Dequel.Semantic.Coerce.coerce("2024-01-15T10:30:00Z", :utc_datetime)
~U[2024-01-15 10:30:00Z]
iex> Dequel.Semantic.Coerce.coerce("hello", :string)
"hello"
iex> Dequel.Semantic.Coerce.coerce("hello", :unknown_type)
"hello"Non-string values pass through unchanged:
iex> Dequel.Semantic.Coerce.coerce(42, :integer)
42
iex> Dequel.Semantic.Coerce.coerce(nil, :string)
nil