Exdantic.ComputedFieldMeta (exdantic v0.0.1)
View SourceMetadata structure for computed fields in Exdantic schemas.
Computed fields are derived values that are calculated based on the validated data after field and model validation. They extend the validated result with additional computed information.
Summary
Functions
Returns a string representation of the computed field function reference.
Creates a new ComputedFieldMeta struct.
Sets the readonly flag for a computed field metadata.
Converts computed field metadata to a map for debugging or serialization.
Validates that the computation function exists in the specified module.
Updates the description of a computed field metadata.
Adds an example value to a computed field metadata.
Types
Functions
Returns a string representation of the computed field function reference.
Parameters
computed_field_meta
- The ComputedFieldMeta struct
Returns
- String in the format "Module.function/arity"
Examples
iex> meta = Exdantic.ComputedFieldMeta.new(:uppercased, {:type, :string, []}, :upcase, String)
iex> Exdantic.ComputedFieldMeta.function_reference(meta)
"String.upcase/1"
@spec new(atom(), Exdantic.Types.type_definition(), atom(), module()) :: t()
Creates a new ComputedFieldMeta struct.
Parameters
name
- The computed field name (atom)type
- The computed field type definitionfunction_name
- The name of the function to call for computationmodule
- The module containing the computation function
Examples
iex> Exdantic.ComputedFieldMeta.new(:uppercased, {:type, :string, []}, :upcase, String)
%Exdantic.ComputedFieldMeta{
name: :uppercased,
type: {:type, :string, []},
function_name: :upcase,
module: String,
readonly: true
}
Sets the readonly flag for a computed field metadata.
While computed fields are readonly by default, this function allows explicit control over the readonly flag for special cases.
Parameters
computed_field_meta
- The ComputedFieldMeta structreadonly
- Whether the field should be marked as readonly
Examples
iex> meta = Exdantic.ComputedFieldMeta.new(:uppercased, {:type, :string, []}, :upcase, String)
iex> updated = Exdantic.ComputedFieldMeta.set_readonly(meta, false)
iex> updated.readonly
false
@spec to_map(t()) :: %{ name: atom(), type: Exdantic.Types.type_definition(), function_name: atom(), module: module(), description: String.t() | nil, example: term() | nil, readonly: boolean(), function_reference: String.t() }
Converts computed field metadata to a map for debugging or serialization.
Parameters
computed_field_meta
- The ComputedFieldMeta struct
Returns
- Map representation of the computed field metadata
Examples
iex> meta = Exdantic.ComputedFieldMeta.new(:uppercased, {:type, :string, []}, :upcase, String)
iex> |> Exdantic.ComputedFieldMeta.with_description("Uppercased version")
iex> Exdantic.ComputedFieldMeta.to_map(meta)
%{
name: :uppercased,
type: {:type, :string, []},
function_name: :upcase,
module: String,
description: "Uppercased version",
example: nil,
readonly: true,
function_reference: "String.upcase/1"
}
Validates that the computation function exists in the specified module.
Parameters
computed_field_meta
- The ComputedFieldMeta struct
Returns
:ok
if the function exists and has the correct arity{:error, reason}
if the function is invalid
Examples
# For a module that exists with the function
iex> meta = Exdantic.ComputedFieldMeta.new(:result, {:type, :string, []}, :upcase, String)
iex> Exdantic.ComputedFieldMeta.validate_function(meta)
:ok
# For a module with a missing function
iex> meta = Exdantic.ComputedFieldMeta.new(:bad_field, {:type, :string, []}, :missing_function, String)
iex> Exdantic.ComputedFieldMeta.validate_function(meta)
{:error, "Function String.missing_function/1 is not defined"}
Updates the description of a computed field metadata.
Parameters
computed_field_meta
- The ComputedFieldMeta structdescription
- The description text
Examples
iex> meta = Exdantic.ComputedFieldMeta.new(:uppercased, {:type, :string, []}, :upcase, String)
iex> updated = Exdantic.ComputedFieldMeta.with_description(meta, "Uppercased version")
iex> updated.description
"Uppercased version"
Adds an example value to a computed field metadata.
Parameters
computed_field_meta
- The ComputedFieldMeta structexample
- The example value
Examples
iex> meta = Exdantic.ComputedFieldMeta.new(:uppercased, {:type, :string, []}, :upcase, String)
iex> updated = Exdantic.ComputedFieldMeta.with_example(meta, "HELLO")
iex> updated.example
"HELLO"