Ash FrameworkAF
Ash Frameworkโ€ข6mo agoโ€ข
8 replies
Horizon

Optional argument with defaults for actions

It feels like it should be something really simple but its just not working out.

I have a resource with a non-null string attribute that defaults to an empty string
  attributes do
    uuid_primary_key :id

    attribute :title, :string do
      allow_nil? false
      default ""
      description "Row title for display purposes"
    end
  ...
  end


Then how can I get the action to accept an optional argument for title, defaulting to an empty string?

Something like this:
    create :create do
      accept [:title, :data, :table_id, :position]

Will raise an error telling me
title
is required. And something like this

    create :create do
      argument :title, :string, allow_nil?: true, default: ""
      accept [:data, :table_id, :position]
    
      change set_attribute(:title, arg(:title))
      # or even this 
      # change set_attribute(:title, "")

somehow still raises with the same changeset error. I feel like I'm missing something really obvious ๐Ÿ˜“
Solution
You can also constraints: [allow_empty?: true]
Was this page helpful?