Ash.Type.Struct (ash v3.7.6)
View SourceRepresents a struct.
Use the instance_of constraint to specify that it must be an instance of a specific struct.
This cannot be loaded from a database unless the instance_of constraint is provided.
If not, it can only be used to cast input, i.e for arguments.
Alternative: Ash.TypedStruct
For simpler use cases where you want to define a struct with typed fields inline,
consider using Ash.TypedStruct. It provides a DSL for defining structs with:
- Field type specifications and constraints
- Default values
- Required fields (via
allow_nil?: false) - Automatic
new/1andnew!/1functions
Example:
defmodule MyStruct do
use Ash.TypedStruct
typed_struct do
field :name, :string, allow_nil?: false
field :age, :integer, constraints: [min: 0]
field :email, :string, default: nil
end
endAsh.TypedStruct automatically creates an Ash.Type.Struct with the appropriate
constraints under the hood.
Constraints
:instance_of(atom/0) - The module the struct should be an instance of:preserve_nil_values?(boolean/0) - If set to true, when storing, nil values will be kept. Otherwise, nil values will be omitted. The default value isfalse.:fields(keyword/0) - The types of the fields in the struct, and their constraints.
For example:fields: [ amount: [ type: :integer, description: "The amount of the transaction", constraints: [ max: 10 ] ], currency: [ type: :string, allow_nil?: false, description: "The currency code of the transaction", constraints: [ max_length: 3 ] ] ]allow_nil? is true by default
:type(anAsh.Type) - Required.:allow_nil?(boolean/0) - The default value istrue.:description(String.t/0):constraints(keyword/0) - The default value is[].