# `Ash.Type.Struct`
[🔗](https://github.com/ash-project/ash/blob/v3.23.1/lib/ash/type/struct.ex#L5)

Represents 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/1` and `new!/1` functions

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
    end

`Ash.TypedStruct` automatically creates an `Ash.Type.Struct` with the appropriate
constraints under the hood.

## Constraints

* `:instance_of` (`t:atom/0`) - The module the struct should be an instance of

* `:preserve_nil_values?` (`t:boolean/0`) - If set to true, nil values will be preserved both when storing and in the casted struct.
  Otherwise, keys whose values are `nil` will be omitted.  
  preserved_nil_values? is false by default The default value is `false`.

* `:fields` (`t: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` (an `Ash.Type`) - Required.

  * `:allow_nil?` (`t:boolean/0`) - The default value is `true`.

  * `:description` (`t:String.t/0`)

  * `:constraints` (`t:keyword/0`) - The default value is `[]`.

# `handle_change?`

# `prepare_change?`

---

*Consult [api-reference.md](api-reference.md) for complete listing*
