<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="353654" data-post-id="353654">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="clifinger" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/clifinger/120/37289_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  clifinger
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote group-Ash-Core-Team" data-username="zachdaniel" data-post="10" data-topic="68874">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zachdaniel/48/31980_2.png" class="avatar"> zachdaniel:</div>
<blockquote>
<p><code>v3.4.56</code></p>
</blockquote>
</aside>
<p>I will switch to it by tomorrow <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="353654" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-define-custom-actions/68874/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-353654" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="353654"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="353660" data-post-id="353660">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="zachdaniel" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zachdaniel/120/31980_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  zachdaniel
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Ash</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="clifinger" data-post="11" data-topic="68874">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/clifinger/48/37289_2.png" class="avatar"> clifinger:</div>
<blockquote>
<p>However, to be completely transparent and honest, I genuinely think the documentation has gaps.</p>
</blockquote>
</aside>
<p>Definitely.  No denying it <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"> We’ll get it sorted, but likely not in the next few days <img src="https://forum.elixirforum.com/images/emoji/apple/sweat.png?v=15" title=":sweat:" class="emoji" alt=":sweat:" loading="lazy" width="20" height="20"> The book has been released into beta, so that’s also a good resource to learn.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="353660" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-define-custom-actions/68874/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-353660" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="353660"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="353664" data-post-id="353664">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="clifinger" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/clifinger/120/37289_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  clifinger
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Where I can buy the book ?</p>
<p>Quick question, I tested with</p>
<aside class="quote no-group">
<blockquote>
<p><code>v3.4.56</code></p>
</blockquote>
</aside>
<p>And it work, but something bother me as I need to duplicate the global validations.<br>
Do you have a hint to reuse some validations from <code>resource</code> in another module ?<br>
Maybe it’s more relative to Elixir and my level but I will appreciate if you can help me on this one <img src="https://forum.elixirforum.com/images/emoji/apple/wink.png?v=15" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="353664" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-define-custom-actions/68874/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-353664" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="353664"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="353665" data-post-id="353665">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="clifinger" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/clifinger/120/37289_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  clifinger
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>My frustration got away :</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">create :register do
      argument :password, :string, allow_nil?: false
      accept [:email]

      validate present(:email), message: "Email must be present"
      validate present(:password), message: "Password must be present"

      change fn changeset, _ -&gt;
        changeset |&gt; maybe_hash_password()
      end
    end
  end

  defp maybe_hash_password(changeset) do
    password = Ash.Changeset.get_argument(changeset, :password)

    if password &amp;&amp; changeset.valid? do
      changeset
      |&gt; Ash.Changeset.change_attribute(:hashed_password, Argon2.hash_pwd_salt(password))
      |&gt; Ash.Changeset.clear_change(:password)
    else
      changeset
    end
  end
</code></pre>
<p>Both Login and register work <img src="https://forum.elixirforum.com/images/emoji/apple/wink.png?v=15" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="353665" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-define-custom-actions/68874/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-353665" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="353665"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="353669" data-post-id="353669">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="zachdaniel" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zachdaniel/120/31980_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  zachdaniel
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Ash</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>The book is available here: <a href="https://pragprog.com/titles/ldash/ash-framework/" class="inline-onebox" rel="noopener nofollow ugc">Ash Framework: Create Declarative Elixir Web Apps by Rebecca Le and Zach Daniel</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="353669" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-define-custom-actions/68874/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-353669" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="353669"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #15"></div>
  </section>
</div>
    <div class="postbit" id="353670" data-post-id="353670">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="clifinger" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/clifinger/120/37289_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  clifinger
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Ok I will buy it asap.</p>
<p>Can you help me please ? :</p>
<aside class="quote no-group" data-username="clifinger" data-post="14" data-topic="68874">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/clifinger/48/37289_2.png" class="avatar"> clifinger:</div>
<blockquote>
<p>And it work, but something bother me as I need to duplicate the global validations.<br>
Do you have a hint to reuse some validations from <code>resource</code> in another module ?<br>
Maybe it’s more relative to Elixir and my level but I will appreciate if you can help me on this one <img src="https://forum.elixirforum.com/images/emoji/apple/wink.png?v=15" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>
</blockquote>
</aside> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="353670" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-define-custom-actions/68874/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-353670" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="353670"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #16"></div>
  </section>
</div>
    <div class="postbit" id="353673" data-post-id="353673">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="zachdaniel" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zachdaniel/120/31980_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  zachdaniel
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Ash</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Anonymous functions are just one way to define changes/preparations. You can use modules that implement the appropriate behavior.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">prepare PreparationModule
....
validate ValidationModule
...
change ChangeModule
</code></pre>
<p>In all of those cases you can also add opts to reuse the behavior but customize per use.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">change {SomeChange, opt: :value}
</code></pre>
<p>You can’t use a change as a preparation or a validation etc. so within those modules if you have to share logic you do it with “regular functions”. I.e if you want a function that takes a string and tells you if it’s a valid email, you can put that in a module alongside your resource and call into it from the resource.</p>
<p>Each of the ways of using those types of modules I mentioned above is explained in its respective guide: <a href="https://hexdocs.pm/ash/changes.html#custom-changes" class="inline-onebox" rel="noopener nofollow ugc">Changes — ash v3.29.3</a></p>
<p>The book also goes into this and how to use/think about it in detail.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="353673" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-define-custom-actions/68874/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-353673" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="353673"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-solved cat-solved" title="Marked as solution"></div>
  </section>
</div>
    <div class="postbit" id="353675" data-post-id="353675">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="clifinger" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/clifinger/120/37289_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  clifinger
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I ended up by:</p>
<pre data-code-wrap="ex"><code class="lang-ex">defmodule VsApp.Accounts.Constants do
  @moduledoc """
  Centralized constants for account-related validations and messages.
  """

  @validation_rules %{
    email: %{
      regex: ~r/^[\w.+-]+@\w+\.\w+$/,
      max_length: 160,
      messages: %{
        format: "Invalid email format",
        length: "Email should have max length of 160",
        presence: "Email must be present",
        unique: "Email should be unique"
      }
    },
    password: %{
      min_length: 6,
      messages: %{
        length: "Password should have min length of 6",
        presence: "Password must be present"
      }
    }
  }

  # Getters for validations
  def email_regex, do: get_in(@validation_rules, [:email, :regex])
  def email_max_length, do: get_in(@validation_rules, [:email, :max_length])
  def password_min_length, do: get_in(@validation_rules, [:password, :min_length])

  # Getters for messages
  def message(:email, key), do: get_in(@validation_rules, [:email, :messages, key])
  def message(:password, key), do: get_in(@validation_rules, [:password, :messages, key])

  # Validation helper
  def valid_email?(email) do
    email &amp;&amp; is_binary(email) &amp;&amp; Regex.match?(email_regex(), email)
  end
end

</code></pre>
<p>And:</p>
<pre data-code-wrap="ex"><code class="lang-ex">defmodule VsApp.Accounts.Validations.ValidLoginPreparation do
  use Ash.Resource.Preparation
  require Logger
  require Ash.Query
  alias VsApp.Accounts.Constants

  @impl true
  def init(opts), do: {:ok, opts}

  @impl true
  def prepare(query, _opts, _context) do
    password = Ash.Query.get_argument(query, :password)
    email = Ash.Query.get_argument(query, :email)

    cond do
      is_nil(email) or not is_binary(email) -&gt;
        Ash.Query.add_error(query, field: :email, message: Constants.message(:email, :presence))

      not Regex.match?(Constants.email_regex(), email) -&gt;
        Ash.Query.add_error(query, field: :email, message: Constants.message(:email, :format))

      String.length(email) &gt; Constants.email_max_length() -&gt;
        Ash.Query.add_error(query, field: :email, message: Constants.message(:email, :length))

      is_nil(password) or not is_binary(password) -&gt;
        Ash.Query.add_error(query,
          field: :password,
          message: Constants.message(:password, :presence)
        )

      String.length(password) &lt; Constants.password_min_length() -&gt;
        Ash.Query.add_error(query,
          field: :password,
          message: Constants.message(:password, :length)
        )

      true -&gt;
        query
        |&gt; Ash.Query.filter(email == ^email)
        |&gt; Ash.Query.load([:hashed_password])
        |&gt; Ash.Query.after_action(fn _, user -&gt;
          case user do
            [user] -&gt;
              if Argon2.verify_pass(password, user.hashed_password) do
                {:ok, [user]}
              else
                {:ok, []}
              end

            [] -&gt;
              Argon2.no_user_verify()
              {:ok, []}

            _ -&gt;
              {:ok, []}
          end
        end)
    end
  end
end
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="353675" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-define-custom-actions/68874/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-353675" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="353675"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #18"></div>
  </section>
</div>
    <div class="postbit" id="353679" data-post-id="353679">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="zachdaniel" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zachdaniel/120/31980_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  zachdaniel
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Ash</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>It is on the short list to allow validations to be run against queries, which will make this experience much nicer. i.e <code>validate match(:email, ~r//), message:  "..."</code> For now, however, preparations is the only way. Sorry about that!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="353679" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-define-custom-actions/68874/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-353679" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="353679"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>