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


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="spacebat" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/spacebat/120/25330_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  spacebat
                    <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, so <code>change</code> is early on but still post “basic input validation”. Is that different from the validation that the warning was about? Is there somewhere the full sequence of events is documented?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="352060" 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/best-fix-for-changeset-has-already-been-validated-for-action-warning/68577/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-352060" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="352060"
                     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="352065" data-post-id="352065">
  <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>We should enhance the docs here to talk about the way that changes/validations are executed. It’s documented in the changeset functions that run that logic. We should probably but it in module docs or a guide somewhere.</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://ash.hexdocs.pm/Ash.Changeset.html">
  <header class="source">

      <a href="https://ash.hexdocs.pm/Ash.Changeset.html" target="_blank" rel="noopener nofollow ugc">ash.hexdocs.pm</a>
  </header>

  <article class="onebox-body">
    

<h3><a href="https://ash.hexdocs.pm/Ash.Changeset.html" target="_blank" rel="noopener nofollow ugc">Ash.Changeset — ash v3.29.3</a></h3>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>When I said “basic input validation” I mean that we handle the types of each attribute/argument and validate them before setting them. So in the below example, you won’t ever see <code>foo</code> as <code>10</code>. But the changes still run on an invalid changeset.</p>
<p>Each change and validation is run in the order they appear. When we say that “ypass validations or other action logic”, we’re referring to validations in the action.</p>
<p>Here is an example:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">create :create do
  argument :foo, :string, allow_nil?: false

  change fn changeset, _ -&gt; 
    if changeset.arguments[:foo] == "fred" do
      Ash.Changeset.change_attribute(changeset, :is_fred, true)
    else
      changeset
    end
  end

  validate attribute_does_not_equal(:is_fred, true)
end
</code></pre>
<p>With the above construction, if you passed in <code>%{foo: "fred"}</code> to the function, it would set <code>is_fred</code> to <code>true</code>, and the validation would fail, because the validation comes after the change.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">create :create do
  argument :foo, :string, allow_nil?: false

  change fn changeset, _ -&gt;
    Ash.Changeset.before_action(changeset, fn changeset -&gt; 
      if changeset.arguments[:foo] == "fred" do
        Ash.Changeset.change_attribute(changeset, :is_fred, true)
      else
        changeset
      end
    end)
  end

  validate attribute_does_not_equal(:is_fred, true)
end
</code></pre>
<p>With this construction, the order of events looks like this:</p>
<ol>
<li>validate arguments</li>
<li>run the change, which adds a hook (to be executed later)</li>
<li>run the validation, which doesn’t see the attribute change (because its in a hook to be executed later)</li>
<li>return the changeset</li>
</ol>
<p>So all of the above happens when you do</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Ash.Changeset.for_action(Resource, :create, %{foo: "foo"})
</code></pre>
<p>In the second example you get back a changeset with a hook on it.</p>
<p>Then it can be passed to <code>Ash.create</code> to actually run the action, which runs any associated hooks.</p>
<p>When using things like <code>AshGraphql</code> or <code>AshJsonApi</code> or code interfaces etc. this is kind of transparent to the caller because it both builds a changeset and runs it on the spot. But Ash actions are actually two phase things, with the idea that phase 1 is cheap and repeatable. If using <code>AshPhoenix.Form</code> for example, each change runs on every keypress because we build the changeset each time.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="352065" 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/best-fix-for-changeset-has-already-been-validated-for-action-warning/68577/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-352065" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="352065"
                     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="352066" data-post-id="352066">
  <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>There are also docs in each action showing what the action does w/ the changeset given to it: <a href="https://hexdocs.pm/ash/create-actions.html#what-happens-when-you-run-a-create-action" class="inline-onebox" rel="noopener nofollow ugc">Create Actions — ash v3.29.3</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="352066" 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/best-fix-for-changeset-has-already-been-validated-for-action-warning/68577/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-352066" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="352066"
                     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="352067" data-post-id="352067">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="spacebat" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/spacebat/120/25330_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  spacebat
                    <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>That’s a lot to chew on, thanks I’m sure re-reading this in the morning will be fruitful. I can’t wait for the Ash book <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="352067" 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/best-fix-for-changeset-has-already-been-validated-for-action-warning/68577/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-352067" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="352067"
                     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>