title: How to add a conditional required field?
body: <p>I have an ecto model for an address with this (simplified) schema:</p>
  
  <pre><code>defmodule Address do
    use Ecto.Model
  
    schema "addresses" do
      field :zip, :string
      field :country, :string
      # snip
    end
  
    @countries_requiring_zip ~w(US) # snip
  
    def changeset(model, params \\ :empty) do
      model
      |&gt; cast(params, ~w(country), ~w(zip))
      |&gt; validate_zip
    end
  
    defp validate_zip(changeset) do
      if get_field(changeset, :country) in @countries_requiring_zip do
        # ????
      end
  
      changeset
    end
  end
  </code></pre>
  
  <p>I want to mark <code>zip</code> as required instead of optional, but only if the country is in a whitelist, but I can't figure out a clean way to write the validation.  How do I add that constraint?</p>